0.1.4 • Published 6 months ago
html-pdf-js v0.1.4
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.
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
orLetter
. - margin: Defines the page margins (top, bottom, left, right).
- waitUntil: Specifies the event that should trigger the PDF generation, such as
networkidle0
.
- format: Specifies the page format, such as
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' ). |