Newer
Older
import {
nopodofo as npdf,
NPDFAnnotation,
NPDFAnnotationFlag,
pdfDate,
} from "nopodofo";
import path from "path";
import { readFileSync } from "fs";
import { PDFNet } from "@pdftron/pdfnet-node";
export const pdf = (document: any): Promise<any> => {
return new Promise((resolve, reject) => {
const doc = new npdf.Document();
return doc.load(document, { forUpdate: true }, (e, data: any) => {
if (e instanceof Error) {
reject(e);
}
resolve(data);
});
});
};
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
export const write = (document: any): Promise<Buffer> => {
return new Promise((resolve, reject) => {
document.write((err, buf) => {
if (err instanceof Error) {
reject(err);
}
resolve(buf);
});
});
};
export const getSignatureField = (doc: any) => {
const rect = new npdf.Rect(360, 685, 50, 20),
page = doc.getPage(0),
annot = page.createAnnotation(NPDFAnnotation.Widget, rect);
annot.flags = NPDFAnnotationFlag.Print;
const image = new npdf.Image(doc, path.join(__dirname, "/qrcode.png"));
const painter = new npdf.Painter(doc);
painter.setPage(page);
painter.drawImage(image, rect.left, rect.bottom, {
width: rect.width,
height: rect.height,
});
painter.finishPage();
// end apply image to page
const field = new npdf.SignatureField(annot, doc);
// Set field properties
field.setReason("test");
field.setLocation("here");
field.setCreator("me");
field.setFieldName("signer.sign");
// This will set the date as now
field.setDate();
return field;
};
export const sign = (signer: any): Promise<Buffer> => {
const certificate = Buffer.from(
readFileSync(path.join(__dirname, "/certificate.pem"))
);
const pkey = Buffer.from(readFileSync(path.join(__dirname, "/key.pem")));
return new Promise((resolve, reject) => {
signer.loadCertificateAndKey(
certificate,
{
pKey: pkey,
},
(error, signatureLength) => {
if (error) {
reject(error);
}
signer.write(signatureLength, (e, d) => {
if (e) {
reject(e);
}
resolve(d);
});
}
);
});
};
function writeAp(pdfWriter, { w, h }, text, objId) {
const xf = pdfWriter.createFormXObject(0, 0, w, h, objId);
// draw text starting 1/4 of the way up
const textY = h / 4;
xf.getContentContext().writeText(text, 0, textY, {
size: fontSize,
colorspace: "rgb",
color: 0xfc2125,
font: arialFont,
});
pdfWriter.endFormXObject(xf);
}
export const TimestampAndEnableLTV = async (
in_docpath,
in_trusted_cert_path,
in_appearance_img_path
) => {
const doc = await PDFNet.PDFDoc.createFromBuffer(in_docpath);
doc.initSecurityHandler();
const doctimestamp_signature_field = await doc.createDigitalSignatureField();
const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
"http://rfc3161timestamp.globalsign.com/advanced"
);
const opts = await PDFNet.VerificationOptions.create(
PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
);
/* It is necessary to add to the VerificationOptions a trusted root certificate corresponding to
the chain used by the timestamp authority to sign the timestamp token, in order for the timestamp
response to be verifiable during DocTimeStamp signing. It is also necessary in the context of this
function to do this for the later LTV section, because one needs to be able to verify the DocTimeStamp
in order to enable LTV for it, and we re-use the VerificationOptions opts object in that part. */
await opts.addTrustedCertificateUString(in_trusted_cert_path);
/* By default, we only check online for revocation of certificates using the newer and lighter
OCSP protocol as opposed to CRL, due to lower resource usage and greater reliability. However,
it may be necessary to enable online CRL revocation checking in order to verify some timestamps
(i.e. those that do not have an OCSP responder URL for all non-trusted certificates). */
await opts.enableOnlineCRLRevocationChecking(true);
const widgetAnnot =
await PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc,
new PDFNet.Rect(0, 100, 200, 150),
doctimestamp_signature_field
);
await (await doc.getPage(1)).annotPushBack(widgetAnnot);
// (OPTIONAL) Add an appearance to the signature field.
const img = await PDFNet.Image.createFromMemory2(doc, in_appearance_img_path);
await widgetAnnot.createSignatureAppearance(img);
console.log("Testing timestamping configuration.");
const config_result = await tst_config.testConfiguration(opts);
if (await config_result.getStatus()) {
console.log(
"Success: timestamping configuration usable. Attempting to timestamp."
);
} else {
// Print details of timestamping failure.
console.log(await config_result.getString());
if (await config_result.hasResponseVerificationResult()) {
const tst_result = await config_result.getResponseVerificationResult();
console.log(
"CMS digest status: " + (await tst_result.getCMSDigestStatusAsString())
);
console.log(
"Message digest status: " +
(await tst_result.getMessageImprintDigestStatusAsString())
);
console.log(
"Trust status: " + (await tst_result.getTrustStatusAsString())
);
}
// return false; throw error instead
}
await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
// Save/signing throws if timestamping fails.
const res = await doc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_incremental
);
return res.buffer;
};