@truedxf/pdf
v0.1.1
Published
PDF underlays for truedxf DXF drawings, rendered by PDF.js and placed exactly where AutoCAD puts them.
Readme
@truedxf/pdf
PDF underlays for truedxf DXF drawings, rendered by PDF.js and placed exactly where AutoCAD puts them.
truedxf never parses or draws PDF itself: PDF.js
(pdfjs-dist, Apache-2.0) renders the page and
@truedxf/render places the pixels like an IMAGE.
Install
pnpm add @truedxf/pdfESM only. Brings pdfjs-dist along as a dependency.
import { createPdfRasterizer } from "@truedxf/pdf";
import { renderPlot } from "@truedxf/render";
const pdf = createPdfRasterizer({ assetsUrl: "/pdfjs/" }); // keep one per app: it caches files
const result = await renderPlot(doc, request, { loadImage, pdf });@truedxf/viewer creates one by default (DxfViewer option pdf).
What it does
createPdfRasterizer(options)returns aPdfRasterizer(RenderEnv.pdf). PDF files are fetched through the renderer'sloadImage, like image files.- The renderer asks for each underlay's page through an affine transform at the target
resolution, so rotated and mirrored underlays are rendered straight into device pixels. Plots
rasterize only the part in view. Interactive lists cover the whole page, up to
maxSizepixels (default 4096) on the longer side. - PDF.js is imported on the first
open(): drawings without PDF underlays never load it. It parses in the calling thread (PDF.js "fake worker"), which in the viewer is already its worker, so no worker script URL has to be configured. - Browsers and workers draw into an
OffscreenCanvas. Node uses the legacy build of PDF.js and its canvas factory (@napi-rs/canvas, an optional dependency of pdfjs-dist). - Deterministic output: glyphs are drawn as paths (no FontFace, no system fonts), no GPU canvas.
assetsUrlis a folder with pdfjs-dist'sstandard_fonts/,cmaps/,wasm/andiccs/folders, copied from the package. Only PDFs with non-embedded standard fonts, CJK CMaps, JPEG 2000 / JBIG2 images or ICC colours need them. Without it, such content falls back or is skipped.
Contrast, fade, monochrome and the clip boundary are applied by the renderer, with the formulas fitted to AutoCAD's plots (note 07 §10.3).
Limitations
- No vector output: underlay content is a bitmap, so it cannot be picked, inspected or snapped
to. Deep zoom in the viewer is limited by
maxSize. - PDF.js antialiases fill edges and draws strokes at least 1 pixel wide, but AutoCAD's plot does neither (Q-0746).
- No per-underlay PDF layer (OCG) states: the PDF's default layer visibility is used.
- Encrypted PDFs (with a password) are reported as missing (
pdf-missing).
License
MIT. AutoCAD and DXF are trademarks of Autodesk, Inc. truedxf is not affiliated with or endorsed by Autodesk.
