How to Merge PDF Files using Node.js

Cloudmersive
2 min readDec 29, 2022

--

Storing PDF files separately creates a lot of unnecessary clutter in your file storage system, especially if those files’ contents are related to one another. Thankfully, the complementary, ready-to-run code examples below code will make it easy to programmatically merge PDF documents, ensuring you won’t waste time pointing and clicking your way through a PDF reader. You can use this code to call our “Merge Two PDF Files Together” API, which requires a very simple set of request parameters. These include the following:

  1. inputFile1 — the first PDF file to perform the operation on (this file will stack first in your resulting merged PDF)
  2. inputFile2 — the second PDF file to perform the operation on (this file will stack second in your resulting merged PDF)
  3. Cloudmersive API Key — this can be obtained for free on our website (free accounts provide a limit of 800 API Calls per month with no commitments)

To begin your API call, first run the below command to install the Node.js SDK:

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

Or add this snippet to your package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Lastly, use the final code snippet below to call the function, and include your prepared input request parameters in their respective fields:

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.MergeDocumentApi();

var inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.

var inputFile2 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Second input file to perform the operation on (more than 2 can be supplied).


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.mergeDocumentPdf(inputFile1, inputFile2, callback);

All done! Yep, it’s that easy.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet