How to Convert HTML Code to PDF for Free
Convert a webpage, local HTML file, or raw markup to PDF for free with browser print, wkhtmltopdf, and ScreenshotNeo.
Short answer: If the HTML is already rendered in Chrome or Firefox, open the print dialog and choose Save as PDF. For a local file, open the .html file in a browser first. If you only have a raw snippet, save it as an HTML document, render it, then print it. For repeatable conversions, use the open-source wkhtmltopdf command-line tool.
Choose the right method
| What you have | Best free method | Why |
|---|---|---|
| An open webpage | Chrome or Firefox print dialog | No installation and easy preview |
A saved .html file |
Open it in a browser, then print to PDF | Uses the browser’s rendered layout |
| Raw HTML code in a message or editor | Save it as .html, open it, then print |
Browsers print rendered HTML, not an unrendered snippet |
| Many pages or an automated job | wkhtmltopdf or an API |
Repeatable from a script or build process |
Convert an open webpage to PDF in Chrome
- Open the webpage in Chrome.
- Choose File → Print, or press Ctrl+P on Windows/Linux or ⌘+P on macOS.
- Set the destination to Save as PDF.
- Choose the paper size, orientation, margins, scale, page range, and whether background graphics should print.
- Check the preview, then save the file.
Google documents this print workflow for webpages, images, and local files in Chrome Help.
Convert an HTML file to PDF in Firefox
- Open the
.htmlfile in Firefox. You can use File → Open File or drag the file into a browser window. - Open the application menu and choose Print….
- Choose Save to PDF as the destination.
- Review orientation, paper size, pages, scale, margins, headers and footers, and background printing.
- Save the PDF after checking the preview.
Firefox warns that print styling can make a page look different on paper than on screen. Its documented controls are described in Mozilla’s printing guide.
Convert raw HTML code to PDF
A browser print dialog works on a rendered document. If you have only markup, first save it as a file such as invoice.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Invoice</title>
<style>
@page { size: A4; margin: 18mm; }
body { font: 14px/1.5 Arial, sans-serif; color: #222; }
h1 { margin-top: 0; }
</style>
</head>
<body>
<h1>Invoice</h1>
<p>This HTML will be rendered before it is printed.</p>
</body>
</html>
- Copy the code into a plain-text editor.
- Save it with the
.htmlextension, for exampleinvoice.html. Ensure the editor does not append.txt. - Open the file in Chrome or Firefox.
- Use the browser’s Save as PDF workflow above.
If you want the source code itself to appear as text in the PDF, wrap it in escaped markup such as <pre><code> or use your editor’s print/export command. Saving a snippet as HTML and printing it produces the rendered page, not a code listing.
Control PDF page breaks with CSS
Print CSS can make a browser-generated PDF more predictable:
@page {
size: A4 portrait;
margin: 16mm;
}
@media print {
.screen-only { display: none; }
.avoid-break { break-inside: avoid; }
h1, h2 { break-after: avoid; }
.page-break { break-before: page; }
body { color: #000; background: #fff; }
}
Browser support and page layout can vary. Always inspect the print preview, especially for tables, flexbox layouts, fixed-position elements, and content loaded by JavaScript.
Automate conversion with wkhtmltopdf
wkhtmltopdf is an open-source command-line renderer that converts a URL or local HTML input into a PDF using Qt WebKit. Its project documentation covers paper size, orientation, margins, backgrounds, headers, footers, and related options. See the project page and usage manual.
wkhtmltopdf https://example.com/page.pdf output.pdf
For a local file:
wkhtmltopdf ./invoice.html invoice.pdf
Common layout options include:
wkhtmltopdf \
--page-size A4 \
--orientation Portrait \
--margin-top 16mm \
--margin-right 16mm \
--margin-bottom 16mm \
--margin-left 16mm \
--background \
https://example.com/report report.pdf
This renderer is useful for repeatable jobs, but it uses Qt WebKit. Modern sites that depend on newer browser APIs, complex JavaScript, or client-side rendering may need a current browser-based renderer instead.
What to check before you save
- Fit: Wide tables and long lines may shrink or paginate. Try landscape orientation, a larger paper size, or a different scale.
- Margins: Large margins can force unwanted page breaks; small margins can clip content.
- Backgrounds: Enable background graphics when shaded sections, charts, or colored headers matter.
- Page range: Select only the pages you need for a partial report.
- Print CSS: A site may intentionally hide navigation, ads, or interactive controls when printed.
- Dynamic content: Wait for charts, images, fonts, and API data to finish loading before opening print preview.
- Links and accessibility: Check whether the chosen workflow preserves selectable text, links, bookmarks, and tags if those are requirements.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Only a blank page appears | The file was not saved as valid HTML, or content is injected after load | Check the extension, open the file in a browser, and wait for scripts to finish. |
| Styles or colors are missing | Print backgrounds are disabled or print CSS overrides screen CSS | Enable background printing and inspect @media print rules. |
| Images do not appear | Images are lazy-loaded, blocked, or still downloading | Scroll through the page first, wait for loading, and verify image URLs. |
| Text is cut off | Fixed widths, overflow rules, or oversized content | Use landscape mode, reduce scale, remove fixed widths in print CSS, or split the content. |
| Page breaks split a table or card | Automatic pagination conflicts with the layout | Apply break-inside: avoid to the relevant block and preview again. |
| Fonts look different | The font was not available or had not finished loading | Wait for fonts, use a reliable fallback, or embed the font where licensing permits. |
| wkhtmltopdf shows an outdated or broken layout | The page relies on browser features Qt WebKit does not support | Use print-to-PDF in a current browser or a browser-based PDF service. |
Performance, reliability, and cost
Browser printing is usually the quickest one-off method because there is no setup. It still depends on the page being fully rendered and on your local browser settings. Command-line conversion adds installation and maintenance, but it is easier to repeat in a build or batch job. Neither method guarantees identical output for every JavaScript-heavy site, so keep a representative sample for regression checks.
Both browser printing and wkhtmltopdf are free to use in the workflows described here. For frequent remote captures, account for rendering time, retries, authentication, network failures, and the cost model of any service you choose. Do not assume a PDF is correct without checking page count, missing assets, and important values.
Or skip the browser setup
ScreenshotNeo provides a website capture API that can return a PDF as well as PNG, JPEG, or WebP. Use the same one-call pattern from the ScreenshotNeo API documentation:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
Configure PDF output and the other capture options in the documentation. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the result with X-Page-Verdict and X-Billed headers. Its MCP server lets AI agents use take_screenshot, get_page_info, and capture_pdf. The Free plan includes 1,000 screenshots each month with no card, and paid plans start at $5 for 3,000 shots.
Create a free ScreenshotNeo account to get started.
FAQ
How do I convert an HTML file to PDF for free?
Open the file in Chrome or Firefox, open Print, choose Save as PDF, review the preview, and save it.
Can I convert pasted HTML directly?
Save the snippet as an .html file and render it first. The browser workflow prints the rendered document, not raw markup.
Why does my PDF differ from the webpage?
Print styles, page size, margins, background settings, and unloaded dynamic content can change the result. Use print preview and adjust those settings.
Is wkhtmltopdf suitable for every website?
No. It is useful for repeatable jobs, but its Qt WebKit engine may not support features used by modern script-heavy sites.


