Skip to Content
NexPDF

Digital signatures

Sign PDFs with PAdES using a PKCS#12 file or an external signer such as an HSM or signing service in the browser, and verify signatures, with NexPDF for the web.

English

License module

Signing needs the signatures module (Business tier and above). verifySignatures works without a key.

Every signing call returns the complete signed file as a Uint8Array. Keep those bytes as they are: saving the document object again afterwards breaks the signature.

Sign with a PKCS#12 file

sign(p12, password, options) signs with a .p12 or .pfx identity, for example one the user picks with a file input:

const p12 = new Uint8Array(await p12File.arrayBuffer());
const signed = doc.sign(p12, password, {
  reason: 'Approved',
  location: 'Lisbon',
  appearance: { page: 0, rect: [360, 60, 560, 120], text: 'Signed by Ana Silva' },
});

const url = URL.createObjectURL(new Blob([signed], { type: 'application/pdf' }));

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 makes no network calls, so it signs 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.raw.signPades(signer, reason, location) signs with a new self-signed certificate.

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:

const cert = new Uint8Array(await (await fetch('/signer-chain.pem')).arrayBuffer());   // PEM or DER, chain optional

const prepared = doc.prepareExternalSignature(cert, { name: 'Ana Silva', reason: 'Approval' });
const toSign = nex.externalSignatureData(prepared, cert);        // DER signed attributes
const signature = await signingService.sign(toSign);             // your call: SHA-256 with RSA or ECDSA
const signed = nex.completeExternalSignature(prepared, cert, signature, { algorithm: 'ecdsa' });
StepCallReturns
1doc.prepareExternalSignature(cert, options)The document with an empty signature. options as for sign (name, reason, location, field, appearance)
2nex.externalSignatureData(prepared, cert)The bytes the device signs
3nex.completeExternalSignature(prepared, cert, signature, { algorithm })The signed PDF

algorithm is 'rsa' (PKCS#1 v1.5 with SHA-256, the default) or 'ecdsa' (SHA-256, DER or raw r||s). Devices that take a digest sign the SHA-256 of the step 2 bytes. completeExternalSignature gives PAdES B-B; await nex.completeExternalSignatureOnline(prepared, cert, signature, { algorithm, level, tsaUrl }) completes at B-T, B-LT or B-LTA.

Verify signatures

const report = nex.verifySignatures(new Uint8Array(await signedFile.arrayBuffer()));
for (const s of report) {
  console.log(s.signer, s.level, s.digest_ok && s.signature_ok ? 'valid' : 'INVALID');
}

Each 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 in the browser, see Long-term signatures.

Handwritten signatures

A drawn signature without cryptography is an ink annotation: doc.raw.addInkSignature(page, color, width, strokes). See Annotations.

On this page