JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q35789F
  • License MIT

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 generating reports, invoices, or other documents directly from web-based content.

Package Exports

  • html-pdf-js
  • html-pdf-js/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (html-pdf-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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').

🔗 Auther Details

portfolio linkedin