How to Export PDF Form Data with JavaScript

Share this page

Why Export PDF Form Data with JavaScript?

The pdfRest Export Form Data API Tool allows users to extract form data from a PDF and convert it into a specified format such as XML, FDF, or XFDF. This tutorial will guide you through the process of sending an API call to Export Form Data using JavaScript. This can be particularly useful when you need to programmatically extract form data from a large number of PDFs for data analysis or migration to another system.

JavaScript Code Example

// This request demonstrates how to apply 256-bit AES encryption with a 32-bit key. Encrypted PDFs cannot be viewed without first providing the open password.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('data_format', 'xml');
data.append('output', 'pdfrest_exported_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/exported-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.

Source: pdf-rest-api-samples on GitHub

Explaining .js Code

The code begins by requiring necessary modules:

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

Axios is used for making HTTP requests, FormData is used for building multipart/form-data payloads, and the fs module is used to read files from the file system.

Next, a new FormData instance is created and populated with the file and parameters:

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('data_format', 'xml');
data.append('output', 'pdfrest_exported_form_data');

The 'file' field should contain the path to the PDF file. The 'data_format' field specifies the format for the exported data (e.g., 'xml'). The 'output' field names the output file.

The Axios request configuration is defined:

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

The 'Api-Key' header must be replaced with your actual API key from pdfRest. The data object is attached to the request.

Finally, the request is sent and the response or error is handled:

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

Next Steps with pdfRest

This tutorial explained how to use JavaScript to call the pdfRest Export Form Data API to extract data from a PDF file. By following the steps outlined, you can programmatically export form data from PDFs into various formats.

Feel free 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/ for more details.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found 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.