AutoRpa/rpa_contract/pdf_utils.py

32 lines
914 B
Python

"""合同 PDF 合并。"""
from __future__ import annotations
from pathlib import Path
def ensure_merged_pdf(source_paths: list[str], output_path: str) -> str:
"""Return one PDF path, merging multiple source PDFs when needed.
同供应商同日只上传一份 PDF。单文件直接返回路径。
"""
if not source_paths:
raise ValueError("YYC contract creation requires at least one platform PDF")
if len(source_paths) == 1:
return source_paths[0]
try:
from pypdf import PdfWriter
except ImportError as exc:
raise RuntimeError("Install pypdf to merge multiple YYC platform PDFs") from exc
writer = PdfWriter()
for source in source_paths:
writer.append(str(Path(source)))
out = Path(output_path)
out.parent.mkdir(parents=True, exist_ok=True)
with out.open("wb") as file:
writer.write(file)
return str(out)