Problem : I have an existing PDF form ( *.pdf ) that needs to be filled out. How can I fill it dynamically by using the Node JS?
Check out the following modules on npm:
fill-pdf
pdffiller
pdf-fill-form
node-pdffiller
pdfkit
The node-pdffiller
is a wrapper for PDFtk, the PDF Toolkit:
There's a lot of good documentation on the website of PDFKit:
Those modules have different features and API. You should find something that suits your needs.
There's also a nice article:
For anyone still searching for a solution, give pdf-lib (https://pdf-lib.js.org / https://www.npmjs.com/package/pdf-lib) a look.
There's a nice thread on how to fill in PDF form text fields using the pdf-lib Node.js package: https://github.com/Hopding/pdf-lib/issues/48
I know this is a late response but I found a good library called PDF-LIB
PDF-LIB
It is so feature rich;
This is an example of a code to work with it
const { PDFDocument } = require('pdf-lib')
const fs = require('fs')
const util = require('util')
const data = {
"name": "Alice Alley",
"dateOfBirth": "01/01/1990",
"favoriteFood": "Glue"
}
async function createPdf(input, output) {
const readFile = util.promisify(fs.readFile)
function getStuff() {
return readFile(input)
}
const file = await getStuff()
const pdfDoc = await PDFDocument.load(file)
const form = pdfDoc.getForm()
Object.keys(data).forEach((element) => {
const field = form.getTextField(element)
field.setText(data[element])
})
const pdfBytes = await pdfDoc.save()
fs.writeFile(output, pdfBytes, () => {
console.log('PDF created!')
})
}
createPdf("input.pdf", "output.pdf")
// PDF library https://pdf-lib.js.org/
// PDF form creation https://www.pdfescape.com/
You actually need to create a form inside your PDF file then give each field a name in order to reference it using the code.
PDF Escape
PDF Escape is a website that you upload your PDF then you create a form over it using the "Form Field" button... Over each field right click and select "Object properties..." and assign a name to the field with which you will reference by the code later, then save the file, download it and put it in the project directory.
Note: If you want your fields uneditable and be filled only by script you need to make the field "Read only" in the "Object properties..." also.
After that give the input file name to the createPdf function as well as the output name.
The data object consists on having the keys as the field names in the PDF document and the values as the values that you want to put in the document later.
I read documentation for over a dozen packages and implemented about 5, the only one I could get to work with the requirements being in Node JS and can load a remote PDF file from my MongoDB and fill it was pdfform.js.
More details here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With