"""产品推介材料 PDF 转换适配器。""" import subprocess from pathlib import Path class PromotionPdfAdapter: """首版只在配置启用且本机存在转换器时转换,不伪造 PDF 成功状态。""" def __init__(self, *, enabled: bool = False, converter_path: str = "") -> None: self.enabled = enabled self.converter_path = converter_path def convert(self, pptx_path: str | Path, output_path: str | Path) -> str | None: if not self.enabled or not self.converter_path: return None source = Path(pptx_path).resolve() target = Path(output_path).resolve() if not source.is_file(): raise RuntimeError("PPTX 文件不存在,无法转换 PDF") target.parent.mkdir(parents=True, exist_ok=True) completed = subprocess.run( [ self.converter_path, "--headless", "--convert-to", "pdf", "--outdir", str(target.parent), str(source), ], capture_output=True, text=True, check=False, ) generated = target.parent / f"{source.stem}.pdf" if completed.returncode != 0 or not generated.is_file(): raise RuntimeError("PPTX 转 PDF 失败") if generated != target: generated.replace(target) return str(target)