Converting HTML to PDF is one of the most useful skills in a developer's toolkit. Whether you're generating invoices, creating printable reports, archiving web content, or building a documentation system, knowing how to reliably convert HTML to PDF saves time and produces professional results.
This guide covers every major approach: the simple browser method, online converters, and programmatic tools like Puppeteer, wkhtmltopdf, and Python libraries. We'll also dive into CSS for print, which is the secret to getting pixel-perfect results.
Why Convert HTML to PDF?
- Invoices and receipts: Generate professional PDF invoices from HTML templates with dynamic data
- Reports and dashboards: Export web-based reports as PDFs for sharing and archiving
- Documentation: Convert README files, wiki pages, and API docs to PDF format
- Archiving web content: Save web pages for offline reading or legal record-keeping
- Certificates and badges: Generate personalized PDF certificates from templates
- Email attachments: Convert HTML email templates to PDF for clients that can't render HTML
Method 1: Browser Print (Simplest)
Every modern browser can save any web page as a PDF. This is the quickest method and requires no tools:
- Navigate to the web page you want to convert
- Press Ctrl+P (Windows/Linux) or Cmd+P (macOS)
- In the print dialog, change the destination to "Save as PDF"
- Adjust settings (layout, margins, pages)
- Click Save
Browser Print Settings That Matter
- Margins: "None" for full-bleed designs, "Default" for standard documents
- Headers and footers: Uncheck to remove page numbers and URLs
- Background graphics: Enable to preserve background colors and images
- Layout: Portrait vs. landscape — auto-detected by @page CSS if set
- Scale: Default (100%) works best; adjust only for specific layout issues
Method 2: Online HTML to PDF Converters
Online converters work when you need to convert a URL or HTML snippet without installing anything:
- Visit the HTML to PDF converter
- Paste your HTML code or enter a URL
- Click convert and download the PDF
This approach is ideal for quick conversions, testing how your HTML will look as a PDF, or when you're working on a device where you can't install software.
Convert HTML to PDF right in your browser — no signup, no server upload.
Try HTML to PDF Converter →Method 3: Puppeteer (Best for Node.js)
Puppeteer is the gold standard for HTML-to-PDF conversion. It's a Node.js library that controls a headless Chrome browser, meaning it renders HTML exactly as Chrome does — complete with CSS Grid, Flexbox, web fonts, and JavaScript execution.
Basic Conversion
This was generated from HTML.
', 'output.pdf');Convert a URL to PDF
Advanced Puppeteer Options
waitUntil: 'networkidle0' to ensure all resources (images, fonts, async content) are fully loaded before generating the PDF. This prevents missing images and incomplete layouts.
Method 4: wkhtmltopdf
wkhtmltopdf uses the WebKit rendering engine to convert HTML to PDF. While older than Puppeteer, it's still widely used and very fast:
wkhtmltopdf vs Puppeteer
- Puppeteer uses current Chrome — better CSS support, more accurate rendering, handles modern web features
- wkhtmltopdf is faster and lighter — good for simple HTML, but uses an older WebKit engine with some CSS limitations
- For new projects, Puppeteer is the recommended choice
Method 5: Python Libraries
WeasyPrint (Recommended)
WeasyPrint is a Python library that converts HTML and CSS to PDF. It has excellent CSS support, including CSS Paged Media for precise print layout control:
Amount: $500.00
').write_pdf('invoice.pdf') # From URL HTML(url='https://example.com').write_pdf('page.pdf') # From local file HTML(filename='report.html').write_pdf('report.pdf')pdfkit (Python wrapper for wkhtmltopdf)
xhtml2pdf
Test your HTML as a PDF instantly — paste your code and see the result.
HTML to PDF Online →CSS for Print: The Key to Perfect PDFs
The most common problem with HTML-to-PDF conversion is that the PDF doesn't look right. The fix is almost always CSS. Here's how to control your PDF output:
@page Rules
Page Breaks
Print-Specific Styling
Common Problems and Solutions
- Missing images: Use absolute URLs or base64-encode images. For local files, ensure the tool has file access enabled.
- Fonts look different: Embed web fonts using @font-face with base64 or ensure the fonts are loaded before conversion.
- Page breaks in wrong places: Use CSS page-break properties to control where pages split.
- Content cut off at page edges: Adjust margins and use
overflow: hiddenwith caution — it can clip content. - JavaScript not executed: Only browser-based tools (Puppeteer, browser print) execute JavaScript. wkhtmltopdf and WeasyPrint do not.
Conclusion
HTML to PDF conversion is a well-solved problem with excellent tools at every level. For quick one-off conversions, online converters or browser print work perfectly. For production applications generating invoices, reports, or certificates, Puppeteer (Node.js) or WeasyPrint (Python) provide the control and reliability you need.
The key to professional results is CSS for print. Invest time in your @media print styles and @page rules, and your PDFs will look polished and consistent every time.