How to Convert PDF Forms from XFA to Acroforms with JavaScript in NodeJS

Learn how to use pdfRest API Tool to convert PDF forms from XFA to Acroforms with JavaScript
Share this page

Why Use XFA to Acroforms with JavaScript?

The pdfRest XFA to Acroforms API Tool is a powerful utility that allows developers to convert XFA (XML Forms Architecture) PDFs into Acroform PDFs. This tutorial will guide you through the process of sending an API call to the XFA to Acroforms endpoint using JavaScript. By following this tutorial, you will learn how to use the provided code to make a successful API call and understand each part of the code.

A user might need to convert XFA PDFs to Acroforms to ensure compatibility with a broader range of PDF viewers and editors. For instance, many older PDF forms created with XFA may not be fully supported by modern PDF readers. Converting these forms to Acroforms can help ensure that they are accessible and functional across various platforms and devices.

XFA to Acroforms with JavaScript Code Example

// This request demonstrates how to convert a XFA PDF to an acroform file.
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_acroform_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/pdf-with-acroforms",
  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: GitHub Repository

Breaking Down the Code

Let's break down the code step by step to understand how it works:

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

This section imports the necessary modules: axios for making HTTP requests, form-data for handling form data, and fs for interacting with the file system.

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

Here, a new instance of FormData is created, and the PDF file to be converted is appended to it using fs.createReadStream("/path/to/file"). The output parameter is set to pdfrest_acroform_pdf, specifying the desired output format.

var config = {
  method: "post",
  maxBodyLength: Infinity, // set maximum length of the request body
  url: "https://api.pdfrest.com/pdf-with-acroforms",
  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
};

This block defines the configuration for the axios request. The method is set to post, and maxBodyLength is set to Infinity to allow for large files. The url specifies the endpoint for the API call. The headers include the API key (replace the placeholder with your actual API key) and the headers generated by data.getHeaders(). The data field includes the form data to be sent with the request.

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

This section sends the request using axios and handles the response or any errors. If the request is successful, the response data is logged to the console. If an error occurs, it is logged to the console.

Beyond the Tutorial

In this tutorial, we demonstrated how to convert an XFA PDF to an Acroform PDF using JavaScript and the pdfRest API. By following the steps and understanding the code, you can now integrate this functionality into your own applications.

We encourage you to explore all the pdfRest API Tools available in the API Lab. For detailed information on each API endpoint, refer to the API Reference Guide. Note that this example demonstrates a multipart API call, and code samples using JSON payloads can be found in 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