-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
47 lines (37 loc) · 1.16 KB
/
index.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { PDFDocument } = require('pdf-lib');
const { readFile, writeFile } = require('fs/promises');
async function createPdf(input, output) {
try {
const pdfDoc = await PDFDocument.load(await readFile(input));
// Modify doc, fill out the form...
const fieldNames = pdfDoc
.getForm()
.getFields()
.map((f) => f.getName());
console.log({ fieldNames });
const form = pdfDoc.getForm();
const possibleFields = Array.from({ length: 111 }, (_, i) => i);
possibleFields.forEach((possibleField) => {
try {
form
.getTextField(`Text${possibleField}`)
.setText(possibleField.toString());
} catch (error) {
// console.error(error);
}
});
form.getTextField('Text2').setText('John Smith');
form.getCheckBox('Check Box7').check();
pdfDoc.removePage(0);
pdfDoc.removePage(1);
pdfDoc.removePage(1);
pdfDoc.removePage(1);
pdfDoc.removePage(1);
const pdfBytes = await pdfDoc.save();
await writeFile(output, pdfBytes);
console.log('PDF created!');
} catch (err) {
console.log(err);
}
}
createPdf('medical-claim-form_unlocked.pdf', 'output.pdf');