PDF Conversion PDF conversion pdf html web

How to Convert HTML to PDF: Every Method Explained

Convert HTML pages and web content to PDF using browser print, online tools, and programmatic libraries for developers.

A
Admin
· May 26, 2026 · 6 min read · 2 views

Why Convert HTML to PDF?

HTML is designed for browsers — dynamic, responsive, fluid. PDF is designed for fixed layouts — print-ready, consistent, archivable. Converting between them bridges two fundamentally different document models.

Common reasons to convert HTML to PDF:

  • Save a web article or report as a permanent copy
  • Generate invoices and reports from a web application
  • Create printable versions of web pages
  • Archive web content before it changes or disappears
  • Produce certificates, tickets, or documents with a defined layout

Method 1: Browser Print to PDF (Free, Built-In)

Every modern browser can print any web page to PDF using the OS PDF printer.

Chrome / Edge:

  1. Open the page
  2. Ctrl+P (Windows) / Cmd+P (Mac)
  3. Under "Destination", click "Change" → select "Save as PDF"
  4. Set paper size, margins, and whether to include headers/footers
  5. Click "Save"

Firefox:

  1. Ctrl+P
  2. In the Printer dropdown, select "Microsoft Print to PDF" (Windows) or "Save to PDF" (Mac)
  3. Configure and save

Customising the print output:

  • Toggle "Headers and Footers" to remove page URL and date
  • Set margins (Default, None, Minimum, Custom)
  • Enable/disable Background Graphics (CSS background colours and images)

Limitations:

  • Page breaks may fall at awkward places
  • Interactive elements (dropdowns, JavaScript) don't transfer
  • Fixed headers/footers can duplicate or overlap
  • Responsive designs may not lay out well at print widths

Method 2: Browser Extensions

Chrome/Firefox extensions give more control over web-to-PDF conversion.

Print Friendly & PDF

Very popular extension. Strips ads, navigation, and clutter before converting to PDF. Lets you click to remove individual elements.

GoFullPage

Captures a full-page screenshot and converts to PDF — including content below the fold. Good for pages where the print dialog cuts off scrollable content.

Single File (Save as HTML, then PDF)

Saves the entire webpage as a single HTML file (with all CSS and images embedded inline), which you then convert to PDF separately.


Method 3: Online HTML to PDF Tools

For converting a URL to PDF without any software:

HTML2PDF (online tools)

Smallpdf, PDF2Go, and similar platforms have a "URL to PDF" feature. Enter a URL, they fetch the page and return a PDF.

Privacy note: The tool's server fetches the URL — not suitable for pages behind login or internal URLs.

Web Archive Tools

For archiving public web pages specifically: web.archive.org (Wayback Machine) saves the page. You can then print-to-PDF from the archived version.


Method 4: wkhtmltopdf (Open Source, Command Line)

wkhtmltopdf uses a headless Qt WebKit browser to render HTML and output PDF. It produces excellent results for static HTML pages.

Install:

  • Download from wkhtmltopdf.org
  • Linux: sudo apt install wkhtmltopdf

Convert a URL:

wkhtmltopdf https://example.com output.pdf

Convert a local HTML file:

wkhtmltopdf input.html output.pdf

Common options:

wkhtmltopdf --page-size A4 \
  --margin-top 20mm --margin-bottom 20mm \
  --margin-left 15mm --margin-right 15mm \
  --no-outline \
  input.html output.pdf

Page break control in CSS: Use page-break-before: always in CSS to force page breaks before specific elements.

Limitations:

  • Based on WebKit, so doesn't support all modern CSS3 features
  • JavaScript support is limited
  • Not maintained as actively as Puppeteer/Chrome alternatives

Method 5: Puppeteer (Node.js, Headless Chrome)

Puppeteer controls a real headless Chromium browser, giving you perfect HTML/CSS rendering including modern CSS and JavaScript execution.

Install:

npm install puppeteer

Basic script:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://example.com', { waitUntil: 'networkidle0' });

  await page.pdf({
    path: 'output.pdf',
    format: 'A4',
    printBackground: true,
    margin: { top: '20mm', bottom: '20mm', left: '15mm', right: '15mm' }
  });

  await browser.close();
})();

Convert local HTML:

await page.goto('file:///path/to/input.html', { waitUntil: 'networkidle0' });

Wait for dynamic content:

await page.waitForSelector('.chart-loaded');  // Wait until a specific element exists

CSS @media print support: Puppeteer respects @media print CSS rules, so you can design a print stylesheet that controls page breaks, hides navigation, and adjusts fonts for the PDF output.


Method 6: WeasyPrint (Python)

WeasyPrint is a Python library that converts HTML+CSS to PDF with excellent CSS support — better than wkhtmltopdf for modern CSS.

Install:

pip install weasyprint

Command line:

weasyprint input.html output.pdf

Python script:

from weasyprint import HTML

HTML('input.html').write_pdf('output.pdf')

# Or from URL:
HTML('https://example.com').write_pdf('output.pdf')

# Or from a string:
HTML(string='<h1>Hello World</h1>').write_pdf('output.pdf')

CSS Page Control: WeasyPrint fully supports CSS Paged Media — the CSS specification designed for print:

@page {
  size: A4;
  margin: 20mm;
}
h2 {
  page-break-before: always;
}

Best for: Generating PDF reports and documents from HTML templates in Python web applications.


Method 7: DOMPDF (PHP)

For PHP applications, DOMPDF converts HTML to PDF without external processes.

Install via Composer:

composer require dompdf/dompdf

Basic usage:

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Hello World</h1><p>This is a PDF.</p>');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('output.pdf');  // Send to browser
// OR: file_put_contents('output.pdf', $dompdf->output());

Best for: Laravel, Symfony, or plain PHP apps generating invoices, reports, or certificates.


CSS Tips for Better HTML-to-PDF Output

Regardless of the tool, these CSS techniques improve PDF quality:

/* Force page break before every h1 except the first */
h1 { page-break-before: always; }
h1:first-child { page-break-before: avoid; }

/* Prevent page break inside a table row or figure */
tr, figure { page-break-inside: avoid; }

/* Hide navigation and sidebars in print */
@media print {
  nav, aside, .no-print { display: none; }
  body { font-size: 12pt; }
  a[href]::after { content: " (" attr(href) ")"; }
}

Choosing the Right Tool

Use Case Recommended Tool
Save a web page for personal use Browser Ctrl+P → Save as PDF
Strip ads from a web article Print Friendly browser extension
Convert a URL to PDF (one-off) Online URL to PDF tool
Generate invoices in PHP DOMPDF
Generate reports in Python WeasyPrint
Complex web app with JavaScript Puppeteer (Node.js)
Static HTML with good CSS support wkhtmltopdf or WeasyPrint

Summary

HTML to PDF conversion covers a wide spectrum from simple browser print-to-PDF for personal use, to programmatic generation of complex business documents using Puppeteer, WeasyPrint, or DOMPDF. For developers, matching the tool to the stack (Node.js → Puppeteer, Python → WeasyPrint, PHP → DOMPDF) gives the best results. For non-technical users, browser print or a browser extension handles most everyday needs.