easyhtml2pdf
v1.0.2
Published
High-level HTML → PDF converter using easyhtml2pdf with full options
Maintainers
Readme
EasyHTML2PDF
EasyHTML2PDF is a Node.js utility for converting HTML content to PDF files with advanced customization options. It supports RTL languages (such as Arabic), custom headers/footers, page formatting, and more. This project is ideal for generating reports, invoices, or any document from HTML.
Features
- Convert HTML to PDF: Render any HTML string or file as a PDF.
- RTL Language Support: Easily generate PDFs in right-to-left languages.
- Custom Page Format: Choose standard formats like A4, Letter, etc.
- Custom Margins: Set page margins in millimeters.
- Header & Footer Templates: Add custom HTML headers and footers.
- Print Backgrounds: Optionally include background graphics/colors.
- Flexible Output: Save PDFs to any path.
Installation
npm install easyhtml2pdfUsage
Here’s an example for generating an Arabic PDF report:
const { convertToPdf } = require("easyhtml2pdf");
async function go() {
const html = `...your HTML content...`;
await convertToPdf({
html,
output: "examples/output_arabic.pdf",
options: {
format: "A4",
printBackground: true,
margin: { top: "10mm", bottom: "10mm", left: "10mm", right: "10mm" },
displayHeaderFooter: true,
headerTemplate: '<div style="font-size:10px; text-align:center; margin-left:20px;">Header</div>',
footerTemplate: '<div style="font-size:10px; text-align:center; margin-left:20px;">Page<span class="pageNumber"></span> / <span class="totalPages"></span></div>'
}
});
console.log("PDF created: examples/output_arabic.pdf");
}
go();API Reference
convertToPdf({ html, output, options })
Parameters
| Name | Type | Description | Required |
|-----------|----------|----------------------------------------------------------------------------------------------|----------|
| html | string | The HTML content to convert. | Yes |
| output | string | Output file path for the generated PDF. | Yes |
| options | object | PDF rendering options (see below). | No |
PDF Options (options)
These options are passed directly to Puppeteer's page.pdf():
| Option | Type | Default | Description |
|-----------------------|----------|------------------------------------------------------------|-----------------------------------------------------------------------------|
| format | string | "A4" | Paper format (A3, A4, A5, Letter, Legal) |
| landscape | boolean | false | Orientation of the PDF |
| printBackground | boolean | true | Include CSS backgrounds and images |
| margin | object | {top:'20mm', bottom:'20mm', left:'15mm', right:'15mm'} | Page margins |
| displayHeaderFooter | boolean | false | Enable header/footer rendering |
| headerTemplate | string | - | HTML string for header (works only if displayHeaderFooter: true) |
| footerTemplate | string | - | HTML string for footer |
| scale | number | 1 | Scale of the webpage rendering (0.1–2) |
| pageRanges | string | " " | e.g. "1-3" renders only specified pages |
| width | string | - | Page width in px/mm/in (overrides format) |
| height | string | - | Page height in px/mm/in |
| preferCSSPageSize | boolean | false | Use CSS @page size if available |
Example options
options: {
format: "A4",
landscape: false,
printBackground: true,
margin: { top: "10mm", bottom: "10mm", left: "10mm", right: "10mm" },
displayHeaderFooter: true,
headerTemplate: "<div>Header</div>",
footerTemplate: "<div>Page <span class='pageNumber'></span> / <span class='totalPages'></span></div>",
scale: 1,
pageRanges: "1-3",
width: "210mm",
height: "297mm",
preferCSSPageSize: false
}Advanced Usage
- RTL Support: Use
<html lang="ar" dir="rtl">and setdirection:rtlin your CSS. - Dynamic Content: Insert variables in your HTML before passing to
convertToPdf. - Custom Fonts: Reference web fonts in your HTML
<head>.
Output
The generated PDF will be saved at the path specified in the output parameter.
License
MIT
Author
AQ
Note:
If you need more examples or want to customize further, check the examples/ folder or open
