How to Check PDF Conditions and Metadata with JavaScript in NodeJS
Why Use Query PDF with JavaScript?
The pdfRest Query PDF API Tool is designed to extract detailed information about a PDF document, such as metadata, page count, whether it contains annotations, signatures, JavaScript, and much more. This tutorial will show you how to send an API call to Query PDF using JavaScript.
This is particularly useful, for instance, when you need to automate the process of validating PDF files for certain criteria before they are processed or distributed in a system.
Query PDF 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', 'tagged,image_only,title,subject,author,producer,creator,creation_date,modified_date,keywords,doc_language,page_count,contains_annotations,contains_signature,pdf_version,file_size,filename,restrict_permissions_set,contains_xfa,contains_acroforms,contains_javascript,contains_transparency,contains_embedded_file,uses_embedded_fonts,uses_nonembedded_fonts,pdfa,requires_password_to_open'); // 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.
Source: pdf-rest-api-samples
Breaking Down the Code
The code starts by requiring necessary modules: axios for making HTTP requests, FormData for constructing form data to send with the request, and fs for file system operations.
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
Next, it creates a new FormData instance and appends the PDF file and a string of queries to it. Each query specifies a piece of information you want to retrieve from the PDF.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('queries', '...');
The configuration object for the axios request is defined with the HTTP method, URL, headers (including the API key), and the data payload.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/pdf-info', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
Finally, the axios request is sent, and the response or error is handled accordingly.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Beyond the Tutorial
By following the steps above, you have learned how to use JavaScript to call the pdfRest Query PDF API to retrieve detailed information about a PDF document. This can be the foundation for more complex workflows involving PDF processing and analysis.
Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples.