How to Convert HTML to PDF

· 5 min read

Creating PDFs from HTML is useful for generating invoices, reports, letters, certificates, and any document where you want to control the layout with CSS but distribute as a PDF. A browser-based HTML-to-PDF converter uses your browser's own rendering engine, so the result matches what you see in the live preview, with no server uploads.

How to convert HTML to PDF

  1. Paste your HTML: enter your HTML code including any inline CSS or <style> tags in the editor. The code can include full page structure with headers, tables, images, and styling.
  2. Preview the output: a live preview shows exactly how your PDF will look as you type. Adjust your HTML and CSS until the preview matches what you want.
  3. Generate and download: click the generate button to create the PDF in your browser, then download it instantly.

What you can create

A brief history of HTML-to-PDF conversion

In the early Web (1995-2005), creating a PDF from HTML required server-side tools: Apache FOP (1999), PrinceXML (2002), or wkhtmltopdf (2010), all of which ran on a backend server and required uploading content. The output quality varied wildly because each rendering engine implemented CSS differently.

Browser-side conversion became practical in 2014 with libraries like jsPDF and html2pdf.js, which use HTML5 Canvas to rasterize content. The result was decent for simple documents but failed on complex layouts and selectable text.

The breakthrough was Chromium's headless mode (2017), which exposed the same rendering engine Chrome uses for normal pages. Puppeteer (Node.js, 2017) made server-side Chromium PDF generation accessible to developers. Browser-based converters now use Print API (window.print()) with @page CSS rules, the same path Chromium's headless mode uses, giving identical output between preview and PDF.

In 2026, browser-based HTML-to-PDF is the right choice for most documents. Server-side rendering only makes sense for very large documents (100+ pages), automated batch processing, or when you need to merge PDFs from multiple sources.

Styling tips for PDF output

Use inline styles or <style> tags: external stylesheets are not loaded. Put all your CSS either inline on elements or in a <style> block in the HTML.

Set page margins: use CSS @page { margin: 20mm; } to control the whitespace around your content in the PDF.

Use print-friendly units: mm, cm, and pt are more predictable in PDFs than px or rem. Use mm for margins and spacing that need to match real-world dimensions.

Avoid viewport-dependent layouts: percentage widths and fixed pixel widths work best. Viewport units (vw, vh) may not behave as expected in PDF output.

Control page breaks: use page-break-before, page-break-after, and page-break-inside (or the newer break-before, break-after, break-inside) to control where new pages start. break-inside: avoid keeps a single block from splitting across pages.

Use @media print for PDF-only rules: any CSS inside @media print { ... } applies only when generating the PDF, not in the preview. Useful for hiding screen-only elements like navigation bars.

CSS print-mode example

@page {
  size: A4;
  margin: 20mm 15mm;
}

@media print {
  .no-print { display: none; }
  h1 { page-break-before: always; }
  table { page-break-inside: avoid; }
  a { color: black; text-decoration: none; }
}

body {
  font-family: 'Helvetica', sans-serif;
  font-size: 11pt;
  line-height: 1.4;
  color: #1a1a1a;
}

Page sizes

Common @page size values:

Add landscape for landscape orientation: size: A4 landscape.

Common pitfalls

Alternatives to consider

For everyday business documents (invoices, reports, certificates), a browser HTML-to-PDF converter is faster and just as polished.

Tips

Privacy and confidential documents

The HTML-to-PDF converter runs entirely in your browser. The HTML you paste, the images you embed, and the generated PDF all stay on your device. Nothing is uploaded to a server, logged, or shared with anyone.

This matters because HTML-to-PDF inputs are often sensitive: invoice data with client names, contract drafts with pricing terms, internal reports with financial figures, certificates with personal information. Cloud HTML-to-PDF services by design send your HTML to their server, generate the PDF there, and send it back. Some retain inputs for "improvement" or analytics. For documents containing confidential information, a browser-based converter is the safer choice.

Browser-based conversion also works offline once the page is loaded, and is fast enough for instant feedback as you iterate on the HTML.

Frequently Asked Questions

Does the PDF preserve my CSS styling?

Yes. The converter renders your HTML with CSS styling applied, including colors, fonts, margins, and layout. The PDF looks like the rendered web page, not the raw source code.

Can I include images in the PDF?

Yes. Use base64-encoded images (data URIs) for the most reliable results. External image URLs may work if they are accessible and CORS-enabled.

Is my HTML uploaded to a server?

No. The conversion happens entirely in your browser. Your code and the generated PDF never leave your device.

What CSS features are supported?

Standard CSS including layouts, colors, fonts, backgrounds, borders, and tables are well supported. Advanced features like CSS Grid, complex animations, and some Flexbox edge cases may have limited support.