How to Import Data to PDF Forms with JavaScript in NodeJS

Learn how to use pdfRest Import Form Data API Tool with JavaScript in NodeJS to import data into PDF forms.
Share this page

Why Use Import Form Data with JavaScript?

The pdfRest Import Form Data API Tool is a powerful resource for developers looking to automate the process of populating PDF forms with data. This tool enables you to send an API call to import data into a PDF form programmatically, which can be incredibly useful for generating customized documents on-the-fly.

In this tutorial, we will demonstrate how to make an API call to the Import Form Data API Tool using JavaScript to utilize this functionality.

Imagine you are managing a large number of customer contracts, and each contract needs to be filled out with specific customer details. Manually entering this data can be time-consuming and prone to errors. By using the Import Form Data API, you can automate the process by importing data from a structured data file directly into your PDF forms, ensuring accuracy and efficiency in document generation.

Import Form Data with JavaScript Code Example

/**
 * This request demonstrates how to import data from a data file into a PDF with forms.
 */
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("data_file", fs.createReadStream("/path/to/datafile"));
data.append("output", "pdfrest_pdf_with_imported_form_data");

// 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-imported-form-data",
  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: The code provided is sourced from pdf-rest-api-samples on GitHub.

Breaking Down the Code

The code example above demonstrates how to use Node.js, along with the Axios library, to make a multipart POST request to the pdfRest API for importing form data into a PDF.

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

This snippet includes the necessary modules: Axios for making HTTP requests, FormData to construct multipart/form-data payloads, and fs to interact with the file system.

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

Here, a new FormData instance is created, and the PDF file, data file, and output name are appended. The files are read from the specified paths and included in the request payload.

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

The configuration object for the Axios request is defined, setting the request 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);
  });

The Axios library is used to send the request with the provided configuration. The response is then logged to the console or, in case of an error, the error information is logged.

Beyond the Tutorial

In this tutorial, we've successfully demonstrated how to call the pdfRest API to import form data into a PDF using JavaScript. This process can be integrated into various applications to automate document workflows and increase productivity.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For a comprehensive understanding of the available endpoints and their functionalities, refer to the API Reference Guide.

Note: This is an example of a multipart API call. For code samples using JSON payloads, you can find them at pdf-rest-api-samples on GitHub.

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