DEV Community

Cover image for Simplify PDF Generation in Node.js with html-to-pdf-pup
Aditya Raj
Aditya Raj

Posted on

Simplify PDF Generation in Node.js with html-to-pdf-pup

NPM

Introduction:

I have created a new npm package html-to-pdf-pup, a powerful Node.js package designed to simplify the PDF generation process. In this article, we'll explore how html-to-pdf-pup works, its features, installation process, usage scenarios, and how its optimized.

What is html-to-pdf-pup?

It is a Node.js package that enables developers to convert HTML and CSS content into PDF documents seamlessly. Built on top of Puppeteer, a headless Chrome browser API, html-to-pdf-pup offers a scalable, event-driven system for generating PDFs efficiently.

Features:

  • Highly scalable event-driven system.
  • Asynchronously convert multiple HTML files to PDF using multiple tabs or a single tab for queue PDF generation.
  • Message queue system for managing the conversion process.
  • Simple and easy-to-use API suitable for beginners.
  • Configurable options for advanced users.
  • Full control over the conversion process and PDF options.
  • Returns PDF buffer for further processing.
  • Dynamic height for PDFs by default.

Optimizations techniques used:

html-to-pdf-pup employs several optimization techniques to enhance performance and resource management:

  1. Message Queue System: Utilizes a message queue system to manage the conversion process efficiently, ensuring smooth operation even with multiple conversion requests.

  2. Single Browser Instance: Maintains only a single browser instance regardless of the number of conversion requests, reducing resource overhead.

  3. Concurrent Tab Conversion: Opens multiple tabs for concurrent PDF conversion, maximizing throughput and minimizing processing time.

  4. Tab Recycling: Once a tab completes PDF conversion, it immediately starts processing another request, optimizing tab utilization and reducing idle time.

  5. Integration of Max Tab System: Integrates the maximum tab system with a single browser instance for better resource management, balancing performance and resource consumption effectively.

Why choose this package over the others:-

  1. Light weight: html-to-pdf-pup is very light with only 17kb as unpacked size in the npm package.

  2. Fully configurable: Unlike other packages html-to-pdf-pup is gives full control to the user with its many configurable options and also a dev mode.

  3. Highly optimized for pdf creation: html-to-pdf-pup comes highly optimized out of the box. it has been tested to use minimal memory and resources of server. it gets these optimizations from features such as single browser instance , Tab recycling , zero resource comsumption in downtime,etc.

  4. Scalablity: The main selling point of html-to-pdf-pup is its scalablity , here users can control how many concurrent request it can handle according to their available resources . handeling many request once can lead to faster performance but can also use more resources.

  5. here , user can find their sweet spot according to their needs.

  6. Open source and Documentation:html-to-pdf-pup is an open-source project with active development and maintenance. It benefits from contributions from developers worldwide, ensuring continuous improvement, updates, and enhancements. By using html-to-pdf-pup, you contribute to and benefit from the vibrant open-source ecosystem.

Installation:

To install html-to-pdf-pup, simply run the following command in your Node.js project directory:

npm i html-to-pdf-pup

Enter fullscreen mode Exit fullscreen mode

Basic Usage:

Using html-to-pdf-pup is straightforward. Here's a basic example demonstrating how to convert HTML content to a PDF:

const { create_pdf } = require("html-to-pdf-pup");

let htmlData = "<html><body><h1>Hello World</h1></body></html>";

create_pdf(htmlData)
  .then((pdfBuffer) => {
    console.log(pdfBuffer);
  })
  .catch((err) => {
    console.log(err);
  });
Enter fullscreen mode Exit fullscreen mode

Config Mode:

For advanced users, html-to-pdf-pup offers a config mode to customize Puppeteer , PDF configurations and also a DEV mode:

For more info refer :- npm

Conclusion:

html-to-pdf-pup simplifies the process of converting HTML and CSS content to PDF documents in Node.js. With its powerful features and optimization techniques, including scalability, asynchronous conversion, and configurable options, developers can efficiently generate PDFs for various use cases. Whether you're a beginner or an experienced developer, html-to-pdf-pup provides a seamless solution for your PDF generation needs in Node.js. Try it out in your next project and experience the convenience it offers!

Top comments (0)