How to convert HTML to PDF
- 1
Browser Print to PDF (simplest — no tools needed)
In Chrome: open the HTML file or URL → Ctrl+P (Windows) or Cmd+P (Mac) → set Destination to 'Save as PDF' → adjust margins, scale, and paper size → click Save. For headless browser consistency: File → Print → 'Open PDF in Preview' (Mac) or 'Microsoft Print to PDF' (Windows). This captures exactly what you see — fonts, CSS, background colors. Enable 'Background graphics' in More Settings to include CSS background colors.
- 2
Puppeteer (Node.js, programmatic PDF generation)
Best for server-side generation: `npm install puppeteer`. Script: `const puppeteer = require('puppeteer'); const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('file:///path/to/file.html', {waitUntil: 'networkidle0'}); await page.pdf({path: 'output.pdf', format: 'A4', printBackground: true, margin: {top: '20mm', bottom: '20mm', left: '15mm', right: '15mm'}}); await browser.close();`. Puppeteer uses headless Chromium — identical rendering to Chrome.
- 3
wkhtmltopdf (command line, fast and scriptable)
Install: `sudo apt install wkhtmltopdf` (Ubuntu) or download from wkhtmltopdf.org (Windows/Mac). Convert a local file: `wkhtmltopdf input.html output.pdf`. Convert a URL: `wkhtmltopdf https://example.com output.pdf`. Options: `--page-size A4 --margin-top 20mm --margin-bottom 20mm --encoding utf-8`. Note: wkhtmltopdf uses an older Qt WebKit engine — it may not render modern CSS (CSS Grid, Flexbox) as accurately as Puppeteer.
- 4
WeasyPrint (Python, CSS print specification-compliant)
WeasyPrint implements the CSS Paged Media specification: `pip install weasyprint`. Convert: `weasyprint input.html output.pdf`. Via Python: `from weasyprint import HTML; HTML(filename='input.html').write_pdf('output.pdf')`. WeasyPrint understands @page CSS rules (size, margins, headers/footers) and handles @media print CSS correctly. It doesn't use a full browser engine — it's better than wkhtmltopdf for CSS3 but has some rendering differences from Chrome.
Why convert HTML to PDF?
HTML is live and interactive; PDF is frozen and portable. For invoices, certificates, reports, and archivable documents, PDF is the right output — and headless Chrome makes it pixel-perfect.
Frequently asked questions
Why doesn't my HTML look the same in PDF as in the browser?
How do I add headers and footers to the PDF?
Can I convert a website behind a login to PDF?
No account. No upload. Works in any browser.