Signs pdf files. Uses pdf-lib to add signature to pdf files and node-forge to sign documents and verify the integrity of signed documents.
npm i sign-pdf-lib
Instantiate digital signer:
const settings: SignatureSettings {
signatureLength: ...,
rangePlaceHolder: ...,
signatureComputer: {
certificate: await fse.readFile(...),
password: '...'
}
}
const pdfSigner = new PdfDigitalSigner(settings);
or
const settings: SignatureSettings {
signatureLength: ...,
rangePlaceHolder: ...,
psignatureComputer: {
emCertificate: await fse.readFile(..., 'ascii'),
pemKey: await fse.readFile(..., 'ascii'),
certificatePassword: '...'
}
}
const pdfSigner = new PdfDigitalSigner(settings);
const parameters: SignDigitalParameters = {
pageNumber: 1,
signature: {
name: 'Test Signer',
location: 'Timisoara',
reason: 'Signing',
contactInfo: 'signer@semnezonline.ro'
},
visual: {
rectangle: {
left: 50,
top: 641,
right: 264,
bottom: 711
},
background: await fse.readFile(...),
texts: [{
lines: [
'JOHN',
'DOE'
]}, {
lines: [
'Digitally signed by',
'JOHN DOE',
'Date: 2023.11.03',
'20:28:46 +02\'00\''
]}
]
}
};
IMPORTANT: if coordinates are negative, they are considered from right or bottom.
For non visual signatures, just omit visual field:
const parameters: SignDigitalParameters = {
pageNumber: 1,
signature: {
name: 'Test Signer',
location: 'Timisoara',
reason: 'Signing',
contactInfo: 'signer@semnezonline.ro'
}
};
If you want a specific name for signature, specify it:
const parameters: SignDigitalParameters = {
pageNumber: 1,
name: 'Signature2',
...
};
const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signAsync(pdf, parameters);
const pdf = await fse.readFile(...);
const placeholderPdf = await pdfSigner.addPlaceholderAsync(pdf, parameters);
const parameters: AddFieldParameters = {
pageNumber: 1,
rectangle: {
left: 50,
top: 641,
right: 264,
bottom: 711
}
};
IMPORTANT: if coordinate are negative, they are considered from right or bottom.
If you want a specific name for signature, specify it:
const parameters: AddFieldParameters = {
pageNumber: 1,
name: 'Signature2',
...
};
const pdf = await fse.readFile(...);
const fieldPdf = await pdfSigner.addFieldAsync(pdf, parameters);
const pdf = await fse.readFile(...);
const fields = await pdfSigner.getFieldsAsync(pdf);
const parameters: SignFieldParameters = {
fieldName: 'Signature1,
signature: {
name: 'Test Signer',
location: 'Timisoara',
reason: 'Signing',
contactInfo: 'signer@semnezonline.ro'
},
visual: {
background: await fse.readFile(...),
texts: [{
lines: [
'JOHN',
'DOE'
]}, {
lines: [
'Digitally signed by',
'JOHN DOE',
'Date: 2023.11.03',
'20:28:46 +02\'00\''
]}
]
]
};
const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signFieldAsync(pdf, parameters);
const pdf = await fse.readFile(...);
const checks = await pdfSigner.verifySignaturesAsync(pdf);
IMPORTANT!: This function checks only the integrity of signatures (if the document has been changed after it has been signed).
Instantiate visual signer:
const pdfSigner = new PdfVisualSigner();
const parameters: SignVisualParameters = {
pageNumber: 1,
rectangle: {
left: 50,
top: 641,
right: 264,
bottom: 711
},
background: await fse.readFile(...),
texts: [{
lines: [
'JOHN',
'DOE'
]}, {
lines: [
'Digitally signed by',
'JOHN DOE',
'Date: 2023.11.03',
'20:28:46 +02\'00\''
]}
]
};
IMPORTANT: if coordinates are negative, they are considered from right or bottom.
If the signature is placed relatively to the bottom pf the page use reverseY to fix the problem:
const parameters: SignVisualParameters = {
...
reverseY: true,
...
};
const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signAsync(pdf, parameters);