0.1.4 • Published 6 months ago

html-pdf-js v0.1.4

Weekly downloads
1
License
MIT
Repository
-
Last release
6 months ago

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:

ParameterTypeDescription
pdfPathstringThe file path where the generated PDF will be saved.
htmlContentstringThe HTML content that you want to convert to PDF.
optionsobjectAn optional object to customize the PDF layout and generation.
options.formatstringSpecifies the page format (e.g., 'A4', 'Letter').
options.marginobjectDefines the page margins.
options.margin.topstringThe top margin (e.g., '0in', '1in').
options.margin.bottomstringThe bottom margin (e.g., '0in', '1in').
options.margin.leftstringThe left margin (e.g., '0in', '1in').
options.margin.rightstringThe right margin (e.g., '0in', '1in').
options.waitUntilstringSpecifies the event that should trigger the PDF generation (e.g., 'networkidle0', 'domcontentloaded').

🔗 Author Details

portfolio linkedin

0.1.0

6 months ago

0.0.1

6 months ago

0.1.2

6 months ago

0.0.3

6 months ago

0.1.1

6 months ago

0.0.2

6 months ago

0.1.4

6 months ago

0.0.5

6 months ago

0.1.3

6 months ago

0.0.4

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.0

6 years ago