AutoRpa/rpa_contract/pdf_utils.py

47 lines
1.4 KiB
Python

# 合同 PDF 合并、提交占位正文。
from __future__ import annotations
from pathlib import Path
_PLACEHOLDER_PDF = Path(__file__).resolve().parent / "examples" / "placeholder_contract.pdf"
# 提交必须挂合同正文;没有平台文件时用这一份占位 PDF。
def ensure_placeholder_contract_pdf() -> str:
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:
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)