How to Convert HTML to PDF — Methods, Tips and Best Practices
You've just finished building an invoice template in HTML. Or a resume. Or documentation for a project. Now someone asks, "Can you send that as a PDF?" And suddenly you're searching for ways to convert HTML to PDF without losing your formatting.
Why Would You Need to Convert HTML to PDF?
It sounds like a niche problem until it happens to you. And then it keeps happening. Here are the situations that send people scrambling for an HTML to PDF converter:
- Invoices and receipts: You generate them dynamically in HTML, but clients want downloadable PDFs.
- Resumes: Built as a web page, but job portals only accept PDF uploads.
- Documentation: Your technical docs are in HTML, but the team needs offline PDFs.
- Reports: Dashboards render in HTML, but management wants printable summaries.
- Portfolios: Your design portfolio lives on the web, but agencies ask for a PDF version.
The common thread? HTML is great for screens. PDF is great for sharing, printing, and archiving. You need a bridge between the two, and that's exactly what HTML to PDF conversion provides.
The Three Main Approaches to HTML-to-PDF Conversion
Not all methods are equal. The right one depends on whether you need a quick one-off conversion or an automated pipeline. Let's break them down.
1. Browser Print-to-PDF (Simplest)
Every modern browser — Chrome, Firefox, Edge, Safari — has a built-in "Print" function that includes a "Save as PDF" option. This is the fastest approach for casual use.
You open your HTML in the browser, press Ctrl+P (or Cmd+P on Mac), select "Save as PDF" as the destination, and hit Save. Done. The browser's rendering engine handles all the CSS, layout, and formatting.
Our online converter uses this exact approach but adds convenience — you paste code directly, choose page sizes, and trigger the print dialog without manually opening files.
2. JavaScript Libraries (Developer Approach)
If you're building an application that needs to generate PDFs programmatically, JavaScript libraries like html2canvas + jsPDF, or Puppeteer (Node.js), are the go-to solutions. They render your HTML into a canvas element and then convert that canvas into PDF pages.
This approach gives you fine-grained control but requires coding knowledge and can struggle with complex CSS layouts like flexbox and grid.
3. Server-Side Engines (Enterprise)
Tools like wkhtmltopdf, Prince XML, or headless Chrome via Puppeteer run on a server and produce pixel-perfect PDFs. They're used by companies generating thousands of invoices or reports daily.
The trade-off? Setup complexity, server costs, and the fact that your HTML must be sent to a server for processing — which raises privacy concerns for sensitive documents.
Writing HTML That Converts to PDF Cleanly
Here's what most people get wrong: they write HTML for screens and then wonder why the PDF looks terrible. PDF has different rules. Here's how to write HTML that converts beautifully.
Use Print-Specific CSS
The @media print rule is your best friend. It lets you define styles that only apply when the page is printed or saved as PDF.
.no-print { display: none; }
body { font-size: 12pt; }
a { text-decoration: none; color: #000; }
.page-break { page-break-before: always; }
}
Hide navigation bars, sidebars, and interactive elements. Increase font sizes slightly for readability on paper. Remove colored backgrounds that waste ink.
Set Page Dimensions with @page
The @page CSS rule controls the PDF page size and margins directly:
size: A4 portrait;
margin: 20mm;
}
This ensures consistent output regardless of the browser's default print settings.
Avoid These Common Pitfalls
- Viewport-dependent layouts: CSS that relies on vw/vh units can produce unpredictable results in PDF. Use fixed units like mm, cm, pt, or px.
- External resources: Images hosted on CDNs, Google Fonts, or external CSS files may not load during conversion. Embed everything inline or use base64.
- Overflow content: Long tables or wide divs that overflow the page width will get clipped. Set max-widths and use page-break-inside: avoid on table rows.
- Background colors: Browsers don't print backgrounds by default. Add -webkit-print-color-adjust: exact to force background rendering.
Page Sizes Explained: A4 vs Letter vs Legal
Choosing the wrong page size is one of those mistakes that seems minor until you print the PDF and everything is cut off. Let's clear up the options.
| Page Size | Dimensions | Common Use | Region |
|---|---|---|---|
| A4 | 210 × 297 mm | Documents, invoices, resumes | India, Europe, Asia, most of the world |
| US Letter | 216 × 279 mm | Office documents, school work | United States, Canada |
| Legal | 216 × 356 mm | Legal contracts, long documents | United States (legal sector) |
If you're in India, go with A4. It's the standard for everything from government forms to corporate letterheads. If you're creating documents for US-based clients, ask whether they prefer Letter or Legal.
Portrait orientation works for text-heavy documents. Landscape is better for wide tables, dashboards, or presentation-style layouts.
Real-World Use Cases for HTML to PDF Conversion
🇮🇳 Vikram — E-commerce Seller, Pune
Vikram runs a Shopify store and generates shipping labels and invoices using HTML templates. Every day, he converts 15-20 invoices to PDF using a browser-based converter before printing them for packaging.
✅ Saves 2+ hours daily compared to manually creating invoices in Word
🇮🇳 Sneha — UX Designer, Bengaluru
Sneha builds her portfolio as a responsive HTML website. When agencies request her work samples, she converts her portfolio pages to A4 landscape PDFs to showcase her case studies with full design fidelity.
✅ Portfolio PDFs maintain exact colors and layout from her HTML designs
🇺🇸 Michael — Software Engineer, Austin
Michael writes API documentation in HTML using a static site generator. His enterprise clients require PDF documentation for their internal knowledge bases. He uses headless Chrome to batch-convert 200+ pages weekly.
✅ Automated pipeline generates PDFs from documentation commits via CI/CD
🇮🇳 Fatima — Teacher, Kolkata
Fatima creates HTML-based worksheets and question papers for her students. She converts them to PDF before printing so the formatting stays consistent across different printers in the school.
✅ Worksheets print identically on all printers thanks to PDF's fixed layout
CSS Properties That Matter Most for PDF Output
Not every CSS property works the same way in print. Here are the ones that have the biggest impact on your PDF quality:
page-break-before / page-break-after
These control where new pages start. Use page-break-before: always on chapter headings or section dividers to ensure they start on a fresh page. Use page-break-inside: avoid on elements you don't want split across pages, like table rows or image-caption pairs.
orphans and widows
These typographic properties control the minimum number of lines that must appear at the bottom (orphans) or top (widows) of a page. Setting both to 3 prevents awkward single-line fragments.
box-sizing
Always use box-sizing: border-box in your HTML. Without it, padding and borders can push content beyond the page boundaries, causing clipping in the PDF.
color-adjust
The print-color-adjust: exact (or -webkit-print-color-adjust: exact) property forces the browser to render background colors and images in the PDF. Without it, many backgrounds disappear.
Actionable tip: Create a dedicated print stylesheet that handles all these properties. Test it by printing to PDF before sharing your HTML template with anyone.
Common Mistakes and How to Fix Them
We've seen hundreds of people struggle with HTML to PDF conversion. Here are the mistakes that come up repeatedly — and the fixes.
Mistake 1: Images Don't Appear in the PDF
This happens when images are loaded from external URLs that block cross-origin requests. Fix it by converting images to base64 and embedding them directly in the HTML using data URIs.
Mistake 2: Fonts Look Different
If your HTML uses Google Fonts or other web fonts loaded via external URLs, they may not load during PDF conversion. Fix it by using system fonts (Arial, Helvetica, Times New Roman) or embedding font files as base64 in your CSS.
Mistake 3: Content Gets Cut Off on the Right
This usually means your content is wider than the page. Fix it by setting max-width on your main container and using percentage-based widths for columns. Avoid horizontal scrolling layouts entirely.
Mistake 4: Headers and Footers Overlap Content
Browser-generated headers and footers (page URL, date, page numbers) can overlap your content. In the print dialog, look for options to disable headers and footers, or increase your @page margins to accommodate them.
Mistake 5: Colors Look Washed Out
PDF rendering can sometimes reduce color vibrancy. Use the print-color-adjust property and test with actual printing to ensure your colors reproduce accurately.
Browser-Based vs Server-Based Conversion: Which Is Better?
This depends entirely on your use case. Here's a honest comparison:
| Factor | Browser-Based | Server-Based |
|---|---|---|
| Privacy | Excellent (nothing uploaded) | Depends on provider |
| Speed | Instant | 1-5 seconds (network dependent) |
| CSS Support | Full browser engine | Varies by engine |
| External Resources | Limited by CORS | Full access |
| Automation | Not practical | Fully automatable |
| Cost | Zero | Server/API costs |
| Batch Processing | Manual only | Thousands per minute |
Our recommendation: For individual documents, reports, resumes, and invoices — use a browser-based converter. It's instant, private, and costs nothing. For automated pipelines generating hundreds of PDFs — invest in a server-based solution.
HTML to PDF Conversion in Multiple Languages
HTML to PDF conversion is a universal need. Here's the concept in various languages:
Quick Tips for Perfect PDF Output
Before we wrap up, here are rapid-fire tips that'll save you headaches:
- Always test your print CSS with Ctrl+P before sharing any template.
- Use mm or pt units for print layouts instead of px or em.
- Keep your HTML simple — the more complex the CSS, the more likely something breaks in PDF.
- Embed all resources (images, fonts) inline if you want guaranteed rendering.
- Set explicit widths on tables to prevent them from overflowing the page.
- Use page-break-inside: avoid on elements that shouldn't split across pages.
- Remove all interactive elements (dropdowns, forms, videos) before converting — they serve no purpose in a PDF.
- Test on multiple browsers. Chrome and Firefox render print CSS slightly differently.
The bottom line? Treat your print stylesheet as a first-class citizen, not an afterthought. A few minutes of print CSS optimization saves hours of frustration later.
Convert Your HTML to PDF Now
Paste your HTML code, choose your page size, and generate a clean PDF in seconds — right in your browser.
Use the HTML to PDF Converter →Recommended Hosting
Hostinger
If you are building a website for your tools, blog, or store, reliable hosting matters for speed and uptime. Hostinger is a popular option used worldwide.
Visit Hostinger →Disclosure: This is a sponsored link.