How to Export PDF Form Data with JavaScript
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 export PDF form data as XML.
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
Breaking Down the JavaScript Code
The NodeJS sample uses axios for HTTP, form-data for multipart construction, and the built-in fs module for the input stream. Install the external dependencies with npm install axios form-data when adapting this file outside the samples project.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
These modules let the request stream a PDF rather than loading the entire file into a JSON object or encoding it as text.
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('data_format', 'xml');
data.append('output', 'pdfrest_exported_form_data');
file is the fillable PDF. data_format is required and must match the form technology: AcroForms support fdf, xfdf, and xml, while XFA forms support xdp, xfd, and xml. If the form type is unknown, Query PDF can check contains_acroforms and contains_xfa. output names the exported data file without its extension.
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
};
data.getHeaders() contributes the multipart content type and boundary generated by form-data. Spreading those headers into the Axios configuration is important; manually setting only multipart/form-data would omit the boundary that separates the fields.
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
The success handler prints the generated resource information. The error handler exposes transport or API failures; production code can inspect error.response to distinguish an HTTP response from a client-side failure.
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://docs.pdfrest.com/pdfrest-api-toolkit-cloud/api-reference-guide/ 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.