-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
typescript.ts
30 lines (25 loc) · 1.25 KB
/
typescript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import fs from 'fs';
import { plainAddPlaceholder } from '@signpdf/placeholder-plain';
import { P12Signer } from '@signpdf/signer-p12';
import signpdf from '@signpdf/signpdf';
async function work() {
// contributing.pdf is the file that is going to be signed
const pdfBuffer = fs.readFileSync(`${__dirname}/../../../resources/contributing.pdf`);
// certificate.p12 is the certificate that is going to be used to sign
const certificateBuffer = fs.readFileSync(`${__dirname}/../../../resources/certificate.p12`);
const signer = new P12Signer(certificateBuffer);
// The PDF needs to have a placeholder for a signature to be signed.
const pdfWithPlaceholder = plainAddPlaceholder({
pdfBuffer,
reason: 'The user is decalaring consent.',
contactInfo: '[email protected]',
name: 'John Doe',
location: 'Free Text Str., Free World',
});
// pdfWithPlaceholder is now a modified buffer that is ready to be signed.
const signedPdf = await signpdf.sign(pdfWithPlaceholder, signer);
// signedPdf is a Buffer of an electronically signed PDF. Store it.
const targetPath = `${__dirname}/../output/typescript.pdf`;
fs.writeFileSync(targetPath, signedPdf);
}
work();