DEV Community

Cover image for Generating High-Quality PDFs in Django with Playwright (When Other Libraries Fall Short)
Gbengacode
Gbengacode

Posted on

Generating High-Quality PDFs in Django with Playwright (When Other Libraries Fall Short)

Generating High-Quality PDFs in Django with Playwright (When Other Libraries Fall Short)

PDF generation is one of those tasks developers often underestimate — until you try to render modern HTML, Tailwind CSS, external images, or embedded map screenshots. Suddenly, the limitations of traditional Python PDF libraries become painfully obvious.

Tools like xhtml2pdf, WeasyPrint, and wkhtmltopdf have served developers for years, but they were not built for today’s frontend ecosystem.

  • Tailwind CSS → ❌ ignored
  • CSS Grid → ❌ unsupported
  • Shadows & filters → ❌ ignored
  • External images → ❌ broken or inconsistent
  • Map screenshots → ❌ extremely unreliable

After months of struggling with inconsistencies in multiple Django projects, I eventually landed on the only tool that handles everything flawlessly:

Playwright + Headless Chromium = pixel-perfect PDFs

This article explains why Playwright is superior, how it fixes common rendering issues, and how to integrate it cleanly into a Django application.


🚫 The Problem: Traditional PDF Tools Can't Keep Up

❌ xhtml2pdf

  • No modern CSS support
  • Tailwind utilities are ignored
  • Images often break
  • Shadows, flexbox, grid, gap → unsupported
  • Random blank pages
  • Fonts fail silently

❌ WeasyPrint

Better, but still struggles with:

  • External HTTPS images
  • Advanced Tailwind utilities
  • Responsive layouts
  • Canvas/map rendering

❌ wkhtmltopdf

  • Based on very old WebKit
  • Lacks modern CSS features
  • Annoying server installation
  • Still produces layout inconsistencies

If your design uses:

  • Tailwind CSS
  • API-based images
  • Google Maps or static map screenshots
  • Custom fonts
  • Complex layouts

…then these older libraries simply cannot generate production-grade PDFs.


✅ Why Playwright Is the Ultimate PDF Engine

Playwright solves every rendering problem because it uses real Chromium under the hood.

✔ Full modern HTML + CSS support

  • Tailwind
  • Flexbox
  • Grid
  • Shadows
  • Rounded masks
  • Filters
  • Custom fonts

If Chrome can render it, Playwright can turn it into a PDF.

✔ External images load perfectly

Chromium fetches everything like a normal browser:

  • HTTPS images
  • CDN fonts
  • Map API images
  • Cloud-hosted assets

✔ Pixel-perfect output

What you see in the browser = what you get in the PDF.

✔ No mystery blank pages

Playwright does not guess page breaks — it renders the DOM exactly.


🏗️ Installing Playwright (Python)

pip install playwright
playwright install chromium
Enter fullscreen mode Exit fullscreen mode

🎯 Zero Blank Pages, Zero Layout Breaks

Because Playwright uses a real browser engine:

  • No unexpected page breaks
  • No broken Tailwind layouts
  • No ignored styles
  • No clipped or missing images
  • No random empty PDF pages

If it looks correct in Chrome DevTools, the final PDF will match exactly.


🌍 Perfect for Maps, Images, and External APIs

Playwright handles:

  • Google Static Maps API
  • Mapbox/Leaflet snapshots
  • Remote property images
  • API screenshots
  • CDN assets

Unlike WeasyPrint or xhtml2pdf, you do not need to:

  • Download images locally
  • Embed base64 manually
  • Use any special configuration

If Chromium can load it, your PDF will render it perfectly.


🔥 Real Project Example

A recent real estate platform needed to generate high-end marketing postcards containing:

  • Tailwind CSS layout
  • Agent photo in custom-shaped mask frames
  • Company branding
  • Dynamic market statistics
  • Map previews
  • High-quality fonts and gradients

Every old PDF library failed immediately:

  • Broken layouts
  • Ignored Tailwind classes
  • Missing shadows
  • Random blank pages
  • Incorrect image sizes

Switching to Playwright resulted in:

  • ✔ 100% accurate rendering
  • ✔ No blank pages
  • ✔ No missing images
  • ✔ Perfect brand fidelity

📌 Final Thoughts

If your PDFs require:

  • Modern HTML
  • Complex CSS
  • Tailwind
  • High-quality images
  • Map rendering
  • Exact layout control

Then:

Stop using xhtml2pdf, WeasyPrint, and wkhtmltopdf.

They simply cannot handle modern frontend features.

Start using Playwright.

It is stable, browser-accurate, and handles everything flawlessly.

After switching to Playwright, my Django PDF problems disappeared forever.

Top comments (0)