Convert HTML files and web pages to professional PDF documents using Puppeteer.
bash
node scripts/html-to-pdf.js input.html output.pdf
node scripts/html-to-pdf.js input.html output.pdf A4
node scripts/html-to-pdf.js https://example.com output.pdf A4
javascript
const convertHtmlToPdf = require('./scripts/html-to-pdf.js');
// Simple conversion
await convertHtmlToPdf('input.html', 'output.pdf');
// With options
await convertHtmlToPdf('input.html', 'output.pdf', {
format: 'Letter',
landscape: true,
margin: { top: '20mm', bottom: '20mm' }
});
OptionTypeDefaultNotesformatstring'A4'A4, Letter, A3, A5, etc.landscapebooleanfalseLandscape orientationmarginobject10mm all{ top, right, bottom, left }scalenumber10.1 to 2.0printBackgroundbooleantrueInclude background colorsdisplayHeaderFooterbooleanfalseShow header/footer
javascript
await convertHtmlToPdf('page.html', 'output.pdf', {
displayHeaderFooter: true,
headerTemplate: '<div>Page <span class="pageNumber"></span></div>',
footerTemplate: '<div>© 2024</div>',
margin: { top: '40px', bottom: '40px' }
});
javascript
await convertHtmlToPdf('document.html', 'pages-1-5.pdf', {
pageRanges: '1-5'
});
Requires Node.js and npm:
bash
npm install puppeteer
For complete API documentation and all available options, see api.md.