How to Digitally Sign PDF Files with JavaScript in NodeJS

Learn how to use pdfRest's Sign PDF API Tool with JavaScript to digitally sign PDF files.
Share this page

Why Digitally Sign PDFs with JavaScript?

The pdfRest Sign PDF API Tool is an efficient solution for digitally signing PDF documents using JavaScript. This tutorial will guide you through the process of sending an API call to the Sign PDF endpoint using JavaScript. By leveraging this tool, you can automate the process of applying digital signatures to your PDF files, ensuring authenticity and integrity.

An example of using the Sign PDF tool might involve a company that needs to sign contracts or official documents digitally. By using this API, they can streamline their workflow, reduce the need for manual signatures, and maintain a secure and verifiable record of signed documents. This is particularly useful for businesses that manage a large volume of documents and require a reliable method of authentication.

Sign PDF with JavaScript Code Example

/**
 * This request demonstrates how to apply a digital signature to a PDF using PFX credentials.
 */
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("pfx_credential_file", fs.createReadStream("/path/to/credentials.pfx"));
data.append("pfx_passphrase_file", fs.createReadStream("/path/to/passphrase.txt"));
data.append("logo_file", fs.createReadStream("/path/to/logo.png"));
const signature_config = {
    type: "new",
    name: "esignature",
    logo_opacity: "0.5",
    location: {
        bottom_left: { x: "0", y: "0" },
        top_right: { x: "216", y: "72" },
        page: 1
    },
    display: {
        include_distinguished_name: "true",
        include_datetime: "true",
        contact: "My contact information",
        location: "My signing location",
        name: "John Doe",
        reason: "My reason for signing"
    }
};
data.append("signature_configuration", JSON.stringify(signature_config));
data.append("output", "example_out");

var signed_pdf_config = {
    method: "post",
    maxBodyLength: Infinity,
    url: "https://api.pdfrest.com/signed-pdf",
    headers: {
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
        ...data.getHeaders(), // set headers for the request
    },
    data: data,
};

// send request and handle response or error
axios(signed_pdf_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: GitHub Repository

Breaking Down the Code

The code begins by importing necessary modules: axios for making HTTP requests, form-data for handling form submissions, and fs for file system operations.

var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));

This snippet creates a new FormData instance and appends the PDF file to be signed. The path to the file is specified using fs.createReadStream().

data.append("pfx_credential_file", fs.createReadStream("/path/to/credentials.pfx"));
data.append("pfx_passphrase_file", fs.createReadStream("/path/to/passphrase.txt"));

These lines append the PFX credential file and passphrase file, which are necessary for the digital signature process. The PFX file contains the certificate and private key, while the passphrase file secures it.

const signature_config = {
    type: "new",
    name: "esignature",
    ...
};
data.append("signature_configuration", JSON.stringify(signature_config));

The signature_config object defines the signature's appearance and placement. It includes parameters like type, name, logo_opacity, and location. This configuration is converted to a JSON string and appended to the form data.

var signed_pdf_config = {
    method: "post",
    ...
    url: "https://api.pdfrest.com/signed-pdf",
    headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ...data.getHeaders() },
    data: data,
};

The signed_pdf_config object sets up the API request, specifying the HTTP method, URL, headers (including the API key), and the form data. Ensure to replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

Beyond the Tutorial

In this tutorial, you learned how to apply a digital signature to a PDF using the pdfRest Sign PDF API Tool with JavaScript. This process involves setting up the necessary files and configurations, and sending an API request to the pdfRest endpoint.

To explore more, try demoing all of the pdfRest API Tools in the API Lab. For further details, refer to the API Reference Guide. Note that this is an example of a multipart API call, and code samples using JSON payloads can be found here.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.