Skip to content

Packs / documents-public

pdf-generation

HTML/markdown to PDF on any machine (WeasyPrint or headless Chrome), page-break rules, merging PDFs, and checking the result.

Install

logicpacks pack use documents-public/pdf-generation
get_pack documents-public/pdf-generation

The second line is the MCP tool call for the LogicPacks connector (Claude Code, Codex, Claude.ai and others). The first run of a pack someone else wrote asks for your yes.

Keys it needs (names only)

None.

Playbooks included

  • Spec: spec/SPEC.md (goal, steps, edge cases, don’ts)
  • Routes: chrome-localweasyprint-localpandoc-local
  • Checks: 3 in outcome/checks.yaml
  • Code: merge_pdfs.py
  • Tools: python3

Usage, last 8 weeks

New: no recorded use yet.

Purpose (from spec/SPEC.md)

Goal

Reliable PDF output from HTML: right page size, no blank pages, dark backgrounds printed, verified page count and text.

Steps

  1. Prefer headless Chrome (../tools/html2pdf.py in.html out.pdf); WeasyPrint only if it imports cleanly (keeps margin-box footers).
  2. Page size via CSS @page { size: A4 | A4 landscape | 13.333in 7.5in }, margins in @page not body.
  3. One break rule per container (break-after: page), print-color-adjust: exact for backgrounds.
  4. Merge: python3 code/merge_pdfs.py out.pdf a.pdf b.pdf (pypdf).
  5. Verify with ../tools/check_doc.py out.pdf --pages MIN:MAX --expect TEXT (pdftotext or pypdf).

Edge cases

  • WeasyPrint missing or broken on the Mac: render.py falls back to headless Chrome (no 'Page X of Y' footer).
  • Blank pages in Chrome = the page break is declared twice in CSS; keep one break-after: page.
  • Tall screenshots overflow a page: images are capped at 215 mm high in the CSS.
  • --virtual-time-budget=8000 lets web fonts/images load before printing.
  • Chrome adds headers/footers unless --no-pdf-header-footer.

Don'ts

  • Don't rasterise text into images to 'fix' layout; the PDF must stay searchable.
  • Don't hotlink remote fonts/images in client PDFs; vendor them.