How to Create a PDF From HTML and CSS (Print Styles That Work)

Learn how browsers turn HTML into PDF, which CSS print rules control page size, margins and page breaks, and how to generate PDFs from code by hand or at scale.

Updated September 29, 2026 5 min read

Use the free HTML to PDFTurn HTML and CSS into a print-ready PDF with real, selectable text

HTML and CSS are a surprisingly good way to make documents. Invoices, certificates, reports, event tickets and one-page flyers can all be written as a web page, styled with CSS you already know, and then saved as a PDF. The result has real, selectable text, crisp vector graphics and fonts, and none of the fiddly layout of a word processor.

This guide explains how HTML becomes a PDF, the CSS rules that control printed pages, how to convert a snippet or file in your browser, and the options developers use to generate PDFs automatically.

How HTML becomes a PDF

Every modern browser contains a print engine. When you print a page, the browser lays the HTML out again for paper instead of for a screen: it picks a page size, applies margins, splits the content across pages and applies any CSS written specifically for print. Choosing "Save as PDF" as the printer sends that paged layout to a PDF file instead of a physical printer.

Because the browser is doing the work:

  • Text stays text. It is selectable, searchable and sharp at any zoom.
  • Fonts are embedded as used, including web fonts that loaded successfully.
  • CSS controls the layout, including paper-specific rules that have no effect on screen.

Screenshot-based converters take a different route. They draw the page as an image and put the picture into a PDF. That looks the same everywhere, but text is not selectable and files are larger. For documents, the print engine approach is usually better.

CSS rules that matter for PDFs

A few CSS features do most of the work.

@page {
  size: A4;          /* or letter, or A4 landscape */
  margin: 15mm;
}

h2 { break-after: avoid; }            /* keep headings with their text */
table, figure { break-inside: avoid; } /* don't split across pages */
.new-page { break-before: page; }      /* force a new page */

@media print {
  nav, .no-print { display: none; }
  body { color: #000; }
}
  • @page sets paper size and margins. Browser support for every @page feature varies, so test your target browser.
  • break-before, break-after and break-inside (and the older page-break-* names) control where pages split.
  • @media print lets you hide navigation, buttons and backgrounds that make sense on screen but not on paper.
  • Background colours and images are skipped by many browsers when printing unless the user enables them, or unless you use print-color-adjust: exact where supported.

Convert HTML to PDF in your browser

Our HTML to PDF tool shows a live print preview of your code and uses your browser's own PDF printer, so the PDF keeps real text.

  1. Open HTML to PDF. Paste a full HTML page or just a fragment into HTML code, or click Open .html file (up to 5 MB). Click Sample to load an example invoice.
  2. Set Paper size (A4, US Letter, US Legal, A3 or A5), Orientation and Margins (None, Narrow, Normal or Wide).
  3. Turn Print background colours on or off depending on whether your design relies on coloured blocks.
  4. Enter a PDF file name / title.
  5. If your page builds its content with JavaScript, for example a chart, turn on Run the page's JavaScript. Scripts run in an isolated sandbox and are off by default.
  6. Check the preview, then click Save as PDF. In the print dialog, choose Save as PDF (or Microsoft Print to PDF) and switch off Headers and footers for a clean page.

If a mobile browser does nothing when you click Save as PDF, some block printing from embedded frames. Click Download .html and print the downloaded file from the browser instead.

Keep in mind that relative links to images or stylesheets (such as images/logo.png) cannot be resolved from pasted code. Use absolute URLs or embed small images as data URIs; the Image to Base64 tool can produce those.

Generating PDFs from HTML at scale

For one-off documents, a browser is the simplest route. When an application needs to create thousands of invoices, developers automate the same print engine.

Headless Chrome and Puppeteer

Chrome can print without a window:

chrome --headless --print-to-pdf=invoice.pdf invoice.html

In Node.js, Puppeteer or Playwright open a page and call a PDF method with options for format, margins, headers and footers. These give the same output as the browser's print dialog but can run on a server.

Dedicated HTML-to-PDF engines

Libraries and services built specifically for print CSS often support more of the paged-media specification, such as running headers and footers and page counters. They are worth evaluating if you produce books or complex reports.

Word processors

If you are more comfortable in Word, create the document there and use Word to PDF. For simple formatted text, Markdown to PDF is often quicker than writing HTML by hand.

Troubleshooting

Content is cut off on the right

Fixed widths wider than the printable area get clipped. Use percentage or max-width values and let tables shrink.

A table splits awkwardly

Add break-inside: avoid to rows (tr) or to the whole table if it is short. Repeat table headers by using a proper <thead>, which browsers typically repeat on each printed page.

Colours disappeared

Turn on Print background colours, or add print-color-adjust: exact to the elements that need it.

Date and URL appear at the top of the page

Those are the browser's print headers and footers. Switch them off in the print dialog's settings.

Messy code

Tidy the markup first with the HTML Formatter, and preview it quickly in the HTML Viewer.

Frequently asked questions

Will the text in my PDF be selectable?

Yes. The browser's PDF printer writes real text, so it can be selected, searched and copied.

How do I force a page break in HTML for PDF?

Add break-before: page (or page-break-before: always for older engines) to the element that should start on a new page.

Can I convert a web page by URL?

This tool works with pasted code or uploaded HTML files. To save a live website as PDF, open it in your browser and use Print, then Save as PDF.

Why are my images missing?

Relative image paths cannot be found from pasted code. Use absolute URLs or embed images as data URIs.

Does my HTML get uploaded?

No. The preview and PDF are produced by your own browser.

Try it now: HTML to PDF

Loading tool…