How to Validate PDF/A Conformance with JavaScript in NodeJS

Learn how to validate PDF/A conformance with pdfRest Query PDF API Tool using JavaScript
Share this page

Why Validate PDF/A with JavaScript?

The pdfRest Query PDF API Tool is a powerful resource for developers looking to extract detailed information from PDF documents programmatically. This API provides a means to retrieve metadata, structure, and content details, which can be essential for document management systems, content analysis, and automated workflows. This tutorial will guide you through the process of sending an API call to Query PDF using JavaScript to perform a PDF/A validation check.

Many businesses operate within legal and regulatory frameworks that mandate specific document retention standards. PDF/A conformance validation plays a vital role in ensuring your digital documents meet these requirements. By validating your PDFs as PDF/A, you're creating tamper-proof archives that comply with industry regulations and legal mandates. This helps organizations avoid potential penalties and legal risks associated with non-compliant document storage. Furthermore, PDF/A validation guarantees the long-term accessibility and searchability of electronic documents, facilitating efficient information retrieval for audits or legal proceedings. In today's compliance-driven environment, PDF/A validation offers a reliable and secure solution for managing your critical business documents.

Validate PDF/A with JavaScript Code Example

// This request demonstrates how to get detailed information about a PDF document and its contents to assess the current state of the file and drive conditional processing.
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('queries', '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/pdf-info', 
  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 code above is sourced from the GitHub repository at pdf-rest-api-samples.

Breaking Down the Code

The code sample provided demonstrates how to use the pdfRest API to query information about a PDF file. Here's a breakdown of the key parts of the code:

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

These lines import the necessary 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'));

Here, a new FormData object is instantiated, and the PDF file is appended to it as a readable stream. The file path should be replaced with the actual path to your PDF file.

data.append('queries', 'pdfa');

This line appends queries that specify the information you want to retrieve about the PDF file. Each query corresponds to a specific attribute or feature of the PDF, such as whether it's tagged, contains images only, and so on. In this case, we are only including "pdfa" to run a PDF/A validation check.

var config = {
  method: 'post',
  ...
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

The config object contains the configuration for the axios request, including the HTTP method, the API endpoint URL, headers (including the API key which should be replaced with your own), and the data payload.

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

Finally, the axios request is sent, and the response is handled. If successful, the response data is logged to the console. If there is an error, it is logged as well.

Beyond the Tutorial

In this tutorial, we've learned how to use JavaScript to make a multipart API call to the pdfRest Query PDF endpoint. This enables us to programmatically retrieve a wealth of information about a PDF document, such as confirming whether or not it is a valid PDF/A document. Such capabilities are invaluable for automating document processing and analysis tasks.

To explore more possibilities and to demo all of the pdfRest API Tools, visit the API Lab. For a deeper understanding of the API and its capabilities, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at the pdf-rest-api-samples repository.

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