How to convert PDF to JSON from file (node for PDF to JSON API in JavaScript using PDF.co Web API

See how to convert PDF to JSON from file (node to have PDF to JSON API in JavaScript

Today you are going to learn how to convert PDF to JSON from file (node in JavaScript. PDF to JSON API in JavaScript can be implemented with PDF.co Web API. PDF.co Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

The SDK samples like this one below explain how to quickly make your application do PDF to JSON API in JavaScript with the help of PDF.co Web API. For implimentation of this functionality, please copy and paste code below into your app using code editor. Then compile and run your app. Writing JavaScript application typically includes multiple stages of the software development so even if the functionality works please test it with your data and the production environment.

Trial version of ByteScout is available for free download from our website. This and other source code samples for JavaScript and other programming languages are available.

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

ConvertPdfToJsonFromFile_AsyncApi.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 JSON file name const DestinationFile = "./result.json"; // Prepare request uri for var query = `https://api.pdf.co/v1/pdf/convert/to/json`; let reqOptions = { uri: query, headers: { "x-api-key": API_KEY }, formData: { name: path.basename(DestinationFile), password: Password, pages: Pages, async: 'True', 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); console.log(`Job #${data.jobId} has been created!`); checkIfJobIsCompleted(data.jobId, data.url); }); function checkIfJobIsCompleted(jobId, resultFileUrl) { let queryPath = `/v1/job/check?jobid=${jobId}`; let reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), method: "GET", headers: { "x-api-key": API_KEY } }; https.get(reqOptions, (response) => { response.on("data", (d) => { response.setEncoding("utf8"); // Parse JSON response let data = JSON.parse(d); console.log(`Checking Job #${jobId}, Status: ${data.status}, Time: ${new Date().toLocaleString()}`); if (data.status == "working") { // Check again after 3 seconds setTimeout(function(){ checkIfJobIsCompleted(jobId, resultFileUrl);}, 3000); } else if (data.status == "success") { // Download JSON file var file = fs.createWriteStream(DestinationFile); https.get(resultFileUrl, (response2) => { response2.pipe(file) .on("close", () => { console.log(`Generated JSON file saved as "${DestinationFile}" file.`); }); }); } else { console.log(`Operation ended with status: "${data.status}".`); } }) }); }

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

VIDEO

ON-PREMISE VERSION INFORMATION

Get 60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Get PDF.co Web API Free Training

WEB API

Get Your Free API Key

Explore Web API Documentation

Related Samples: