How to Merge PDF Files with JavaScript in NodeJS
Why Use Merge PDFs with JavaScript?
The pdfRest Merge PDFs API Tool allows users to combine multiple PDF files into a single document. This can be incredibly useful in various scenarios, such as consolidating reports, combining scanned documents, or merging chapters of a book.
In this tutorial, we will demonstrate how to send an API call to the Merge PDFs tool using JavaScript.
JavaScript Code Sample
// This request demonstrates how to merge multiple PDFs into a single document. var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs'); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('pages[]', '1-last'); data.append('type[]', 'file'); data.append('file', fs.createReadStream('/path/to/file')); data.append('pages[]', '1-last'); data.append('type[]', 'file'); data.append('output', 'pdfrest_merged_pdf'); // define configuration options for axios request var config = { method: 'post', maxBodyLength: Infinity, // set maximum length of the request body url: 'https://api.pdfrest.com/merged-pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key ...data.getHeaders() // set headers for the request }, data : data // set the data to be sent with the request }; // send request and handle response or error axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Source: pdf-rest-api-samples
A Breakdown of the .js
The code uses the Axios library to make HTTP requests and the FormData object to construct a multipart/form-data payload. Here's a breakdown of the key parts:
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
This imports the necessary modules: Axios for HTTP requests, FormData for building form data, and fs for file system operations.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('pages[]', '1-last'); data.append('type[]', 'file'); ... data.append('output', 'pdfrest_merged_pdf');
FormData is used to append files and parameters. 'file' is the PDF to be merged, 'pages[]' specifies the pages to include, 'type[]' indicates the type of the appended item, and 'output' is the name of the output file.
var config = { ... headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key ...data.getHeaders() // set headers for the request }, ... };
The configuration object for Axios includes the method, URL, headers (including the API key), and data. The API key must be replaced with your own to authenticate the request.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
The Axios instance sends the request with the given configuration. The response is then logged to the console, or an error is logged if the request fails.
Next Steps with pdfRest
We've covered how to use JavaScript to call the pdfRest Merge PDFs API. This allows you to programmatically merge multiple PDF files into a single document. You can now experiment with all of the pdfRest API Tools in the API Lab, and refer to the API Reference documentation for more details.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples.