verify pdf files in JS (supports both node.js & browser).
The signed PDF file has the public certificate embedded in it, so all we need to verify a PDF file is the file itself.
npm i @ninja-labs/verify-pdf
// CommonJS require
const verifyPDF = require('@ninja-labs/verify-pdf');
// ES6 imports
import verifyPDF from '@ninja-labs/verify-pdf';
Verify the digital signature of the pdf and extract the certificates details
const verifyPDF = require('@ninja-labs/verify-pdf');
const signedPdfBuffer = fs.readFileSync('yourPdf');
const {
verified,
authenticity,
integrity,
expired,
signatures
} = verifyPDF(signedPdfBuffer);
import verifyPDF from '@ninja-labs/verify-pdf';
const readFile = (e) => {
const file = e.target.files[0]
let reader = new FileReader();
reader.onload = function(e) {
const { verified } = verifyPDF(reader.result);
}
reader.readAsArrayBuffer(file);
};
You can get the details of the certificate chain by using the following api.
const { getCertificatesInfoFromPDF } = require('@ninja-labs/verify-pdf'); // require
import { getCertificatesInfoFromPDF } from '@ninja-labs/verify-pdf'; // ES6
const certs = getCertificatesInfoFromPDF(signedPdfBuffer);
signedPdfBuffer: signed PDF as buffer.
certs:
node-forge is used for working with signatures.