PDF → CAD (SVG + DXF) Converter
Turn a PDF drawing into two deliverables:
- .svg — layered (Inkscape layers: Geometry / Text / Images), 1:1 real-world mm.
- .dxf — DXF R2010, mm units, 4 layers (Geometry / Hatch / Text / Images),
imports cleanly into AutoCAD, 中望, 浩辰, LibreCAD, and most AI CAD tools.
When to use
- User says "把这份 PDF 图纸转成 CAD/DXF/矢量图" or equivalent (zh/en).
- User wants editable vector geometry from a PDF (not a raster trace).
- User is prepping a PDF for AI-assisted CAD redraw / parametric editing.
Inputs
- The source PDF path — usually
@-referenced in the message or pasted as text.
If missing, ask the user for the PDF file. - Optional output directory (default: same folder as the PDF).
Prerequisites (one-time per environment)
Managed Python venv with pymupdf + ezdxf. Set this up once:
VENV="C:/Users/muxinshu/.workbuddy/binaries/python/envs/default"
PY="$VENV/Scripts/python.exe"
"$VENV/Scripts/pip.exe" install --quiet pymupdf ezdxf
If the venv doesn't exist yet, create it first:
"C:/Users/muxinshu/.workbuddy/binaries/python/versions/3.13.12/python.exe" \
-m venv "$VENV"
Steps
- Locate the PDF. Read the path from the user message. Resolve to an absolute path.
- Run the bundled converter (this skill's
convert.py). It does everything:
vector SVG export via PyMuPDF, layer reorganization, DXF build via ezdxf,
and the critical extents patch.
bash
VENV="C:/Users/muxinshu/.workbuddy/binaries/python/envs/default"
"$VENV/Scripts/python.exe" \
"C:/Users/muxinshu/.workbuddy/skills/pdf-to-cad/convert.py" \
"ABSOLUTE_PDF_PATH" [--out OUTPUT_DIR]
- Validate (quick). Confirm non-zero entity counts printed at the end
(polylines / hatches / texts / images). The script'sDXF extents patched
line must appear — that's what prevents a blank open. - Report. Tell the user the output paths and the key note below.
Outputs
- base.svg — layered vector, open in Inkscape/Illustrator.
- base.dxf — CAD-ready; must sit in the same folder as base_images/
(29-ish PNGs referenced by relative path). Move both together. - base_images/ — extracted raster images (photos / renders) referenced by the DXF.
CRITICAL gotchas (why a naive ezdxf export fails)
- Blank-on-open fix: ezdxf's auditor recomputes
$EXTMIN/$EXTMAXonsaveas
and fails for docs with many HATCH/IMAGE, resetting them to±1e20. CAD apps
frame the initial view from these → blank screen. The script PATCHES the ASCII
DXF after save with the real A4-range extents. Never remove that patch. - Chinese font:
doc.styles.add("SimSun", font=...)—font=must be a
keyword arg, else the style silently drops and text uses a missing font. - Mirrored images: PyMuPDF emits image transforms with negative determinant
(vertical flip).add_image(size_in_units, rotation)can't express a mirror, so
the script setsu_pixel/v_pixelvectors directly. - Orientation: SVG/PDF are y-DOWN; DXF is y-UP. Flip with
(H_PT - y).
Page heightH_PTis read dynamically from the PDF mediabox (don't hardcode). - Inherited paint: fill/stroke are often set on ancestor "g" groups, not the
path.resolve_paint()walks the ancestor chain; SVG default fill is black.
Skip(255,255,255)fills (background) to avoid a black sheet.
Notes
- Vector curves stay exact (Bézier flattened to polylines at 0.25 mm step in DXF).
- Raster photos are embedded as images, NOT traced — they remain raster by nature.
- If the user later reports "DXF blank", re-check the patched extents line first.