← All guides

How to convert an HTML file to PDF locally (no upload)

The fastest local way to turn an .html file into a PDF is to open it in your browser and use File → Print → Save as PDF, which works offline and uses the browser's own rendering engine, so your CSS (flexbox, grid, gradients, custom fonts) renders exactly as it does on screen. For a repeatable or scripted conversion, a headless-browser library like Playwright or a CSS-focused engine like WeasyPrint does the same job programmatically. And if you want a drop-in tool that handles print CSS and page setup for you without installing anything, PDFMoka's HTML to PDF converter runs the conversion in your browser so the file is never uploaded. Below, each route and one important warning about an old tool you will see recommended everywhere.

Route 1: Browser print (simplest, fully local)

Any browser converts a local HTML file to PDF with no software to install:

  1. Open the .html file in your browser (double-click it, or File → Open).
  2. Press Cmd+P / Ctrl+P.
  3. Set the destination to Save as PDF.
  4. Set paper size, margins, and (importantly for styled pages) enable Background graphics so background colors and images print.
  5. Save.

This is free, works with no internet connection, and because it uses the browser's real layout engine, modern CSS renders faithfully, no third-party renderer to mistranslate flexbox or grid. The limitation is control: you get the browser's page-break behavior, and long or complex layouts may break across pages in ways you cannot fine-tune from the dialog. For most single files this is all you need.

If your styled page prints without its colors or highlighting, the fix is the Background graphics toggle plus print-color-adjust: exact in the CSS; see printing with colors and syntax highlighting preserved for the details.

Route 2: Scripted conversion (Playwright or WeasyPrint)

If you need to convert HTML to PDF repeatedly or as part of a build, use a library.

Playwright drives a headless Chromium and prints to PDF with full modern-CSS support (it is effectively the browser print method, automated):

pip install playwright
playwright install chromium
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("file:///absolute/path/to/your.html")
    page.pdf(path="out.pdf", format="A4", print_background=True)
    browser.close()

WeasyPrint is a Python engine that renders HTML/CSS to PDF without running a browser or executing JavaScript, a good fit when your document is static HTML/CSS (templates, reports) and you want accurate print layout without a headless browser:

pip install weasyprint
weasyprint input.html output.pdf

verify against current docs: the Playwright page.pdf() API (and print_background) and the WeasyPrint CLI are current, but run them once on your machine before relying on the exact syntax. WeasyPrint does not execute JavaScript, so if your HTML builds content with JS at runtime, Playwright is the right choice, not WeasyPrint.

Important: don't reach for wkhtmltopdf on a new project

You will find countless tutorials recommending wkhtmltopdf (and the pdfkit Python wrapper that calls it). Be aware before you install it: wkhtmltopdf was archived in January 2023 and is no longer maintained. It also carries a known serious SSRF vulnerability, which makes it a poor choice for converting untrusted HTML. It still works and still appears in many existing codebases, but for anything new the maintained alternatives are Playwright (if you need JavaScript execution) or WeasyPrint (for static HTML/CSS). We mention this because "just use wkhtmltopdf" is the single most common outdated advice for this task.

verify: confirm the current maintenance status yourself before publishing, project status can change. As of the last update to this guide, the repository is archived and the maintainers direct new users elsewhere.

Route 3: A local browser tool, nothing to install

If you do not want to open a print dialog and fiddle with settings, or install a Python library, convert your HTML file with PDFMoka. It renders the HTML with the browser's engine (so your CSS is faithful), lets you set page size and orientation, and applies sensible print CSS. It runs entirely in your browser, so the file is never uploaded, which matters when the HTML is an internal report, an email template with customer data, or a billing summary. For automated server-side generation at scale, that is Route 2's job; for a one-off or occasional local conversion, this is the least-friction option.

A safety note specific to this conversion: converting HTML means rendering it, and rendering untrusted HTML can execute scripts. PDFMoka sanitizes input and renders in a sandboxed frame so scripts in the source HTML do not run, which is the safe way to handle an HTML file you did not write yourself.

Which route?

  • One local file, right now: Route 1 (browser print → Save as PDF).
  • Repeatable or in a build pipeline: Route 2, Playwright (needs JS) or WeasyPrint (static HTML/CSS).
  • No install, no dialog fiddling, privacy matters: Route 3, convert it in your browser.
  • You were about to install wkhtmltopdf: don't, on a new project; use Playwright or WeasyPrint.

References