gitmyhub

boxpdf

TypeScript ★ 16 updated 25d ago

Tiny box-layout DSL over pdf-lib. Flexbox-lite for server-side PDF generation in Node, Cloudflare Workers, Deno, and browsers.

boxpdf is a TypeScript library for generating PDF documents using a layout system similar to how flexbox works in web design. Instead of calculating exact positions yourself, you describe the document as nested boxes — vertical stacks, horizontal stacks, text blocks, images, lines, and tables — and the library figures out where everything goes, including word wrapping and multi-page flow.

It is built on top of pdf-lib and runs in Node 18+, Cloudflare Workers, Deno, and browsers, with no native dependencies, no WASM, and no headless browser required. This makes it practical for server-side PDF generation in environments that restrict what code they can run.

The layout primitives accept style options familiar to anyone who has used CSS: padding, margin, gap, flex grow and shrink weights, justify and align options, borders, background colors, and corner radii. The text node handles word wrapping automatically when given a width, and truncates with an ellipsis when you set a maximum line count. A table primitive supports fixed, auto, and fractional column widths, repeating header rows across pages, and per-cell styling. Multi-page documents can include per-page headers and footers, and streaming generation keeps memory bounded regardless of page count.

Four built-in themes — cleanTheme, stripeTheme, editorialTheme, and brutalistTheme — provide pre-made color, spacing, and font settings. A CLI lets you scaffold single-file templates for receipts, boarding passes, resumes, order confirmations, and certificates. The core is approximately 7 KB minified, and the library ships a resource-only MCP server for use with AI agents. The project is written in TypeScript.