Dev.to WebDev ๐Ÿ›  Dev ๐Ÿ‘ 0 ๐Ÿ“– 3 min read

Why html2canvas Makes Blurry PDF Invoices (And How to Render 19 KB Vector PDFs in 200ms with WeasyPrint)

If you have ever built an online invoice, receipt, or report generator, you have probably tried the classic frontend shortcut: render a DOM node with html2canvas and paste the resulting PNG into jsPDF. It works for a 5-

If you have ever built an online invoice, receipt, or report generator, you have probably tried the classic frontend shortcut: render a DOM node with html2canvas and paste the resulting PNG into jsPDF.

It works for a 5-minute prototype โ€” and then your first client tries to copy an invoice number or print it on A4 paper:

  • Blurry text on print and zoom (because it's a raster screenshot, not vector typography)
  • Unselectable text (accounting tools and PDF parsers can't read a single number)
  • Bloated 1.2โ€“2.5 MB file sizes for a 1-page invoice
  • Broken page breaks whenever a line-item table grows past one page

Here is how to switch to crisp, selectable 19 KB vector PDFs in under 200ms using Python, WeasyPrint, and CSS @page rules โ€” with zero server disk storage.

1. The Minimal Dockerfile (With Full CJK + Currency Support)

One common pain point with WeasyPrint in Docker is missing Cairo/Pango libraries or tofu boxes (โ–กโ–กโ–ก) when an invoice contains Japanese Yen (ยฅ), Chinese company names, or European VAT details.

Installing fonts-noto-core and fonts-noto-cjk in python:3.12-slim solves international typography cleanly:

FROM python:3.12-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    libcairo2 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libgdk-pixbuf-2.0-0 \
    libffi8 \
    shared-mime-info \
    fonts-liberation \
    fonts-noto-core \
    fonts-noto-cjk \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
RUN pip install --no-cache-dir fastapi uvicorn weasyprint jinja2

2. Proper Print CSS (@page + Table Pagination)

Unlike browser screenshots, WeasyPrint natively understands CSS Paged Media:

@page {
  size: A4;
  margin: 16mm 14mm;
  @bottom-right {
    content: "Page " counter(page) " of " counter(pages);
    font-size: 8.5pt;
    color: #64748b;
  }
}

thead { display: table-header-group; }
tr { break-inside: avoid; }

That thead { display: table-header-group; } rule guarantees that if an invoice spills onto page 2, the table header repeats cleanly instead of slicing a row in half.

3. Zero-Storage FastAPI Streaming Endpoint

Never write user invoices or client addresses to /tmp when you don't have to. Render straight to bytes in RAM and stream back:

from fastapi import FastAPI
from fastapi.responses import Response
from weasyprint import HTML

app = FastAPI()

@app.post("/api/render-invoice")
def render_invoice(html_doc: str):
    pdf_bytes = HTML(string=html_doc).write_pdf()
    return Response(
        content=pdf_bytes,
        media_type="application/pdf",
        headers={"Content-Disposition": 'attachment; filename="invoice.pdf"'},
    )

In production on an ARM64 Linux VM, a typical 1-page invoice renders in ~140โ€“190 ms and weighs ~19 KB (compared to ~1.4 MB from html2canvas), with 100% crisp vector glyphs.

4. When to Use WeasyPrint vs. Headless Chromium

Both engines belong in a developer's toolkit, just for different jobs:

Use Case Best Engine Why
Invoices, quotes, receipts, packing slips WeasyPrint < 200ms, tiny 19 KB vector PDFs, low RAM, native @page counters
Full web pages with React/Tailwind/Canvas/Charts Headless Chromium Executes JS, modern CSS Grid/Flexbox, exact browser viewport fidelity

Live Demos Built With This Pattern

If you want to inspect the output quality (try zooming to 400% or selecting text inside the generated PDF):

Full curl examples and toolkit index: GitHub โ€” snapkit-tools.

Are you still using Puppeteer/Playwright for transactional PDFs, or have you switched to WeasyPrint / Typst? Curious what everyone runs in production!

๐Ÿ“ฐ Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.