Converting Multi-Page PDF Floor Plans to Layered AutoCAD R12 .DXF in the Browser (PDF.js + TypeScript)
Homeowners, remodelers, and builders almost always start with the same file: a flattened PDF floor plan from a developer brochure, real estate listing, or older permit packet. Turning that PDF into an editable, true-scale AutoCAD .DXF normally means installing heavy desktop vectorization software, paying a monthly CAD subscription, or waiting two days for a manual redraw.
In TrueDraft CAD (open-source under AGPL-3.0 on GitHub), we built the full PDF-to-CAD pipeline directly in the browser using lazy-loaded pdfjs-dist, an orthogonal wall-graph model, single-dimension scale calibration, and a pure-TypeScript AutoCAD R12 .DXF compiler. Here are the concrete engineering details that made multi-page PDF floor plan conversion fast, memory-safe, and reliable on Mac, Windows, Chromebook, and Linux.
1. Lazy-Loading pdfjs-dist Only When a User Drops a .PDF
Shipping PDF.js in the initial studio bundle adds unnecessary startup weight for users drafting from scratch or starting from a starter template. Instead, src/lib/studio/TraceModal.svelte dynamically imports both pdfjs-dist and its worker URL only the first time a .pdf file is dropped or selected:
let pdfjsPromise: Promise<typeof import('pdfjs-dist')> | null = null; function loadPdfJs(): Promise<typeof import('pdfjs-dist')> { if (!pdfjsPromise) { pdfjsPromise = Promise.all([ import('pdfjs-dist'), import('pdfjs-dist/build/pdf.worker.min.mjs?url') ]) .then(([pdfjs, workerMod]) => { pdfjs.GlobalWorkerOptions.workerSrc = workerMod.default; return pdfjs; }) .catch((err) => { pdfjsPromise = null; throw err; }); } return pdfjsPromise; } Clearing pdfjsPromise = null inside .catch() prevents a transient network hiccup from poisoning retries for the rest of the session.
2. Race-Safe Multi-Page Sheet Rendering Without Memory Leaks
Architectural PDF packets routinely contain 8 to 20 sheets (cover page, site plan, Level 1 floor plan, Level 2 floor plan, elevations). Users click rapidly through page thumbnails until they find the floor plan sheet they actually want to trace.
Two bugs bite almost every naive PDF.js viewer:
- Out-of-order page paints: Page 2 finishes rendering after Page 4, overwriting the preview with the wrong floor.
- Canvas and PDF page memory leaks: Repeatedly calling
pdfDoc.getPage(n)withoutpage.cleanup()andloadingTask.destroy()spikes RAM quickly on Safari and Chromebooks.
Our renderer solves both with a monotonic pdfRenderToken, bounded viewport scaling (maxW = 1600, maxH = 2200, scale clamped to 0.5..4), an explicit opaque white fill (architectural CAD PDFs frequently have transparent backgrounds that look black if encoded straight to JPEG), and deterministic cleanup in finally and onDestroy:
async function renderPdfPage(pageNum: number): Promise<void> { if (!pdfDoc) return; const token = ++pdfRenderToken; pdfRendering = true; try { const page = await pdfDoc.getPage(pageNum); if (token !== pdfRenderToken || !pdfDoc) { page.cleanup(); return; } try { const baseViewport = page.getViewport({ scale: 1 }); const scale = Math.max( 0.5, Math.min(1600 / baseViewport.width, 2200 / baseViewport.height, 4) ); const viewport = page.getViewport({ scale }); const canvas = document.createElement('canvas'); canvas.width = Math.max(1, Math.round(viewport.width)); canvas.height = Math.max(1, Math.round(viewport.height)); const ctx = canvas.getContext('2d'); if (!ctx) throw new Error('Canvas 2D context unavailable.'); ctx.fillStyle = '#FFFFFF'; ctx.fillRect(0, 0, canvas.width, canvas.height); await page.render({ canvas, viewport, background: '#FFFFFF' }).promise; if (token !== pdfRenderToken) return; imageDataUrl = canvas.toDataURL('image/jpeg', 0.82); } finally { page.cleanup(); } } finally { if (token === pdfRenderToken) { pdfRendering = false; } } } Because rendering runs completely client-side inside the browser tab, multi-page permit PDFs never need to be stored on a server just to pick a sheet.
3. Why Single-Dimension Scale Calibration Beats Guessed Pixel DPI
A PDF exported from a real estate flyer rarely preserves a strict 1/4" = 1' physical scale after cropping or print-to-PDF margins. Rather than trusting PDF media-box points blindly, we keep internal CAD geometry strictly in meters and let the user calibrate against any known reference span (for example, an overall building width like 40'-0" or a standard 3'-0" exterior entry door).
Once traced walls and openings land on the 2D drafting grid, every wall stays live and parametric:
- Drag any corner node and connected walls re-miter cleanly.
- Toggle Split 2D + 3D to verify ceiling heights, door swings, and window sill elevations immediately in Three.js WebGL.
- Live room cycle detection recomputes net square footage and exterior/interior wall linear footage automatically.
4. Compiling Clean AIA-Layered AutoCAD R12 .DXF in Pure TypeScript
Opening a traced floor plan inside AutoCAD, LibreCAD, QCAD, BricsCAD, or Rhino only feels clean when every entity lands on standard architectural layers with true 1:1 model-space coordinates (inches for US imperial plans, millimeters for metric plans).
In src/lib/exporters/dxf.ts, we emit an ASCII AutoCAD R12 (AC1009) file with an explicit TABLES -> LAYER table covering standard AIA architectural layers:
| Layer | ACI Color | Entities Written |
|---|---|---|
A-WALL | 7 (White/Black) | Exterior wall boundary polygons and segments |
A-WALL-INTR | 8 (Dark Gray) | Interior partition walls |
A-DOOR | 30 (Orange) | Door frames, leaf lines, and 90-degree swing arcs |
A-GLAZ | 5 (Blue) | Window jambs, glazing bars, and sills |
A-FURN | 9 (Light Gray) | Architectural furniture and fixture blocks |
A-ELEC / A-PLMB | 2 / 4 | Electrical panels, outlets, and plumbing fixtures |
A-AREA | 3 (Green) | Closed room boundary polylines |
A-ANNO-DIMS | 1 (Red) | Dimension extension lines, tick marks, and text |
A-ANNO-TEXT | 7 (White/Black) | Room labels, area callouts, and title notes |
Writing ASCII R12 (AC1009) directly with explicit safeNum() finite checks and sanitizeDxfText() line-break stripping avoids binary handle corruption and opens cleanly across every desktop CAD package from AutoCAD 2026 down to lightweight open-source viewers like LibreCAD and QCAD.
Watch It in Action & Try It Free
Watch our 38-second walkthrough editing a 60x40 Barndominium and a 960 sq ft Backyard ADU in 2D and live 3D cutaway before exporting scaled Blueprint PDFs, AutoCAD .DXF, and watertight 3D-printable .STL scale models:
Try the PDF-to-DXF workflow free in your browser (zero signup required to draft, inspect in 3D, or run 2 free AI traces per 24 hours; optional one-time clean export unlock from $9 with zero subscription):
- Convert PDF Floor Plan to AutoCAD .DXF: truedraftcad.com/pdf-floor-plan-to-dxf
- Open Studio Trace Tool directly: truedraftcad.com/studio?trace=1
- Mac, Chromebook & Linux Browser CAD Guide: truedraftcad.com/floor-plan-software-for-mac
- Full Open-Source Repository (AGPL-3.0): github.com/truedraftcad/truedraft-cad