Simple signing of PDFs in node.
The purpose of this package is not as much to be used as a dependendency, although it could. The main purpose is to demonstrate the way signing can be achieved in a piece of readable code as it can take a lot of hours to figure out.
Simply said this could be used in two steps. install
and sign
.
Install with npm i -S node-signpdf node-forge
.
And call .sign()
import signer from 'node-signpdf';
const signedPdf = signer.sign(
fs.readFileSync(PATH_TO_P12_CERTIFICATE),
fs.readFileSync(PATH_TO_PDF_FILE)
);
In practice we expect that most people will just read through the code we've written in the testing part of this package and figure it out themselves. If that's your case, you should read the [Signing PDF in simple steps] section.
- The process of signing a document is described in the Digital Signatures in PDF document.
- This lib:
- requires the signature placeholder to already be in the document. Takes
Buffer
s of the PDF and a P12 certificate to use when signing - does not cover multiple signatures, incremental updates, etc. Only the basic scenario of signing a freshly created PDF. We actually only worked with documents created with PDFKit.
- requires the signature placeholder to already be in the document. Takes
- Feel free to copy and paste any part of this code. See its defined Purpose.
See the unit-testing code. PDFKit is used there for generating the document. This also allows easy addition of the signature placeholder.
What's needed is a Sig
element and a Widget
that is also linked in a Form
. The form needs to be referenced in the root descriptor of the PDF as well. A (hopefully) readable sample is available in the test. Note the Contents
descriptor of the Sig
where zeros are placed that will later be replaced with the actual signature.
Note: Signing in detached mode makes the signature length independent of the PDF's content length, but it may still vary between different signing certificates. So every time you sign using the same P12 you will get the same length of the output signature, no matter the length of the signed content. It is safe to find out the actual signature length your certificate produces and use it to properly configure the placeholder length.
That's where node-signpdf
kicks in. Given a PDF and a P12 certificate a signature is generated in detached mode and is replaced in the placeholder. This is best demonstrated in the tests.
node-forge is used for working with signatures.
PDFKit is used in the tests for generating a PDF with a signature placeholder.
- The whole signing flow is a rework of what's already in pdfsign.js so thanks go to @tbocek
- node-forge is an awesome package written in pure JavaScript and supports signing in detached mode. Many thanks to all the guys who wrote and maintain it.
- Thanks to the guys of PDFKit as well. They've made PDF generation incredibly easy.
- All PRs are welcome.
- This is a git-flow repo. We use the default git flow with a
v
version prefix. - Note that gitmoji is used in the commit messages. That's not a must but we think it's nice.