Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill Data In Existing PDF Form Using NodeJS

Tags:

node.js

pdf

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?

like image 994
M.Tae Avatar asked Dec 25 '16 14:12

M.Tae


Video Answer


4 Answers

Check out the following modules on npm:

  • fill-pdf
  • pdffiller
  • pdf-fill-form
  • node-pdffiller
  • pdfkit

The node-pdffilleris a wrapper for PDFtk, the PDF Toolkit:

  • https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

There's a lot of good documentation on the website of PDFKit:

  • http://pdfkit.org/

Those modules have different features and API. You should find something that suits your needs.

There's also a nice article:

  • Generating a PDF with Express & Node.js by Koen van Gilst (it uses PDFKit)
like image 170
rsp Avatar answered Oct 19 '22 05:10

rsp


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

like image 30
Martin Carstens Avatar answered Oct 19 '22 04:10

Martin Carstens


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.

like image 22
Sofienne Lassoued Avatar answered Oct 19 '22 05:10

Sofienne Lassoued


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.

like image 31
JBW Avatar answered Oct 19 '22 04:10

JBW