If you have ever built invoices, legal contracts, or multi-page reports in a serverless or Bubble.io app, you have probably hit the same two walls:

  1. Client-side canvas wrappers (html2canvas / jsPDF) blur vector text, break CSS Flexbox/Grid, and slice table rows right in half across page breaks.
  2. Serverless PDF APIs cold-start headless Chrome in 6–15 seconds, blowing past Bubble's hard 30-second server action timeout or costing $29–$59/month before you even launch.

We built DocEngine to solve both problems with a dual-engine, zero-retention architecture on Cloudflare Workers + Coolify Gotenberg.

Architecture Overview

Instead of forcing every document through one slow pipeline, we split rendering into three paths:

  1. Warm Edge HTML → PDF (~80ms warm / <1.6s cold):

    • Uses Cloudflare's REST /browser-rendering/pdf quick action as the primary engine with warm @cloudflare/puppeteer (keep_alive: 60_000) fallback.
    • Because HTML renders in real Chromium 130+, modern @page CSS rules work out of the box:
     @page { size: A4; margin: 18mm; } thead { display: table-header-group; } tr { break-inside: avoid; page-break-inside: avoid; } 
  2. Native Word .docx Template Merge → PDF (1.60s end-to-end):

    • Legal and finance teams hate rebuilding Word contracts in HTML.
    • DocEngine accepts any standard .docx file containing {customer_name}, conditionals {#has_discount}...{/has_discount}, and repeating table rows {#items}{name} | {amount}{/items}, merges the XML in memory, and converts to PDF via an isolated Gotenberg (LibreOffice) cluster behind a Cloudflare Zero Trust Tunnel (zero exposed host ports).
  3. Async Queue + Signed Webhooks for 50+ Page Reports:

    • Large jobs enqueue onto Cloudflare Queues with state coordinated by a SQLite Durable Object and return immediately (202 Accepted).
    • When rendering finishes, DocEngine uploads the PDF to Cloudflare R2 (with a mandatory 24-hour auto-expiry lifecycle rule) and fires an HMAC-SHA256 signed webhook (X-DocEngine-Signature) with an expiring signed download URL.

Hardening Against SSRF in Arbitrary HTML Templates

Rendering user-supplied HTML in headless Chromium is a classic SSRF vector (<img src="http://169.254.169.254/latest/meta-data">). Every render passes through a strict Go RE2 rejectRequestPattern firewall before any subrequest leaves Chromium:

rejectRequestPattern: [ "^https?://(localhost|127\\.|10\\.|169\\.254\\.|192\\.168\\.|172\\.(1[6-9]|2[0-9]|3[01])\\.|0\\.|\\[?(::1|fc|fd|fe[89ab]))" ] 

Combine that with JavaScript disabled by default, SHA-256 hashed API keys (sk_live_*) verified in constant time, and 24-hour R2 auto-deletion, and sensitive invoices or medical/HR documents never linger on disk.

Try It Live (No Signup Required)

We made the client-side plugin + 50 server renders/month completely free forever (with a $5/mo Pro or $49 one-time Lifetime tier if you scale to production).

Would love to hear how you handle PDF generation or .docx templating in your stack — drop your toughest layout edge case in the comments!