r/coolgithubprojects • • 2h ago

I built a pure-frontend DOM-to-PDF engine in TypeScript + Rust/WASM (no jsPDF, no headless browser, exports 10k vector pages in ~7.7s)

Post image

Hey everyone,

If you've ever had to export HTML to PDF purely on the frontend, you probably know the usual trade-off:

  • html2canvas + jsPDF essentially takes giant screenshots — blurry text, non-selectable, huge file sizes, and it freezes the main thread on long documents.
  • Puppeteer / Playwright gives great output, but requires maintaining a backend service and sending user data to a server.

I wanted a pure-frontend solution that produces real vector PDFs (searchable, selectable text and vector paths) without blocking the UI, so I built dompdf.js (MIT licensed).

How it works:

  1. Extracts DOM layout/geometry in TypeScript on the main thread.
  2. Offloads PDF compilation, pagination, and font subsetting to a Web Worker running Rust compiled to WebAssembly (no jsPDF dependency).

What it supports:

  • Smart multi-page pagination, dynamic headers/footers with page numbers
  • Custom TTF & CJK fonts with built-in font subsetting
  • SVG, Canvas, CSS styling, hyperlinks, text/image watermarks
  • Interactive PDF forms (AcroForm) and password encryption

Usage:

import dompdf from "dompdf.js";

const blob = await dompdf(document.querySelector("#content"), {
  format: "a4",
  pagination: true,
  backgroundColor: "#ffffff",
});

Benchmark & caveat: In the 10k-page demo, exporting 10,000 A4 pages (~17.5 MB) takes ~7.7s in Chrome. To be completely transparent, that benchmark is a plain vector-text ledger without heavy images or CJK font subsetting — real-world complex pages with lots of images/fonts will take longer depending on DOM depth and single-core CPU.

Links:

Would love to hear your thoughts, CSS edge cases that break it, or feedback on the API!

0 Upvotes

0 comments sorted by