Top 5 HTML to PDF libraries for Node.js

Looking for a way to convert HTML to PDF in your Node.js projects? Discover the top HTML to PDF libraries that can help you generate PDFs seamlessly. This list will guide you through some of the most robust and easy-to-use libraries available.’

HTML to PDF libraries for Node.js

Table of Contents

1. Puppeteer

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is one of the most powerful HTML to PDF conversion libraries because it ensures that the PDF looks exactly like the same as it does in a web browser.

Example:

  const puppeteer = require('puppeteer');

  (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com', {waitUntil: 'networkidle0'});
    await page.pdf({path: 'example.pdf', format: 'A4'});

    await browser.close();
  })();

Output: A PDF file called example.pdf formatted in A4 size.

2. html-pdf

html-pdf is another popular library that can create PDFs from HTML content. It’s simple to use and supports flexible configurations.

Example:

  const pdf = require('html-pdf');
  const html = '<h1>Hello, world!</h1>';

  pdf.create(html).toFile('./hello-world.pdf', (err, res) => {
    if (err) return console.log(err);
    console.log(res);
  });

Output: A PDF file named hello-world.pdf with the HTML content rendered in it.

3. PDFKit

PDFKit is a PDF document generation library for Node.js that makes creating complex, multi-page, printable documents easy.

Example:

  const PDFDocument = require('pdfkit');
  const fs = require('fs');

  const doc = new PDFDocument();
  doc.pipe(fs.createWriteStream('output.pdf'));
  doc.text('Hello World!');
  doc.end();

Output: A simple PDF with the text “Hello World!” saved as output.pdf.

4. PhantomJS Cloud API

PhantomJS Cloud offers a REST API that can be used for a variety of web automation tasks, including printing PDFs from HTML. It’s a cloud-based service, so there’s no need to manage any infrastructure.

Example:

  const request = require('request');
  const options = {
    method: 'POST',
    url: 'https://PhantomJScloud.com/api/browser/v2/',
    headers: {'Content-Type': 'application/json'},
    body: {"url":"https://example.com","renderType":"pdf"}
  };

  request(options, function (error, response, body) {
    if (error) throw new Error(error);
    console.log(body);
  });

Output: A PDF rendition of the specified webpage.

5. jsreport

jsreport is an open-source reporting platform where you can design and generate reports in HTML and convert them to PDF. It provides a studio interface and can also be used programmatically in Node.js.

Example:

  const jsreport = require('jsreport-core')();

  jsreport.init().then(() => {
    return jsreport.render({
      template: {
        content: '<h1>Hello, jsreport</h1>',
        engine: 'none',
        recipe: 'chrome-pdf'
      }
    }).then((resp) => {
      // prints pdf with headline Hello jsreport
      resp.stream.pipe(fs.createWriteStream('report.pdf'));
    });
  }).catch((e) => {
    console.error(e);
  });

Output: A PDF report with Hello, jsreport as the content, saved as report.pdf.

Conclusive Summary

The integration of HTML to PDF libraries in Node.js applications can greatly simplify the process of generating printable and shareable documents. Each library on this list provides unique features, from Puppeteer’s precise rendering capabilities to jsreport’s report-designing interface. Assess your specific project needs and choose accordingly to add powerful PDF generation functionality to your applications.

References