How to Convert PDF to HTML from File (node for PDF to HTML API in JavaScript and PDF.co Web API)

PDF.co Web API is the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode barcodes from scans, pictures and PDF.

HTML is wastly used formatting and many times we require to build a quick webpage straight from PDF. Internally PDF has a very complicated structure as the core usefulness of PDF is for printing purpose only. In requirements like these when PDF to HTML conversion is needed, PDF.co’s feature to convert PDF to HTML can be very helpful. In this article we’ll review how we can convert PDF to HTML with NodeJS. The code snippet in this article is also available on our GitHub repository at this location.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

app.js

      
/*jshint esversion: 6 */ var https = require("https"); var path = require("path"); var fs = require("fs"); // `request` module is required for file upload. // Use "npm install request" command to install. var request = require("request"); // The authentication key (API Key). // Get your own by registering at https://app.pdf.co/documentation/api const API_KEY = "***********************************"; // Source PDF file const SourceFile = "./sample.pdf"; // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'. const Pages = ""; // PDF document password. Leave empty for unprotected documents. const Password = ""; // Destination HTML file name const DestinationFile = "./result.html"; // Set to `true` to get simplified HTML without CSS. Default is the rich HTML keeping the document design. const PlainHtml = 'False'; // Set to `true` if your document has the column layout like a newspaper. const ColumnLayout = 'False'; // Prepare URL for `PDF To HTML` API endpoint var query = `https://api.pdf.co/v1/pdf/convert/to/html`; let reqOptions = { uri: query, headers: { "x-api-key": API_KEY }, formData: { name: path.basename(DestinationFile), password: Password, pages: Pages, simple: PlainHtml, columns: ColumnLayout, file: fs.createReadStream(SourceFile) } }; // Send request request.post(reqOptions, function (error, response, body) { if (error) { return console.error("Error: ", error); } // Parse JSON response let data = JSON.parse(body); if (data.error == false) { // Download HTML file var file = fs.createWriteStream(DestinationFile); https.get(data.url, (response2) => { response2.pipe(file) .on("close", () => { console.log(`Generated HTML file saved as "${DestinationFile}" file.`); }); }); } else { // Service reported error console.log("Error: " + data.message); } });

package.json

      
{ "name": "test", "version": "1.0.0", "description": "PDF.co", "main": "app.js", "scripts": { }, "keywords": [ "pdf.co", "web", "api", "bytescout", "api" ], "author": "ByteScout & PDF.co", "license": "ISC", "dependencies": { "request": "^2.88.2" } }

Output

At the start of this program, we’re referencing the package dependencies such as https, path, fs, as well as requesting the external package dependecies (request)

Next logical step in the program is to prepare all the necessary request data for the PDF.co endpoint. We’re providing placeholders for the input source file (SourceFile), Pages which we’re considering converting to HTML (Pages), Destination file location (DestinationFile), etc. We’re also specifying the additional parameters like PlainHtml that are useful when we want to output HTML without any stylesheet(CSS), ColumnLayout when we have the input PDF in the column structure, etc.

Now that all the data preparations are done, we’re ready to invoke the PDF.co request using /v1/pdf/convert/to/html endpoint. All input data are passed in as form data collection. In order to pass the source PDF, we’re creating the data stream and passing it to the file parameter. The PDF.co API key x-api-key is passed in the header for the request authentication purposes.

In the request output, we have the parameter named URL that contains the link to downlaod a generated HTML document as shown in the above output image.

PDF.co endpoint for HTML conversion provides many parameters by which we can achive response as per our requirement. Some of the request parameters by which we can do the additional settings are as below.

rect This parameter is usful when we want to convert only a certain region of PDF to HTML. We can specify the region co-ordinates in the input such as “51.8, 114.8, 235.5, 204.0”. PDF.co PDF Viewer is useful to easily select and copy the coordinates.
lang This parameter sets the OCR language to be used for scanned PDF, PNG or JPG documents. By default eng is language of choice. Other supported language values are spa, deu, fra, jpn, chi_sim, chi_tra, kor.
async When this parameter is enabled, HTML conversion will run in the asynchronous mode. The output will return Job Number and its status can be checked by running the endpoint /job/check.

Please refer to the PDF.co API documentation at here for more details.

Please try this code in your machine to get the most out of this sample. Thank you for reading!

VIDEO

ON-PREMISE OFFLINE SDK

Get 60 Day Free Trial

See also:

ON-DEMAND REST WEB API

Get Your API Key

See also:

Related Pages:

Related Samples: