npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

html-pdf-js

v0.1.4

Published

`html-pdf-js` is a JavaScript library that enables the generation of PDF documents from HTML content using JavaScript. It leverages the power of the HTML5 canvas and PDF.js to render and convert HTML pages into PDF files, making it an ideal tool for gener

Readme

HTML to PDF

html-pdf-js is a Node.js package that allows you to convert HTML content into PDF documents using the power of Puppeteer. Puppeteer, a headless Chrome Node.js API, is used to render and generate high-quality PDFs from HTML templates.

This package simplifies the process of turning dynamic HTML into professional-looking PDFs.

npm version License

Features

  • HTML to PDF Conversion: Converts HTML files or content into PDFs using Puppeteer.
  • Customizable Layouts: Set page size, margins, headers, footers, and more.
  • Headless Browser Rendering: Uses Puppeteer for headless rendering to ensure accurate PDF output.
  • Efficient and Fast: Optimized for performance while maintaining quality.

Installation

You can install the library using npm:

  npm install html-pdf-js

HTML to PDF Using htmlToPdf

This guide demonstrates how to convert HTML code to PDF using the htmlToPdf library.

Code Example

// Import the required libraries
const { htmlToPdf } = require('html-pdf-js');  // Import the htmlToPdf function from the html-pdf-js package
const path = require('path');  // Import the path module to handle file paths

// Define your HTML content that you want to convert to PDF
const htmlContent = '<h1>Hello World</h1>';  // Example HTML content

// Specify the path where you want to save the generated PDF
const pdfPath = path.join(__dirname, '/pdf/sample.pdf');  // Path to save the PDF file, ensure the directory exists

// Call the htmlToPdf function to generate the PDF from the HTML content
// The first parameter is the path where the PDF will be saved
// The second parameter is the HTML content you want to convert
const pdfPathOutput = await htmlToPdf(pdfPath, htmlContent);

// Output the path of the saved PDF
console.log('PDF saved at:', pdfPathOutput);  // Logs the location of the saved PDF file

With Page Options Code Example

This example demonstrates how to convert HTML code to PDF using the htmlToPdf function while specifying page options such as format, margin, and wait time.

// Import the required libraries
const { htmlToPdf } = require('html-pdf-js');  // Import the htmlToPdf function from the html-pdf-js package
const path = require('path');  // Import the path module to handle file paths

// Define your HTML content that you want to convert to PDF
const htmlContent = '<h1>Hello World</h1>';  // Example HTML content

// Specify the path where you want to save the generated PDF
const pdfPath = path.join(__dirname, '/pdf/sample.pdf');  // Path to save the PDF file, ensure the directory exists

// Define the options for the page layout (e.g., format, margin, waitUntil)
const options = {
  format: 'A4',  // Set the PDF page format to A4
  margin: {
    top: '0in',   // Set the top margin to 0 inches
    bottom: '0in',  // Set the bottom margin to 0 inches
    left: '0in',  // Set the left margin to 0 inches
    right: '0in'  // Set the right margin to 0 inches
  },
  waitUntil: 'networkidle0',  // Wait until there are no network connections for at least 500 ms before generating the PDF
};

// Call the htmlToPdf function to generate the PDF from the HTML content with page options
// The first parameter is the path where the PDF will be saved
// The second parameter is the HTML content you want to convert
// The third parameter is an object with page options
const pdfPathOutput = await htmlToPdf(pdfPath, htmlContent, options);

// Output the path of the saved PDF
console.log('PDF saved at:', pdfPathOutput);  // Logs the location of the saved PDF file

Explanation

  • htmlToPdf: The core function used to convert HTML to PDF.
  • htmlContent: The HTML string that you wish to convert into a PDF.
  • pdfPath: The file path where the generated PDF will be saved.
  • path.join(): Ensures cross-platform compatibility for file paths.
  • options: An object to customize the PDF layout, such as the format, margins, and waiting time for network activity before generating the PDF.
    • format: Specifies the page format, such as A4 or Letter.
    • margin: Defines the page margins (top, bottom, left, right).
    • waitUntil: Specifies the event that should trigger the PDF generation, such as networkidle0.

Parameters

The htmlToPdf function accepts the following parameters:

| Parameter | Type | Description | |------------------|----------------------|-------------------------------------------------------------------------------------------------------| | pdfPath | string | The file path where the generated PDF will be saved. | | htmlContent | string | The HTML content that you want to convert to PDF. | | options | object | An optional object to customize the PDF layout and generation. | | options.format | string | Specifies the page format (e.g., 'A4', 'Letter'). | | options.margin | object | Defines the page margins. | | options.margin.top | string | The top margin (e.g., '0in', '1in'). | | options.margin.bottom| string | The bottom margin (e.g., '0in', '1in'). | | options.margin.left | string | The left margin (e.g., '0in', '1in'). | | options.margin.right | string | The right margin (e.g., '0in', '1in'). | | options.waitUntil | string | Specifies the event that should trigger the PDF generation (e.g., 'networkidle0', 'domcontentloaded'). |

🔗 Author Details

portfolio linkedin