Digital signatures
Sign PDFs with PAdES using a PKCS#12 file or an external signer such as an HSM, smart card or cloud KMS, and verify signatures, with NexPDF for Node.js.
License module
Signing and verification (with or without your own roots) need the signatures module (Business
tier and above).
Every signing call returns the complete signed file as a Uint8Array. Write those bytes as they
are: saving the document object again afterwards breaks the signature.
Sign with a PKCS#12 file
import { readFile, writeFile } from 'node:fs/promises';
import { PdfDocument, pkcs12Info } from '@nexdevit/nexpdf-node';
const doc = await PdfDocument.open('contract.pdf');
const p12 = await readFile('signer.p12');
console.log(pkcs12Info(p12, 'p12-password')); // the certificate
const signed = doc.sign(p12, 'p12-password', {
reason: 'Approved',
location: 'Lisbon',
appearance: { page: 0, rect: [360, 60, 560, 120], text: 'Signed by Ana Silva' },
});
await writeFile('contract-signed.pdf', signed);Options: name, reason, location, contact, field (an existing signature field), level
(B-B, B-T, B-LT or B-LTA, see
Long-term signatures), timestamp (the B-LTA document
timestamp's hash, field and reserved) and appearance (page, rect, ink strokes,
text). A wrong password throws Error('incorrect password').
sign and addLtv make no network calls, so sign works at level: 'B-B'. For PAdES B-T, B-LT
and B-LTA use await doc.signOnline(p12, password, { level, tsaUrl }), which gets the timestamps and
the OCSP and CRL data with fetch; see Long-term signatures.
For tests, doc.signTest({ signer, reason, location }) signs with a new self-signed certificate
(PAdES B-B).
External signers
When the private key is in an HSM, a smart card, a cloud KMS or a remote signing service, sign in three steps that need no state in between:
import { completeExternalSignature, externalSignatureData } from '@nexdevit/nexpdf-node';
const cert = await readFile('signer-chain.pem'); // PEM or DER, chain optional
const prepared = doc.prepareExternalSignature(cert, { name: 'Ana Silva', reason: 'Approval' });
const toSign = externalSignatureData(prepared, cert); // DER signed attributes
const signature = await kms.sign(toSign); // your call: SHA-256 with RSA or ECDSA
await writeFile('signed.pdf', completeExternalSignature(prepared, cert, signature, { algorithm: 'ecdsa' }));| Step | Call | Returns |
|---|---|---|
| 1 | doc.prepareExternalSignature(cert, options) | The document with an empty signature. options as for sign (name, reason, location, field, appearance) |
| 2 | externalSignatureData(prepared, cert) | The bytes the device signs |
| 3 | completeExternalSignature(prepared, cert, signature, options) | The signed PDF |
algorithm is 'rsa' (PKCS#1 v1.5 with SHA-256, the default) or 'ecdsa' (SHA-256, DER or raw
r||s), or 0 / 1. Devices that take a digest sign the SHA-256 of the step 2 bytes.
completeExternalSignature makes no network calls; for B-T, B-LT or B-LTA use
await completeExternalSignatureOnline(prepared, cert, signature, { algorithm, level, tsaUrl }).
Verify signatures
import { verifySignatures } from '@nexdevit/nexpdf-node';
const data = await readFile('contract-signed.pdf');
for (const s of verifySignatures(data)) {
console.log(s.signer, s.level, s.digest_ok && s.signature_ok ? 'valid' : 'INVALID');
}
const roots = await readFile('company-roots.pem', 'utf8');
const report = verifySignatures(data, roots); // trust your own root certificatesEach entry includes field, signer, subject, digest_ok, signature_ok,
covers_whole_document, reason, signing_time, timestamp_ok, chain_ok, trusted,
revocation, ltv, level, pades_level (B-B, B-T, B-LT or B-LTA) and
document_timestamp (true for a document timestamp, whose signer is the timestamp authority).
Long-term archival
For PAdES B-LTA, document timestamps and archive timestamp renewal, and what of it works under WebAssembly, see Long-term signatures.
Handwritten signatures
A drawn signature without cryptography is an ink annotation:
doc.raw.addInkSignature(page, color, width, strokes). See
Annotations.