How to merge multiple PDF Documents together in Node.JS

Here’s a quick tutorial on merging numerous PDF files into a single PDF. Getting this done in Node.JS has never been easier, as I am about to demonstrate.

With this dependencies reference in our package.json file, we can install our client without any hassle.

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

From there, call mergeDocumentPdfMulti.

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.MergeDocumentApi();var inputFile1 = "/path/to/file"; // File | First input file to perform the operation on.var inputFile2 = "/path/to/file"; // File | Second input file to perform the operation on.var opts = {'inputFile3': "/path/to/file", // File | Third input file to perform the operation on.'inputFile4': "/path/to/file", // File | Fourth input file to perform the operation on.'inputFile5': "/path/to/file", // File | Fifth input file to perform the operation on.'inputFile6': "/path/to/file", // File | Sixth input file to perform the operation on.'inputFile7': "/path/to/file", // File | Seventh input file to perform the operation on.'inputFile8': "/path/to/file", // File | Eighth input file to perform the operation on.'inputFile9': "/path/to/file", // File | Ninth input file to perform the operation on.'inputFile10': "/path/to/file" // File | Tenth input file to perform the operation on.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.mergeDocumentPdfMulti(inputFile1, inputFile2, opts, callback);

As you can see, you may input as many as 10 files at a time. The function can then be repeated with the output as inputFile1, allowing you to daisy-chain together as many PDFs as you wish. Not bad.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store