AutoRpa/rpa_contract/pdf_utils.py

49 lines
1.5 KiB
Python

"""合同 PDF 合并、提交占位正文。"""
from __future__ import annotations
from pathlib import Path
_PLACEHOLDER_PDF = Path(__file__).resolve().parent / "examples" / "placeholder_contract.pdf"
def ensure_placeholder_contract_pdf() -> str:
"""提交必须挂合同正文;没有平台文件时用这一份占位 PDF。"""
path = _PLACEHOLDER_PDF
if path.is_file() and path.stat().st_size > 64:
return str(path)
path.parent.mkdir(parents=True, exist_ok=True)
from pypdf import PdfWriter
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with path.open("wb") as file:
writer.write(file)
return str(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)