How to Flatten PDF Forms with JavaScript in NodeJS

Learn how to flatten PDF forms with pdfRest Flatten Forms API Tool using JavaScript
Share this page

Why Flatten PDF Forms with JavaScript?

The pdfRest Flatten Forms API Tool is a powerful resource for developers working with PDF forms. Flattening a PDF form means converting the editable form fields into static content, which ensures that the entered data cannot be altered. This tutorial will guide you through the process of sending an API call to the Flatten Forms API using JavaScript, which can be particularly useful when you need to programmatically process PDFs within a web application or a server-side script.

Consider a scenario where you're running a business that collects customer feedback through PDF forms. Once a customer fills out the form, you might want to secure the information by flattening the form before archiving it or sending it to another department. By using the Flatten Forms API with JavaScript, you can automate this process, ensuring that the data remains intact and unchangeable, thus maintaining data integrity and preventing any post-submission modifications.

Flatten PDF Forms with JavaScript Code Example

/**
 * This request demonstrates how to flatten forms in 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("output", "pdfrest_flattened_forms_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/flattened-forms-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.

The provided code can be found in the pdfRest API samples repository on GitHub.

Breaking Down the Code

The code begins by importing the necessary modules: axios for HTTP requests, FormData for constructing multipart/form-data payloads, and fs for file system operations.

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

A new FormData instance is created and the PDF file along with output parameter are appended to it. The file path should be replaced with the actual path to your PDF file.

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

The configuration object for the axios request is defined with the request method, URL, headers (including the API key), and data. The API key should be replaced with your actual pdfRest API key.

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

The axios request is sent, and the response or error is handled with a promise. On success, the response data is logged to the console.

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

Beyond the Tutorial

By following this tutorial, you've learned how to send an API call to the Flatten Forms endpoint of pdfRest using JavaScript. With this knowledge, you can start integrating PDF form flattening capabilities into your applications. To explore and demo all of the pdfRest API Tools, visit the API Lab. For further information and a comprehensive understanding of the API's capabilities, refer to the API Reference Guide.

Note: This is an example of a multipart API call. For code samples using JSON payloads, you can visit the pdfRest API samples repository on GitHub.

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