How to Merge PDF Files in JavaScript

Cloudmersive
1 min readApr 6, 2021

Merging PDF files into a single document can be a great way to consolidate similar types of content and avoid the mess of delving through and storing several files. The following API can be run in JavaScript to instantly perform the merge function while preserving the order of the input documents (up to ten) in the combined output document.

To use the API, we can run this command to install the jQuery library:

bower install jquery

Next, we’re ready to call the function with the following code:

var form = new FormData();
form.append("inputFile1", fileInput.files[0], "file");
form.append("inputFile2", fileInput.files[0], "file");
form.append("inputFile3", fileInput.files[0], "file");
form.append("inputFile4", fileInput.files[0], "file");
form.append("inputFile5", fileInput.files[0], "file");
form.append("inputFile6", fileInput.files[0], "file");
form.append("inputFile7", fileInput.files[0], "file");
form.append("inputFile8", fileInput.files[0], "file");
form.append("inputFile9", fileInput.files[0], "file");
form.append("inputFile10", fileInput.files[0], "file");
var settings = {
"url": "https://api.cloudmersive.com/convert/merge/pdf/multi",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
});

The end result will be a single PDF file containing all the content and formatting from the input files. You can retrieve your free API key from the Cloudmersive website; this will give you access 800 monthly calls across our entire library of APIs.

--

--

Cloudmersive

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