Files
WareShipManifest/core/print_document.py
Misaka_Company ee2fab3847 Add A4 carrier print mode (paper=a4) and document it in README + Swagger
- packing_list renders A5 content into top region of an A4 portrait page
  when paper=a4, so A5 paper loaded horizontally in an A4 tray prints upright
- run.generate/print_document/print_api thread the optional paper param through
- README: new Print Service section with A4 carrier usage + curl example
- Swagger: POST /api/print description documents the A4 carrier mode
2026-08-05 16:53:47 +08:00

72 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""打印编排:生成报表 PDF -> 送打印机。
复用现有 run.generate() 完成「查询→装配→渲染→PDF→页脚盖印」全链路
再调用 core.printing 把 PDF 静默打印到默认/指定打印机。
"""
from __future__ import annotations
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
# run.py 在项目根,生成逻辑已验证;此处直接复用,不改动 run.py
import run # noqa: E402
from core.settings import settings # noqa: E402
from core import printing # noqa: E402
VALID_REPORTS = ("packing_list", "sign_receipt")
def print_document(
report_type: str,
params: dict,
printer: str | None = None,
dry_run: bool = False,
paper: str | None = None,
) -> dict:
"""生成报表 PDF 并打印。
:param report_type: packing_list | sign_receipt
:param params: 报表参数(如 paichan_no/box_no 或 receipt_no
:param printer: 指定打印机名;为 None 时用 settings.printers.default
:param dry_run: True 时只构造打印命令不真正执行(用于验证链路)
:param paper: 可选纸张承载模式。packing_list 支持 "a4" —— 渲染到 A4 纵向页面顶部,
配合横向装载的 A5 纸正常出纸;其余报表/取值忽略。
:return: 结构化结果(含 pdf 路径、打印命令、ok 状态)
"""
if report_type not in VALID_REPORTS:
raise ValueError(f"未知报表类型:{report_type}(支持 {list(VALID_REPORTS)}")
# 1. 生成 PDFrun.generate 内部已做 required 参数校验与 DB 查询)
output = run._default_output(report_type, params)
# A4 承载模式的 PDF 用独立文件名,避免覆盖 A5 直打结果
if report_type == "packing_list" and paper == "a4":
output = output[:-4] + "_a4.pdf" if output.lower().endswith(".pdf") else output + "_a4"
out_path = run.generate(report_type, params, output, paper=paper)
# 2. 选打印机与纸张(装箱单 A5 横向 / 签收单 A4 纵向)
target_printer = printer or settings.printers.default
if report_type == "packing_list" and paper == "a4":
# A4 承载:物理发送 A4 纵向指令,横向装载的 A5 纸自动承接页面顶部 148mm 内容区
paper_settings = "paper=A4,orientation=portrait,color=bw"
else:
paper_settings = settings.print.settings_by_report.get(
report_type, settings.print.default_settings
)
# 3. 打印
result = printing.print_pdf(
pdf_path=out_path,
printer=target_printer,
settings=paper_settings,
sumatrapdf_path=settings.print.sumatrapdf_path,
dry_run=dry_run,
)
result["report_type"] = report_type
result["params"] = params
return result