---
name: pdf-generation
description: Generate PDFs (reports, invoices) server-side. Use for printable/exportable documents.
---

# Skill: PDF Generation

## When to use

Invoices, reports, certificates — anything users print or archive.

## Approach

- **Node side:** queue a job (per `queue-worker`); render with a headless approach (Puppeteer for HTML→PDF, or pdfkit for programmatic).
- **.NET side:** QuestPDF (license-aware) for typed, code-defined layouts.

## Steps

1. Generation is async — enqueue, don't block the request.
2. Render from an HTML template (i18n-aware, token-styled) or a typed layout.
3. Store the result via `em-file-storage`; return a signed URL.
4. Record a `Document` row so it appears in the library.

## Gotchas

- Puppeteer needs Chromium in the worker image — add it to the worker Dockerfile (Phase 8).
- Thai fonts: bundle a Thai-capable font (e.g. Sarabun) or text renders as boxes.
- Large reports: stream/paginate; don't hold the whole doc in memory.
- Deterministic output for tests — fix the timestamp + locale.

## Validation

PDF renders with Thai text; stored + downloadable; generated off the request path.
