How to Add Attachments to PDF Files with JavaScript in NodeJS

Learn how to use JavaScript to call the Add to PDF API tool by pdfRest.
Share this page

Why Add Attachments to PDF with JavaScript?

The pdfRest Add to PDF API Tool is a powerful resource for developers who need to manipulate PDF files programmatically. This tool allows you to add file attachments to PDF documents. In this tutorial, we will learn how to send an API call to Add to PDF using JavaScript.

This functionality is particularly useful in scenarios where you need to bundle non-PDF files with a PDF document, such as attaching source data files to a report or including supplementary materials with an eBook.

Add Attachments to PDF with JavaScript Code Example

/**
 * This request demonstrates how to add a file attachment to a PDF.
 */
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('file_to_attach', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_pdf_with_added_attachment');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/pdf-with-added-attachment', 
  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.

Reference: GitHub Repository

Breaking Down the Code

The code provided demonstrates how to use JavaScript to make an API call to the pdfRest service in order to add a file attachment to a PDF document.

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

This snippet includes the required modules: 'axios' for making HTTP requests, 'form-data' for constructing multipart/form-data payloads, and 'fs' for file system operations.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('file_to_attach', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_pdf_with_added_attachment');

This creates a new FormData object and appends the PDF file, the file to attach, and the desired output name.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/pdf-with-added-attachment',
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

This configures the axios request with the HTTP method, URL, headers (including the API key), and the data payload.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

This sends the request and logs the response data or catches any errors that occur during the request.

Beyond the Tutorial

In this tutorial, we have successfully made an API call to add a file attachment to a PDF document using JavaScript. This is a fundamental example of how to interact with the pdfRest API to enhance and modify PDF documents programmatically. Users are encouraged to demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us