Turn over the big rock first
September 10, 2021Chromium Developer Tools – Animations
October 11, 2021Recently I had a task to auto-fill a PDF for a client to prevent them from the tedious work of filling them in individually.
The way I accomplished this was to use the npm package called “node-pdftk“. This is the package I found to work best. The first step is to install the package npm install node-pdftk
. Once it is installed, you will need to put pdftk in your system path. After the setup is complete, you will need to require it in your file const pdftk = require('node-pdftk');
.
One nice thing I found with this package was the helpful chaining.
pdftk.input(pdfTemplatePath).fillForm(data.formData).flatten().output().then(buf => {
res.type('application/pdf');
res.send(buf);
}).catch(e => {
res.status(500).json({error: true, message:'Error filling PDF.'});
});
In order to fill the pdf, you must know the field names of the fields you will be filling. Since you already need pdftk to use the node-pdftk package, you can use it to get the form fields. All you have to do is run the following command: pdftk filename.pdf dump_data_fields
. An example of the output that will be generated is:
---
FieldType: Text
FieldName: License #
FieldNameAlt: License #
FieldFlags: 12582912
FieldJustification: Left
---
FieldType: Text
FieldName: Date Signed
FieldFlags: 0
FieldJustification: Left
---
Once the form fields have been dumped out, the next step is to create an object that contains the field names as the key with that value being what you would like in the form field.
For example:
{
"Date Signed": "1/1/2022",
"License #": "R245-3425-24"
}
Overall I thought using pdftk with the node-pdftk package was very easy to work with. I hope this solution helps filling in your pdf form quickly and easily!