How to Convert HTML to PDF
You have an HTML file or webpage you need as a PDF. Reports, documentation, archives — many workflows need HTML → PDF conversion. Every browser does this natively, and several command-line and programmatic options exist for automation.
The fastest way: browser print
Every modern browser converts HTML to PDF through the print dialog:
- Open the HTML file in your browser (drag it in, or File → Open)
- Press Ctrl+P / Cmd+P
- Choose Save as PDF as the destination
- Click Save
Works for:
- Live webpages
- Local HTML files
- Any URL
This is the standard approach. Browser engines have great HTML rendering, so the PDF looks like what you see in the browser.
Configuring the output
Browser print dialogs offer settings:
- Paper size: A4, Letter, custom
- Orientation: portrait or landscape
- Margins: default, none, custom
- Scale: percentage to fit content
- Background graphics: include/exclude (default often excludes)
- Headers and footers: URL, date, page numbers
Common adjustments:
- Turn off headers/footers for clean output without URL/date stamps
- Enable “Background graphics” if your HTML has colored backgrounds
- Adjust scale if content doesn’t fit page width
Reader mode for clean output
Some pages have lots of clutter (ads, sidebars, navigation):
Safari: tap Reader View icon → then print to PDF.
Firefox: tap Reader View → then print.
Chrome: doesn’t have built-in reader mode; use a Reader extension.
Reader mode strips everything but the article content. The resulting PDF is much cleaner.
See our Webpage to PDF guide for more.
Command-line: wkhtmltopdf
For batch or programmatic conversion:
wkhtmltopdf (free, cross-platform): converts HTML files or URLs to PDF from the command line.
wkhtmltopdf input.html output.pdf
wkhtmltopdf https://example.com output.pdf
Options for paper size, orientation, margins, headers/footers all available via flags.
Excellent for:
- Server-side PDF generation
- Batch converting many HTML files
- Scripted workflows
Command-line: weasyprint
weasyprint (free, Python-based): another HTML → PDF converter with strong CSS3 support.
weasyprint input.html output.pdf
Better for CSS-heavy designs than wkhtmltopdf in some cases.
Programmatic: headless Chromium
For developers, headless browser approaches:
Puppeteer (Node.js): controls headless Chrome to render HTML and save as PDF.
Playwright: similar to Puppeteer with multi-browser support.
These match the browser’s exact rendering — what you see in Chrome is what you get in PDF.
For Markdown source
If your content is in Markdown rather than HTML:
Direct path: Markdown to PDF — converts directly without HTML intermediate.
Two-step path: Markdown to HTML, then HTML to PDF using browser print.
Direct is simpler. Two-step gives you a clean HTML version you might also want.
What gets preserved
Usually preserved:
- Text content
- Most CSS styling (when “Background graphics” is enabled)
- Images
- Basic tables
- Hyperlinks (clickable in the PDF)
Sometimes imperfect:
- Complex CSS layouts (CSS Grid, flexbox in older renderers)
- Web fonts (substituted if not embedded)
- Custom interactive elements (sliders, etc. become static)
Not preserved:
- JavaScript-driven interactivity
- Embedded videos as playable
- Forms (visible but not submittable in static PDF)
Page breaks
HTML doesn’t naturally have page breaks. The browser inserts them automatically. To control:
CSS print rules:
.page-break { page-break-before: always; }
Add class="page-break" to elements where you want forced breaks.
For HTML built for printing, page break controls give clean section breaks in the resulting PDF.
Headers and footers from HTML
For repeating headers/footers on each page:
CSS @page rules:
@page { @top-center { content: "Title"; } }
Supported in some tools (Prince XML, weasyprint) but not basic browser print.
For browser print: page number/header overlays are controlled via print dialog settings, not HTML.
File size
HTML → PDF file sizes vary:
- Simple text page: 50-200 KB
- Page with images: 500 KB - 5 MB
- Long HTML report with many images: 5-20 MB
For email-friendly files: PDF Compressor reduces size after conversion.
Specific use cases
Save documentation pages for offline: browser print → save as PDF.
Generate invoices from HTML templates: server-side wkhtmltopdf or Puppeteer.
Archive blog posts: browser print with reader mode for clean output.
Convert generated reports: wkhtmltopdf or weasyprint from automated pipelines.
Make printable versions of webpages: design HTML with print-specific CSS, then convert.
Browser differences
Different browsers render HTML slightly differently. The resulting PDF reflects that:
- Chrome/Edge: similar rendering (same engine)
- Safari: slightly different
- Firefox: slightly different again
For consistency, use the same browser for all conversions in a workflow.
CSS for print
When designing HTML you’ll convert to PDF, consider:
@media print {
/* styles specific to print/PDF */
body { font-size: 11pt; }
.no-print { display: none; } /* hide screen-only elements */
.page-break { page-break-before: always; }
}
This lets you have screen and print versions in one HTML file.
Privacy
Browser print runs locally; nothing uploads.
Command-line tools (wkhtmltopdf, weasyprint) run locally; nothing uploads.
Online HTML → PDF services upload your HTML/URL. Privacy concern for sensitive content.
TL;DR
- Easiest: browser → print → Save as PDF
- For clean output: use Reader View first (Safari/Firefox)
- Command-line: wkhtmltopdf or weasyprint for batch/server use
- Programmatic: Puppeteer or Playwright (headless browsers)
- For Markdown source: Markdown to PDF directly
- Print CSS (
@page,@media print) controls print-specific layout - For email: PDF Compressor after conversion