Files
PDF-to-DWG-converter/app/input_document.py
T
Michael-A-SloanandCursor 2c9829ef8c feat(app): add image input support and bump to 1.5.0
Allow PNG/JPG/TIFF/BMP/GIF/WebP alongside PDF for preview and DXF conversion.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-03 17:05:17 -04:00

46 lines
1.1 KiB
Python

"""Open supported PDF and image inputs as PDF documents."""
from __future__ import annotations
from pathlib import Path
import fitz
PDF_EXTENSIONS = {".pdf"}
IMAGE_EXTENSIONS = {
".bmp",
".gif",
".jpeg",
".jpg",
".png",
".tif",
".tiff",
".webp",
}
SUPPORTED_EXTENSIONS = PDF_EXTENSIONS | IMAGE_EXTENSIONS
def is_image_input(path: str | Path) -> bool:
"""Return whether the selected input is a supported image."""
return Path(path).suffix.lower() in IMAGE_EXTENSIONS
def open_input_document(path: str | Path) -> fitz.Document:
"""Open a PDF directly or wrap an image in an in-memory PDF."""
input_path = Path(path)
extension = input_path.suffix.lower()
if extension not in SUPPORTED_EXTENSIONS:
supported = ", ".join(sorted(SUPPORTED_EXTENSIONS))
raise ValueError(f"Unsupported input type. Supported: {supported}")
document = fitz.open(str(input_path))
if extension in PDF_EXTENSIONS:
return document
try:
pdf_bytes = document.convert_to_pdf()
finally:
document.close()
return fitz.open("pdf", pdf_bytes)