Creating PDFs with Node.js - Part 1
The ability to generate PDFs is a common business need. There are many PDF libraries for a variety of programming languages and use cases available to help with this task. Some are proprietary and some are open-source.
In one of my recent projects I needed to create a PDF generation service using Node.js. My requirements were:
- PDF generation should be quick (less than a minute)
- Use only open-source libraries
- Layout the PDF contents using HTML and CSS (as opposed to a library-specific API)
These requirements led me to Puppeteer. Puppeteer is an API for manipulating Chromium, which is an open-source version of the Chrome browser. The feature most relevant to my PDF generator project was the ability to create a PDF from a web page.
Here is the PDF creation example from the repo’s readme:
The steps are essentially create a new browser instance, tell that instance to load a URL, then save the rendered page as a PDF. Easy!
Here’s how I used Puppeteer in my project:
renderer.js:
Instead of getting the HTML into Chromium via a URL, I wanted to provide the HTML as a string. With Puppeteer you can set a page’s HTML using page.setContent()
. After the page’s HTML is set, it gets saved to a PDF with page.pdf()
.
index.js:
The index script reads HTML from a file (markup.html
) and passes the HTML as a string to the renderer’s generatePDFFromHTML()
method, defined above. That method returns the PDF as a Buffer
, which is then written to a file.