When you export a residential floor plan for a contractor, cabinet shop, or local building department, a rasterized screenshot wrapped in a PDF fails the most basic test on site: putting a physical triangular architect's scale ruler on the printed page.
If a 20-foot exterior wall measures 4-15/16" instead of exactly 5.00" at 1/4" = 1'-0" scale, or if lines turn fuzzy when zoomed to 400% in Bluebeam Revu or Acrobat, contractors stop trusting the sheet immediately.
For PlumbDraft, we needed clean 2-sheet vector blueprint PDFs (Sheet A1.0 scaled floor plan + Sheet A1.1 room schedule, door/window schedules, material takeoff, and IRC/NKBA clearance audit) generated both in the browser and inside stateless Cloudflare Workers — with zero Puppeteer, zero headless Chromium, zero WASM binaries, and zero external PDF packages.
Here is how we wrote a byte-exact PDF 1.4 true-vector compiler in ~1,100 lines of pure TypeScript.
1. Exact US Architectural Scale in PDF Points (72 pt = 1 inch)
PDF user space is defined in PostScript points where 1 inch = 72 pt and the origin (0, 0) sits at the bottom-left of the sheet (unlike SVG/Canvas, which put (0, 0) at top-left):
- US Letter Landscape (
11" × 8.5"):MediaBox [0 0 792 612] - ANSI B Tabloid Landscape (
17" × 11"):MediaBox [0 0 1224 792]
Because our CAD model stores all wall endpoints in US architectural inches, mapping CAD inches to true printed PDF points at architectural scale is exact arithmetic:
- At
1/4" = 1'-0"(1:48ratio):12"in real life =0.25"on paper =18 pt. Therefore1 CAD inch = 1.5 pt. - At
1/8" = 1'-0"(1:96ratio, used for 40'×60' barndominiums):12"in real life =0.125"on paper =9 pt. Therefore1 CAD inch = 0.75 pt. - At
1/2" = 1'-0"(1:24ratio, used for 5'×8' bathrooms and 10'×12' kitchens):1 CAD inch = 3.0 pt.
Converting a CAD point (x, y) in inches to bottom-left PDF coordinates flips the Y axis around the drawing viewport center so a 20'-0" (240") wall renders at 360.00 pt (5.000 inches exact) at 1/4" scale — verifyable to the millimeter when printed at 100% / Actual Size.
(You can check whether your building footprint fits on ANSI B or US Letter at 1/4" vs 1/8" scale with our interactive 1/4" Scale Blueprint PDF Calculator.)
2. True Vector Geometry Primitives (Zero Raster Bloat)
Instead of importing a large PDF toolkit, a tiny PdfStreamBuilder emits standard PDF path operators directly:
-
m/l/S— moveto, lineto, stroke (wall poche outlines, witness lines, dimension ticks) -
re/B— rectangle fill+stroke (fixtures, cabinets, title blocks) -
cm— 2D affine transform matrix (cos sin -sin cos tx ty cm) so rotated furniture blocks, range burners, and door swing arcs stay crisp at any zoom - Cubic Bezier circles using
BEZIER_K = 0.5522847498for toilet bowls, round dining tables, shower drains, and window callout bubbles
Splitting exterior and interior walls cleanly around door and window jambs takes a single normalized interval pass (solidWallSpans) so wall fills stop cleanly at every opening without painting over door thresholds or window mullions.
3. WinAnsi Escaping and Built-in Type1 Fonts
Embedding custom TrueType font subsets bloats edge bundles and complicates glyph metrics. For technical architectural drawings, the 14 standard PDF Type1 fonts built into every viewer (Helvetica, Helvetica-Bold, Courier, Courier-Bold) are lightweight, legible, and guaranteed to render identically across macOS Preview, Adobe Acrobat, Chrome, and Bluebeam.
The only catch is sanitizing Unicode punctuation (’, “, —, ×) into clean WinAnsi ASCII and escaping PDF parentheses and backslashes before writing Tj string operands:
function pdfStr(input: string): string { const ascii = input .replace(/[’‘]/g, "'") .replace(/[“”]/g, '"') .replace(/[–—]/g, '-') .replace(/·/g, '|') .replace(/×/g, 'x') .replace(/[^\x20-\x7E]/g, ' '); return ascii.replace(/\\/g, '\\\\').replace(/\(/g, '\\(').replace(/\)/g, '\\)'); } 4. Computing Byte-Exact UTF-8 xref Offsets
The most common failure mode in hand-rolled PDF generators is calculating xref offsets with JavaScript string .length instead of encoded UTF-8 byte lengths. One stray multi-byte character shifts every subsequent object offset and causes strict PDF readers to warn about a corrupt cross-reference table.
Encoding every object upfront with TextEncoder makes the cross-reference table deterministic to the exact byte:
const header = new Uint8Array([ 0x25, 0x50, 0x44, 0x46, 0x2d, 0x31, 0x2e, 0x34, 0x0a, // %PDF-1.4\n 0x25, 0xe2, 0xe3, 0xcf, 0xd3, 0x0a // binary comment marker ]); const chunks: Uint8Array[] = [header]; const offsets: number[] = []; let bytePos = header.byteLength; for (const objBytes of objects) { offsets.push(bytePos); chunks.push(objBytes); bytePos += objBytes.byteLength; } const xrefLines = ['xref', `0 ${objects.length + 1}`, '0000000000 65535 f ']; for (const off of offsets) { xrefLines.push(`${String(off).padStart(10, '0')} 00000 n `); } Every 2-sheet residential blueprint we generate comes out around 12–28 KB — fast enough to compile in ~3 milliseconds in a browser tab or a Cloudflare Worker isolate with zero cold-start penalty.
5. Same Compiler for Browser CAD and AI Agents
Because exportBlueprintPdf(plan, { watermark }) returns a plain Uint8Array with zero Node.js or DOM APIs, it runs unmodified across three surfaces:
- Instant client-side downloads in PlumbDraft CAD Studio.
- Automated Vitest integrity checks verifying
%PDF-1.4,/Count 2,xref, and%%EOFon every template. - Our hosted Streamable HTTP MCP server (
https://www.plumbdraft.com/mcp) via theexport_blueprint_pdftool so AI coding and architecture agents can generate true-to-scale 2-sheet construction packets alongside 6-layer AutoCAD R12 DXF files.
Try It Live
- Interactive 2D/3D Floor Plan CAD Studio: https://www.plumbdraft.com/studio
- 1/4" Scale Blueprint PDF Fit Calculator: https://www.plumbdraft.com/tools/quarter-inch-scale-blueprint-pdf
- Hosted MCP Server & REST API Docs: https://www.plumbdraft.com/developers