How to Validate a PDF Document in Node.js
Document validation steps are critical for maintaining secure, high-quality data, and they’re also great for triggering conditional actions in our automated workflows.
Using the below code, we can easily take advantage of a free API that validates PDF documents. This will 1) provide a Boolean response for document validity; 2) enumerate any errors/warnings associated with the document — if any; 3) identify if the document has password-protection security measures in place.
To structure our API call, let’s start by installing the SDK. We can do so either by running the following command:
npm install cloudmersive-convert-api-client --save
Or by adding the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
After that, we can use the ready-to-run Node.js code examples below to structure our request. To authorize our request, we’ll need to supply a free-tier API key (this allows up to 800 API calls per month with no commitment):
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentPdfValidation(inputFile, callback);
The service will process our request in-memory and release all data upon completion. Easy!