How to copy Pages from one PDF document to another in Node.JS

Transfering pages between PDFs should be a walk in the park? Well, that’s not normally the case. There’s actually quite a bit of annoying code standing between you and this type of functionality. That’s why today we will be using a handy little API to get the job done so you can get back to walking in the park, or whatever.

We can use npm install to add the API client to our project.

npm install cloudmersive-convert-api-client --save

The next step is to call editPdfInsertPages, as shown here.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();var sourceFile = "/path/to/file"; // File | Source PDF file to copy pages from.var destinationFile = "/path/to/file"; // File | Destination PDF file to copy pages into.var pageStartSource = 56; // Number | Page number (1 based) to start copying pages from (inclusive) in the Source file.var pageEndSource = 56; // Number | Page number (1 based) to stop copying pages pages from (inclusive) in the Source file.var pageInsertBeforeDesitnation = 56; // Number | Page number (1 based) to insert the pages before in the Destination file.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editPdfInsertPages(sourceFile, destinationFile, pageStartSource, pageEndSource, pageInsertBeforeDesitnation, callback);

Now we just need to punch in our source and destination files, as well as our parameters, such as page range to copy and where to paste it. And just like that, we have our solution set up.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.