How to convert HTML to PDF

Whether you have HTML code, an .html file or a web page, getting a tidy PDF comes down to three things: the right tool, the right page settings and a few lines of print CSS. Here is how to do each.

An HTML code page turning into a PDF document

To convert HTML to PDF, paste the code (or open the .html file) in the free HTML to PDF tool, pick A4 or Letter and your margins, and press Convert to PDF. It is printed by Chromium, the engine inside Google Chrome, so styles, web fonts and links come out as Chrome shows them. For a web page, switch to Web page address and type the address. For a page behind a sign-in, use your own browser’s Print › Save as PDF instead (steps below).

Which way should you use?

You haveBest wayWhy
HTML code or an .html file (invoice, report, email template)HTML to PDF, HTML code or fileNo need to open it in a browser first; paper size, margins and backgrounds in one place
A public web page or articleHTML to PDF, Web page addressOne step, choose the print version to drop menus on many sites
A page that needs your sign-in (bank, email, dashboard)Your browser: Print › Save as PDFOnly your browser has your accounts
Hundreds of pages from your own appHeadless Chrome or a PDF library in your codeAutomation; see the last section

Convert HTML code or an .html file online

  1. Open HTML to PDF. Paste your code into the box, or press Open .html file.
  2. Choose the Paper size (A4 for most of the world, Letter for the US and Canada) and Margins. Narrow (about 1 cm) suits invoices; Normal (about 2 cm) suits letters.
  3. Keep Print background colours and images on if your design has coloured headers, table stripes or a logo on a coloured band. Browsers leave backgrounds out when printing unless you ask for them.
  4. Pick the Look: As on screen, or Print version if your CSS has @media print rules.
  5. Press Convert to PDF. The file downloads with the same name as your .html file.

Pictures missing? Pasted code has no folder around it, so src="images/logo.png" can’t be found. Use full addresses (https://yoursite.com/images/logo.png) or embed the picture as a data: URL. Plain http:// addresses are blocked for safety, so use https://.

Save a web page as PDF in your browser

Every major browser can do this without any extra software, and it is the right choice for pages that need your sign-in:

  • Chrome: press Ctrl + P (Cmd + P on a Mac), set Destination to Save as PDF, open More settings to change paper size, margins and Background graphics, then press Save.
  • Edge: Ctrl + P, choose the printer Save as PDF, then Save.
  • Firefox: Ctrl + P, set the destination to Save to PDF; More settings has Print backgrounds.
  • Safari on Mac: File › Export as PDF, or File › Print and the PDF button.
  • iPhone and Android: open the share or menu button, choose Print, then save or share the preview as a PDF.

Many news sites hide menus and adverts when printed, so the browser PDF is often cleaner than the page looks. If the result has cut-off columns, try landscape.

CSS that makes HTML print well

If you write the HTML yourself (an invoice template, a report), a few lines of CSS decide whether the PDF looks designed or accidental:

GoalCSS
Set the paper size and margins in the file@page { size: A4; margin: 15mm; } (turn on Use the page’s own size in the tool)
Keep a table row, card or signature block on one pagetr, .card { break-inside: avoid; }
Start a section on a new page.chapter { break-before: page; }
Keep background colours even if the reader’s browser drops themprint-color-adjust: exact;
Hide buttons and menus in print only@media print { nav, button { display: none; } }
Repeat table headings on every pagePut the heading row in <thead>; Chromium repeats it

Use sizes in mm, cm or pt for print layouts; they come out the same on every paper size. Web fonts loaded from Google Fonts or your own https:// address are embedded in the PDF.

For developers: converting HTML to PDF in code

For one-off files, the online tool or the browser is quicker. To turn many pages into PDFs automatically, run a headless browser: Chrome itself can print a page with chrome --headless --print-to-pdf=out.pdf https://example.com, and libraries such as Puppeteer and Playwright (for Node.js and Python) call the same print engine with options for paper size, margins and backgrounds. Server tools such as Gotenberg wrap it as an HTTP service. Always check what addresses a server-side converter may load, so it can’t be used to reach your internal network.

Once you have the PDF, you can merge it with other PDFs, make it smaller or add page numbers. Converting the other way, our guide on converting Word to PDF covers documents from Office.

Frequently asked questions

Why are background colours missing from my PDF?

Browsers skip backgrounds when printing to save ink. Turn on “Print background colours and images” in the HTML to PDF tool, or “Background graphics” in Chrome’s print settings, or add print-color-adjust: exact to your CSS.

How do I stop a table row splitting across two pages?

Add tr { break-inside: avoid; } to your CSS. Put the heading row in a thead element and Chromium repeats it at the top of each new page.

Can I convert an HTML email to PDF?

Yes. In most email apps you can view or download the message source (or save it as .html), then paste or open it in the HTML to PDF tool. Pictures that come from https:// addresses load; attached pictures may not.

Are the links clickable in the PDF?

Yes. Chromium keeps links as clickable links in the PDF, and the text stays selectable and searchable.