34

I have an old system that was written in PHP a long time ago that I would like to update to node.js to allow me to share code with a more modern system. Unfortunately, one of the main features of the PHP system is a tool that allows it to load an existing PDF file (which happens to be a government form), fill out the user's information, and provide a PDF to the browser that has all of that information present.

I have considered making a PHP script that will just do the PDF customization and using node for everything else, but it seems like something like this should be able to be done without requiring PHP to be installed.

Any idea how I might solve my problem just using node?

taxilian
  • 14,229
  • 4
  • 34
  • 73

1 Answers1

64

After a lot of searching and nearly giving up, I did eventually find that the HummusJS library will do what I want to do!

Update April 2020: In the intervening years since I posted this other options have cropped up which look like they should work. Since this question still gets a lot of attention I thought I'd come back and update with some other options:

  • pdf-lib - This one is my current favorite; it works great. It may have limitations for extremely large PDFs, but it is constantly improving and you can do nearly anything with it -- if not through the helper API then through the abstraction they provide which allows you to use nearly any raw PDF feature, though that requires more knowledge of the PDF file format than most possess.

    It's worth noting that pdf-lib doesn't support loading encrypted pdfs, but you can use something like qpdf to strip the encryption before loading it.

  • https://www.npmjs.com/package/nopodofo - This one should be one of the best options out there, but I couldn't get it working myself on a mac

  • https://www.npmjs.com/package/node-pdfsign - Not exactly the same thing but can be used with other tools to do digital signatures on a PDF. Haven't used it yet, but I expect do

Update Dec 2021: I'm still using pdf-lib and I think it's still the best available library, but there are a lot of new libraries that have come out in the last couple of years for handling PDFs, so it's worth looking around a bit.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • 2
    Do you have a working example I can see? I have a similar scenario and am using HummusJS, but can't get it to work properly. I keep on getting an 'unable to modify PDF file', but it does export the original pdf file as output (without the modification) – PBandJ Jan 31 '17 at 22:30
  • PBandJen my guess would be that the PDF in question may be "locked" -- try a different pdf (perhaps one you generate yourself) to verify – taxilian Feb 02 '17 at 15:25
  • @PBandJen I'm sure you are long past this problem, but for you and anyone else with that issue you can use `qpdf` to strip the encryption / "unlock" the file – taxilian Jan 03 '22 at 18:51