Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/d0e1c39771047ed7ab5a061ad98214ca to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/d0e1c39771047ed7ab5a061ad98214ca to your computer and use it in GitHub Desktop.
Combine and Merge PDF files into One Online using Node.js
// How to Merge and Combine PDF files into One using Node.js
const combine = async () => {
let documentApi = groupdocs_merger_cloud.DocumentApi.fromKeys(clientId, clientSecret);
// create first join item
let item1 = new groupdocs_merger_cloud.JoinItem();
item1.fileInfo = new groupdocs_merger_cloud.FileInfo();
item1.fileInfo.filePath = "nodejs-testing/sample-file1.pdf";
// create second join item
let item2 = new groupdocs_merger_cloud.JoinItem();
item2.fileInfo = new groupdocs_merger_cloud.FileInfo();
item2.fileInfo.filePath = "nodejs-testing/sample-file2.pdf";
// create join options
let options = new groupdocs_merger_cloud.JoinOptions();
options.joinItems = [item1, item2];
options.outputPath = "nodejs-testing/joined-file.pdf";
try {
// Create join documents request
let joinRequest = new groupdocs_merger_cloud.JoinRequest(options);
let result = await documentApi.join(joinRequest);
}
catch (err) {
throw err;
}
}
combine()
.then(() => {
console.log("Successfully combined PDF documents: ");
})
.catch((err) => {
console.log("Error occurred while merging the PDF files:", err);
})

You can combine multiple PDF files into a single PDF file programmatically on the cloud. In this article, you will learn how to merge PDF files into One PDF file using Node.js.

The following topics shall be covered in this article:

  1. Online Document Merger REST API and Node.js SDK
  2. Merge and Combine PDF files into One using Node.js
  3. How to Merge Multiple PDF files Pages in Node.js
  4. Upload and Download PDF file using Cloud Storage
// How to Merge Multiple PDF files Pages in Node.js
const mergespecific = async () => {
// Merge PDFs api initialization
let documentApi = groupdocs_merger_cloud.DocumentApi.fromKeys(clientId, clientSecret);
// create first join item
let item1 = new groupdocs_merger_cloud.JoinItem();
item1.fileInfo = new groupdocs_merger_cloud.FileInfo();
item1.fileInfo.filePath = "nodejs-testing/sample-file1.pdf";
item1.pages = [1, 3];
// create second join item
let item2 = new groupdocs_merger_cloud.JoinItem();
item2.fileInfo = new groupdocs_merger_cloud.FileInfo();
item2.fileInfo.filePath = "nodejs-testing/sample-file2.pdf";
item2.startPageNumber = 2
item2.endPageNumber = 4
// create join options
let options = new groupdocs_merger_cloud.JoinOptions();
options.joinItems = [item1, item2];
options.outputPath = "nodejs-testing/joined-file.pdf";
try {
// Create join documents request
let joinRequest = new groupdocs_merger_cloud.JoinRequest(options);
let result = await documentApi.join(joinRequest);
}
catch (err) {
throw err;
}
}
mergespecific()
.then(() => {
console.log("Successfully merged PDF documents specific pages: ");
})
.catch((err) => {
console.log("Error occurred while combining the PDF files:", err);
})
// construct FileApi to download merged file
var fileApi = groupdocs_merger_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_merger_cloud.DownloadFileRequest("nodejs-testing/joined-file.pdf", myStorage);
// download file and response type Stream
fileApi.downloadFile(request)
.then(function (response) {
// save file in your system directory
fs.writeFile("H:\\groupdocs-cloud-data\\joined-file.pdf", response, "binary", function (err) { });
console.log("Expected response type is Stream: " + response.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
// construct FileApi
let fileApi = groupdocs_merger_cloud.FileApi.fromConfig(config);
// open file in IOStream from your system drive.
let resourcesFolder = 'H:\\groupdocs-cloud-data\\sample-pdf\\';
fs.readdir(resourcesFolder, (err, files) => {
files.forEach(file => {
// read files one by one
fs.readFile(resourcesFolder + file, (err, fileStream) => {
// create upload file request
let request = new groupdocs_merger_cloud.UploadFileRequest("nodejs-testing/" + file, fileStream, myStorage);
// upload file
fileApi.uploadFile(request)
.then(function (response) {
console.log(file + " file uploaded: " + response.uploaded.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
});
});
});
# Import Node.js SDK in your node application from http://api.groupdocs.cloud
global.groupdocs_merger_cloud = require("groupdocs-merger-cloud");
global.fs = require("fs");
// get clientId and clientSecret from https://dashboard.groupdocs.cloud (free registration is required).
global.clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
global.clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
global.myStorage = "test-internal-storage";
const config = new groupdocs_merger_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment