Verify PDF signatures in Node.js with Certeno
In this tutorial you write a small Node.js script that reads a signed PDF, verifies every digital signature in it, and prints one line per signature. There is no viewer and no browser: the same engine that runs in the viewer runs inside your Node process.
You will:
- create a Node project and install Certeno,
- write a script that verifies a PDF and prints the result,
- add your licence, which a server or script needs before it starts.
Prerequisites
- Node.js 20.19 or later and npm.
- Access to the Certeno registry. Certeno's packages are served from a private registry. Installation shows how to point the
@trexolabscope at it. - A PDF with a digital signature, to see something to verify.
- A Certeno Server licence, for step 4. You need its licence key (it starts with
CERT-). If you do not have one yet, request an evaluation or a production licence on the contact page. You can do steps 1 to 3 without it.
Tip: You can try the verification itself, with no licence, in the live demo before you set anything up.
1. Create a Node project
-
Create a folder and a
package.jsonin it:bashmkdir verify-pdf && cd verify-pdf npm init -y -
Turn on ES modules, so the script can use
importand top-levelawait:bashnpm pkg set type=module
2. Install Certeno
-
Point the
@trexolabscope at the Certeno registry, in a.npmrcfile at the project root:bashecho '@trexolab:registry=https://certeno.trexolab.com/api/registry' > .npmrc -
Install the core package. It contains the verification engine:
bashnpm install @trexolab/certeno-core
There is nothing else to set up. The engine is a WebAssembly module inside the package, so you do not compile anything or copy a .wasm file.
3. Verify a PDF
-
Create
verify.mjs:jsimport { createVerifier } from '@trexolab/certeno-core' import { readFile } from 'node:fs/promises' const path = process.argv[2] if (!path) { console.error('Usage: node verify.mjs <file.pdf>') process.exit(1) } // createVerifier() is async: it starts the engine. const verifier = await createVerifier() try { const result = await verifier.verify(await readFile(path), path) if (result.signatures.length === 0) { console.log(`${path} has no signatures.`) } else { console.log(`${result.signatures.length} signature(s) in ${path}`) for (const sig of result.signatures) { console.log(`- ${sig.name}: ${sig.overallStatus} (${sig.overallMessage})`) } } } finally { // Release the engine, even when verify() throws. verifier.dispose() } -
Run it on your signed PDF:
bashnode verify.mjs signed.pdfYou should see a line for the file and one line for each signature, such as:
text1 signature(s) in signed.pdf - Jane Example: warning (Signature is VALID with warnings — signer's identity could not be verified against a trusted root.)The name and the message depend on your PDF. Each signature gets one overall status:
valid,warning,invalidorunknown. The line above is what a signature that is intact, but whose certificate does not chain to a root Certeno trusts, looks like. That is expected for a test certificate; a signature from a trusted authority readsvalid.
verify() returns much more than this: eight separate checks per signature (integrity, signature, certificate chain, expiry, timestamp, revocation, algorithm and key usage), the signer's certificate and the PAdES level. Examples prints all of it.
Note: Without the licence option, createVerifier() does no licence check. A server or script that you run for your own users needs a Server licence, which the next step adds.
4. Add your licence
A Server licence belongs to your machines. Certeno activates the machine the first time the script runs, and checks the licence again every 6 hours while a long-running process works. The verifier does not start unless the licence is valid and includes the node_runtime and signature_verification features.
-
Install the licence server's SDK. Certeno loads it only when you pass
licence. Pin the version, because thelatesttag on npm points at an older beta:bashnpm install @trexolab-solution/[email protected] -
Keep the licence key out of your code. Put it in an environment variable:
bashexport CERTENO_LICENCE_KEY=CERT-XXXX-XXXX-XXXX-XXXX -
Create
licensed.mjs:jsimport { createVerifier, CertenoLicenceError } from '@trexolab/certeno-core' try { const verifier = await createVerifier({ licence: { licenceKey: process.env.CERTENO_LICENCE_KEY }, }) console.log('Licensed. Features:', verifier.licence?.features.join(', ')) // ... verifier.verify(pdf, 'file.pdf') verifier.dispose() } catch (error) { if (error instanceof CertenoLicenceError) { console.error(`Licence problem: ${error.code}`) process.exit(1) } throw error } -
Run it:
bashnode licensed.mjsThe first run needs a network, because it activates the machine. You should see
Licensed.followed by the features your plan includes. Later runs only check the licence, and start from the cached activation if the network is down.
5. Verify your output
-
Run
node verify.mjs signed.pdfagain. The output is the same as in step 3: the licence changes what the engine may do, not what a signature looks like. -
To see a licence that is refused, unset the key and run the licensed script:
bashunset CERTENO_LICENCE_KEY node licensed.mjsYou should see
Licence problem: NO_LICENCE_KEY, and the script exits with status 1. A key the licence server does not know givesUNAUTHORIZEDinstead. -
In a container, mount the folder in the home directory where the activation is kept. Otherwise every start looks like a new machine and uses one of your plan's activations. Licensing lists the options.
If something is not as described, see Troubleshooting. Every code a licence error can have is listed in Licensing.
Next steps
- Licensing: what each feature unlocks, and how a licence behaves when the network is down.
- Examples: print every check, the signer's certificate and the timestamp.
- Revocation Checking: check online whether a certificate has been revoked, from a server.
- Core API:
createVerifier(),verify()and theVerificationResulttypes. - Deployment: the production checklist.
- Error Reference: the errors you can meet and how to handle them.