| name | pdf-to-cad-vectorizer |
| description | Convert engineering drawing PDFs and raster drawing images into CAD-friendly DXF files by preserving native PDF vector paths when available, including filled text outlines, or vectorizing raster line art from scanned PDFs, PNG, JPG, TIFF, BMP, and WebP images. Use when the user asks to turn PDF drawings, CAD-like PDFs, WPS-style PDF-to-CAD conversions, hollow/outlined PDF text, drawing linework, blueprint images, scanned drawings, or image files into DXF/DWG-ready CAD geometry. |
| metadata | {"short-description":"Convert PDFs/images to DXF"} |
PDF To CAD Vectorizer
Use this skill to convert engineering drawing PDFs or raster drawing images into DXF, especially when the user wants WPS-like PDF-to-CAD behavior where text may become linework or filled outline geometry instead of editable text.
Core Idea
- Prefer native PDF vector paths when
page.get_drawings() contains substantial line/path data. This gives the closest visual match and preserves linework, text outlines, signatures, tables, and fills.
- Use raster fallback when the PDF is mostly one or more embedded images, or when the input is PNG/JPG/TIFF/BMP/WebP. This traces black line art into outline DXF and centerline DXF outputs.
- For PDF text objects, convert them into graphic ink-run polylines instead of CAD
TEXT, avoiding missing Chinese fonts, question marks, misplaced editable text, and HATCH-filled square artifacts in enclosed Chinese characters.
- Output DXF as the primary artifact. DWG requires a separate DXF-to-DWG converter outside this skill.
Bundled Script
Script path:
scripts\pdf_to_dxf_vectorize.py
Use the active Python interpreter. In Codex Desktop, prefer the bundled workspace Python when available.
If imports fail, install into the active Python:
& '<python.exe>' -m pip install --user pymupdf ezdxf opencv-python-headless numpy
Workflow
- Inspect the PDF:
@'
import fitz
from pathlib import Path
pdf = Path(r'<PDF_PATH>')
doc = fitz.open(pdf)
for i, page in enumerate(doc):
drawings = page.get_drawings()
images = page.get_images(full=True)
words = page.get_text('words')
items = sum(len(d.get('items', [])) for d in drawings)
print(i + 1, page.rect, 'drawings', len(drawings), 'items', items, 'images', len(images), 'words', len(words))
'@ | & '<python.exe>' -
- If the PDF has many drawings/path items, run native vector conversion:
& '<python.exe>' '<skill_dir>\scripts\pdf_to_dxf_vectorize.py' '<PDF_PATH>' `
--out-dir '<OUTPUT_DIR>' `
--stem '<safe_name>' `
--native-only
This writes:
<safe_name>_native_vectorized.dxf
<safe_name>_native_preview.png
- If the PDF is mainly embedded images or scanned drawings, run full conversion:
& '<python.exe>' '<skill_dir>\scripts\pdf_to_dxf_vectorize.py' '<PDF_PATH>' `
--out-dir '<OUTPUT_DIR>' `
--stem '<safe_name>' `
--threshold 185 `
--epsilon 0.75 `
--min-area 3 `
--ink-width-scale 0.65
This writes native outputs plus:
<safe_name>_filled_vectorized.dxf
<safe_name>_filled_preview.png
<safe_name>_ink_vectorized.dxf
<safe_name>_ink_preview.png
<safe_name>_outline_vectorized.dxf
<safe_name>_outline_preview.png
<safe_name>_centerline_vectorized.dxf
<safe_name>_centerline_preview.png
Use ink DXF first when the user wants the CAD drawing to look like the image/PDF, with black filled text and heavier line regions, especially if HATCH-based output appears hollow in their CAD software. Use filled DXF when HATCH display is acceptable. Use centerline DXF when the user wants selectable/editable linework. Use outline DXF when preserving stroke boundaries matters more.
- If the input is a standalone image, run the same script directly on the image path:
& '<python.exe>' '<skill_dir>\scripts\pdf_to_dxf_vectorize.py' '<IMAGE_PATH>' `
--out-dir '<OUTPUT_DIR>' `
--stem '<safe_name>' `
--threshold 185 `
--epsilon 0.75 `
--min-area 3
For images, the script creates an intermediate single-page PDF in the output folder and writes:
<safe_name>_outline_vectorized.dxf
<safe_name>_outline_preview.png
<safe_name>_centerline_vectorized.dxf
<safe_name>_centerline_preview.png
<safe_name>_filled_vectorized.dxf
<safe_name>_filled_preview.png
<safe_name>_ink_vectorized.dxf
<safe_name>_ink_preview.png
<safe_name>_image_source.pdf
If the image has no useful DPI metadata, the script assumes 96 DPI. To set CAD page scale explicitly, pass --image-width-mm <width> and/or --image-height-mm <height>. Use one known dimension later in CAD to scale to exact engineering size when needed.
Use --ink-width-scale to control the visual thickness of *_ink_vectorized.dxf: 1.08 is the original dense fill, 0.65 is a thinner practical default, and 0.45-0.55 can be used when the CAD view still looks too heavy.
When the user says the filled raster result is too thick but should stay solid, first lower --threshold to 135-160 to exclude gray antialias edges before vectorizing. This usually makes strokes thinner without breaking small text. Use --raster-thin-pixels 1 only as a stronger fallback; it can break small characters and dashed details.
Validation
Always verify generated DXF with ezdxf:
@'
import ezdxf, os
from collections import Counter
p = r'<DXF_PATH>'
doc = ezdxf.readfile(p)
auditor = doc.audit()
print(Counter(e.dxftype() for e in doc.modelspace()))
print('layers', [layer.dxf.name for layer in doc.layers])
print('errors', len(auditor.errors), 'fixes', len(auditor.fixes), 'size', os.path.getsize(p))
'@ | & '<python.exe>' -
Open or display the preview PNG before final response. If CAD Explorer/render is available for DXF, hand off the DXF for viewer links; otherwise report the preview and audit result.
Practical Notes
- The output page uses millimeters with
INSUNITS=4. For raster PDFs and images, this is page/image scale, not necessarily the engineering dimension scale; users may need to scale by a known dimension.
- If CAD shows hollow text, use the
*_native_vectorized.dxf generated by the current script and ensure PDF_NATIVE_FILLS is visible. In AutoCAD-like tools, set FILLMODE=1 and run REGEN.
- Native vector output uses layers:
PDF_NATIVE_PATHS, PDF_NATIVE_RECTS, PDF_NATIVE_FILLS, PDF_TEXT_GRAPHICS, and PDF_PAGE_FRAME. It should not contain TEXT or MTEXT unless explicitly requested.
- Raster fallback output uses layers:
RASTER_FILLS, RASTER_FILL_OUTLINES, PDF_CENTERLINES, PDF_VECTOR_OUTLINES, and PDF_PAGE_FRAME.
- For image-to-CAD requests, recommend
*_ink_vectorized.dxf first for visual fidelity if the user is seeing hollow text/lines. Then offer *_centerline_vectorized.dxf for editing.
- Avoid claiming semantic CAD recovery. This process preserves visual geometry; it does not infer original CAD layers, constraints, blocks, dimensions, or parametric intent.
Final Response
Include:
- DXF path(s), with the recommended one first.
- Preview PNG path or rendered screenshot.
- Whether the source was native vector or raster/image-based.
- Entity counts and DXF audit result.
- Any scaling caveat.