How to Remove Pages from a PDF Document in Node.js
Specialized APIs for PDF editing/processing make it drastically more efficient to build custom applications that adequately handle our most important documents.
We can use the below ready-to-run Node.js code examples to take advantage of a free API with custom parameters for removing pages from our PDF documents. All we need to do is enter our document and provide starting/ending pages for our request, and we’ll wrap up our task in seconds.
Structuring our API call can be accomplished in three quick steps, starting with SDK installation. We can either run the following command to install the SDK:
npm install cloudmersive-convert-api-client --save
Or we can add the below snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
After that, we’ll need to retrieve a free-tier Cloudmersive API key, which will allow us to make up to 800 API requests per month with zero additional commitment.
Finally, we can copy the below code into our file to structure our request:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var pageStart = 56; // Number | Page number (1 based) to start deleting pages from (inclusive).
var pageEnd = 56; // Number | Page number (1 based) to stop deleting pages from (inclusive).
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfDeletePages(inputFile, pageStart, pageEnd, callback);
And we’re all set!