Turn HTML into PDF with Node.js

Turn HTML into PDF with Node.js

As the title says, today’s post is about how to generate a PDF file based on HTML content, all in Node.js.

To generate PDF files for reports or downloadable content is very common in a certain type of applications where those documents play the role of reports or receipts for a transaction. Unfortunately, while some documents are basically plain text, others require a more complex structure to display all that data. If that’s the case, you can get help with HTML and transform that into a PDF file.

What we are going to need:

  • node.js
  • pug (npm package)
  • html-pdf (npm package)

Here is an example of how you can transform your data into a PDF file easily.

const pug = require('pug');
const pdf = require('html-pdf');

/* Compile the source code
*  template.pug is a standard pug template
const compiledFunction = pug.compileFile('template.pug');

// Render a set of data
const compiledContent = compiledFunction({
  name: 'Timothy'
// "<p>Timothy's Pug source code!</p>"

const filepath = //A dir
pdf.create(compiledContent).toFile(filepath, function(err, res){
  if (err) {
  } else {

Here we take advantage of the versatility provided by pug to render a template based on arbitrary data and then use html-pdf to write that content into a PDF file.

This strategy has worked great for us in recent projects and we wanted to share it with you. Please let us know if this worked for your or share in the comments the way you do similar things!

. . .

Regos Dev Studio is a product development company that builds add-ons for Jira and Confluence, combined with the development of custom solutions in a variety of languages.

Don’t forget to check out our website, visit our Atlassian marketplace listing. You can also follow us on Twitter or LinkedIn and stay tuned for updates!

Leave a Reply

Your email address will not be published. Required fields are marked *