A powerful command-line tool to convert HTML files to PDF with customizable options. Built with Puppeteer for accurate rendering and full support for modern web features.
- 🎯 Accurate HTML to PDF conversion
- 📄 Support for multiple page formats (A4, A3, Letter, etc.)
- 🔄 Landscape and portrait orientations
- 🎨 Background graphics and colors preservation
- 📏 Customizable margins
- 📑 Page range selection
- 🔧 CSS injection for custom styling
- 📊 Header and footer support
- ⏱️ Configurable wait time for dynamic content
- 🌐 Full support for modern CSS and JavaScript
npm install -g html2pdf-cligit clone https://github.com/ssql2014/html2pdf.git
cd html2pdf
npm install
npm linkhtml2pdf input.htmlThis will create input.pdf in the same directory.
html2pdf input.html -o output.pdf -f A4 -l -b --margin-top 20mm --margin-bottom 20mm| Option | Alias | Description | Default |
|---|---|---|---|
--output |
-o |
Output PDF file path | {input-name}.pdf |
--format |
-f |
Page format (A4, A3, Letter, Legal, Tabloid) | A4 |
--landscape |
-l |
Use landscape orientation | false |
--margin-top |
Top margin | 0 |
|
--margin-bottom |
Bottom margin | 0 |
|
--margin-left |
Left margin | 0 |
|
--margin-right |
Right margin | 0 |
|
--background |
-b |
Print background graphics | true |
--scale |
Scale of the webpage rendering | 1 |
|
--width |
-w |
Paper width (overrides format) | |
--height |
-h |
Paper height (overrides format) | |
--wait |
Wait time in ms before generating PDF | 1000 |
|
--css |
Additional CSS to inject | ||
--header |
Display header with title and date | false |
|
--footer |
Display footer with page numbers | false |
|
--range |
Page ranges to print (e.g., "1-5, 8, 11-13") | ||
--prefer-css-page-size |
Use CSS-defined page size | false |
|
--help |
-h |
Show help | |
--version |
-v |
Show version |
html2pdf report.html -o report.pdf -f A3 -lhtml2pdf document.html --margin-top 25mm --margin-bottom 25mm --footerhtml2pdf poster.html -w 24in -h 36in -o poster.pdfhtml2pdf page.html --css "body { zoom: 0.75; }"html2pdf dynamic.html --wait 5000html2pdf long-document.html --range "1-10, 15, 20-25"Perfect for converting HTML presentations with slide breaks:
html2pdf presentation.html -f A4 -l --margin-top 0 --margin-bottom 0Generate professional reports with page numbers:
html2pdf report.html --header --footer --margin-top 20mm --margin-bottom 20mmCreate large format PDFs:
html2pdf poster.html -w 841mm -h 1189mm # A0 sizeArchive web pages with all styling preserved:
html2pdf webpage.html -b --wait 3000Use CSS page break properties in your HTML:
.page-break {
page-break-after: always;
}
.keep-together {
page-break-inside: avoid;
}Add print-specific styles:
@media print {
body {
margin: 0;
background: white;
}
.no-print {
display: none;
}
}Ensure colors are preserved in print:
* {
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
}Ensure fonts are embedded or use web-safe fonts. You can also wait longer for fonts to load:
html2pdf document.html --wait 3000Use the --background flag (enabled by default) and ensure your CSS includes print color adjustments.
Try adjusting the scale or margins:
html2pdf content.html --scale 0.9 --margin-left 10mm --margin-right 10mmMIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and feature requests, please use the GitHub issue tracker.