Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a customized version of an existing pdf file with node.js?

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?

like image 515
taxilian Avatar asked Apr 28 '13 03:04

taxilian


People also ask

How do I edit a PDF in JavaScript?

To open the Document Actions dialog, choose Tools > JavaScript > Document Actions. Select an action and then click Edit to add the script to the action.


1 Answers

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.

like image 188
taxilian Avatar answered Sep 17 '22 15:09

taxilian