How to split PDF from file (node for PDF splitting API in JavaScript with Web API

How to split PDF from file (node for PDF splitting API in JavaScript: How To Tutorial

Writing of the code to split PDF from file (node in JavaScript can be done by developers of any level using Web API. PDF splitting API in JavaScript can be implemented with Web API. Web API is the Rest API that provides set of data extraction functions, tools for documents manipulation, splitting and merging of pdf files. Includes built-in OCR, images recognition, can generate and read barcodes from images, scans and pdf.

Fast application programming interfaces of Web API for JavaScript plus the instruction and the code below will help to learn how to split PDF from file (node. Open your JavaScript project and simply copy & paste the code and then run your app! You can use these JavaScript sample examples in one or many applications.

ByteScout free trial version is available for FREE download from our website. Programming tutorials along with source code samples are included.

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

/*jshint esversion: 6 */ var https = require("https"); 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 const API_KEY = "***********************************"; // Source PDF file to split const SourceFile = "./sample.pdf"; // Comma-separated list of page numbers (or ranges) to process. Example: '1,3-5,7-'. const Pages = "1-2,3-"; // Prepare request to `Split PDF Pages` API endpoint var query = ``; let reqOptions = { uri: query, headers: { "x-api-key": API_KEY }, formData: { pages: Pages, async: 'True', file: fs.createReadStream(SourceFile) }, }; // Send request, 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, resultFileUrlJson) { let queryPath = `/v1/job/check?jobid=${jobId}`; let reqOptions = { host: "", 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, resultFileUrlJson) }, 3000); } else if (data.status == "success") { request({ method: 'GET', uri: resultFileUrlJson, gzip: true }, function (error, response, body) { // Parse JSON response let respJsonFileArray = JSON.parse(body); let part = 1; respJsonFileArray.forEach((url) => { var localFileName = `./part${part}.pdf`; var file = fs.createWriteStream(localFileName); https.get(url, (response2) => { response2.pipe(file) .on("close", () => { console.log(`Generated PDF file saved as "${localFileName} file."`); }); }); part++; }, this); }); } else { console.log(`Operation ended with status: "${data.status}".`); } }) }); }

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



Get 60 Day Free Trial or Visit Web API Home Page

Explore Web API Documentation

Get Web API Free Training


Get Your Free API Key

Explore Web API Documentation

Related Samples: