How to Convert PDF to PDF/A with JavaScript in NodeJS

Learn how to convert standard PDF to PDF/A conformant files using pdfRest Convert to PDF/A API Tool with JavaScript.
Share this page

Why Use Convert to PDF/A with JavaScript?

The pdfRest Convert to PDF/A API Tool allows users to convert documents to the PDF/A format, which is an ISO-standardized version of PDF specialized for digital preservation of electronic documents. This tutorial will demonstrate how to send an API call to Convert to PDF/A using JavaScript.

A user might use Convert to PDF/A to ensure long-term archiving of legal documents, where the PDF/A format is often required for its ability to preserve the visual appearance over time.

Convert to PDF/A with JavaScript Code Example

// This request demonstrates how to convert any of several formats to PDF/A. 
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data object and append the PDF file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output_type', 'PDF/A-2b'); 
data.append('rasterize_if_errors_encountered', 'off');
data.append('output', 'pdfrest_pdfa');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/pdfa', 
  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.

Code source: GitHub

Breaking Down the Code

The code above demonstrates how to use the pdfRest API to convert a document to PDF/A format using JavaScript. Let's break down the code into its core components:

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

This section imports the required modules: axios for making HTTP requests, FormData for constructing form data payloads, and fs for file system operations.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output_type', 'PDF/A-2b');
data.append('rasterize_if_errors_encountered', 'off');
data.append('output', 'pdfrest_pdfa');

Here, we create a new FormData object and append the file to be converted and API parameters. The output_type specifies the PDF/A conformance level, and rasterize_if_errors_encountered determines the behavior if conversion errors are encountered.

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

This snippet sets up the configuration for the axios request, including the API endpoint URL, HTTP method, headers (including the API key), and the form data payload.

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

The final part sends the request and handles the response or error. The response data is logged to the console in JSON format.

Beyond the Tutorial

In this tutorial, we've learned how to use JavaScript to make an API call to pdfRest's Convert to PDF/A endpoint. By following the steps outlined, you can convert documents to a format suitable for long-term archiving.

Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for more details.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

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