第 7 章 模板格式参考

Last Updated: 2026-10-25 02:54 Views: 17

第 7 章 模板格式参考

7.1 概览与存储

一个模板 = 一个 JSON 文档 + 资源文件目录(UTF-8,无二进制 内嵌,图片等资源放旁边路径引用):

~/.wxprint/templates/<id>/
├── template.json      # 模板主文档(本规范)
├── waybill-scan.png   # 背景扫描底图(设计期参考)
└── stamp.png          # 模板自带图片资源
维度 套打模板(overlay) HTML 模板(html)
定位方式 元素绝对定位,毫米坐标 文档流排版(HTML+CSS)
适用 预印单据套印、面单、标签、票据 报表、对账单、说明书
数据代入 元素内 {{变量}} Twig 模板({{ }} / {% for %})
分页 每记录一页(按模板页定义) 按页高自动切片 + 分页类

模板三种来源:本地目录(server 启动扫描注册)、POST /api/templates 上传、打印时 URL 拉取(4.5 节)。

7.2 顶层字段

字段 类型 必填 说明
id string ✓ 模板唯一标识(目录名),^[a-z0-9][a-z0-9._-]{0,63}$
name string ✓ 显示名
kind string ✓ "overlay" / "html"
version int ✓ 格式版本,当前 1
page object ✓ 页面定义(7.3 节)
background object 底图与校准(7.9 节,仅 overlay)
elements array ✓ 元素列表(7.4 起,仅 overlay)
entry / assets string / array HTML 模板入口与资源(7.8 节)
calibration_offset object 模板级套打微调 {x, y} mm(叠加在打印机校准之上)
meta object 自由扩展(作者/备注/业务标识)

7.3 页面定义 page

字段 类型 默认 说明
width_mm / height_mm number — 物理纸宽/高,任意值(连续纸定长单据如 100×93.5;height_mm: 0 = 自适应内容高度,仅 overlay)
orientation string portrait portrait / landscape(交换宽高的便捷声明)
continuous bool false 连续走纸:页间无间隙,输出层按自定义纸型走纸
margin_mm object 0 {top, bottom, left, right}:overlay 仅作设计器安全区提示;html 为内容盒边距(页面保持整页)
size string — HTML 模板纸张预设("A4" 等,与 width/height 二选一)

坐标系:原点 = 打印纸左上角物理顶点;x 向右、y 向下;单位 毫米,最多两位小数(0.01mm 精度)。精度链路:模板坐标 → ×72/25.4 → PDF pt(精确有理换算)→ 1:1 不缩放输出 → 机械误差 由打印机校准吸收(7.10 节)。

7.4 元素通用字段

所有元素共有的字段:

字段 类型 默认 说明
type string ✓ text / line / rect / image / barcode / qrcode / table / chart
x, y, w, h number ✓ 元素框(mm)
rotate number 0 旋转角度(度,顺时针,绕框中心——斜排文字如银行回单)
id string — 元素标识(锚点/引用用);无 id 按下标引用
scope string body page-header / page-footer(每页重复绘制)、first-page / last-page(仅首页/末页)

7.5 text 文本

{ "type": "text", "x": 42.5, "y": 60.0, "w": 60, "h": 6,
  "text": "{{sender.name}} 收,{{sender.phone}}",
  "font": { "family": "", "size_pt": 10, "weight": 400, "italic": false,
            "color": "#000000", "letter_spacing_em": 0, "line_height": 1.2 },
  "align": "left", "valign": "top", "origin": "top-left", "overflow": "shrink" }
字段 类型 默认 说明
text string ✓ 内容:变量 + 过滤器 + 字面量;\n 换行
font.family string "" 空 = 系统默认栈(与 HTML 管线同栈);可写备选列表
font.size_pt number 10 字号(磅——印刷惯例,mm 坐标与 pt 字号并存是套打行业习惯)
font.weight int 400 字重;700 = 粗体
font.italic / font.underline bool false 斜体 / 下划线
font.color string "#000000" 前景色 #rrggbb
font.background string null 背景色 #rrggbb;null = 透明(不绘制背景)
font.letter_spacing_em number 0 字距(em 倍数)
font.line_height number 1.2 行高倍数(overflow: wrap 时生效)
align string left left / center / right(框内水平)
valign string top top / middle / bottom(框内垂直)
origin string top-left top-left = y 是框顶边;baseline = y 是首行文字基线——套表格线时字精确落在线内,不受字号行高微调影响(发票金额栏、凭证格子)
overflow string shrink shrink(推荐,二分缩字号至放得下,可设最小字号)/ clip / wrap

7.6 变量与过滤器

  • 语法 {{path}};点路径 {{a.b.c}},数组下标 {{items.0.qty}}。
  • 过滤器链 {{v|f1|f2:arg}}:
过滤器 示例 说明
date {{created_at\|date:"Y-m-d H:i"}} PHP/Twig 格式符(Y m d H i s),时间戳或 ISO 字符串入
number {{amount\|number:2}} 小数位 + 千分位;负参 = 仅小数位
upper / lower {{name\|upper}} 大小写
default {{remark\|default:"--"}} 空值兜底
truncate {{addr\|truncate:20}} 超长截断加 …
pad {{no\|pad:8}} 左补零到固定位(单号常用)
  • 内置页码变量 {{page}} / {{pages}}(当前页/总页数):仅 scope: page-header/page-footer 元素内有效。
  • 缺失变量:渲染为空串并在作业 warnings 中列出(4.7 节)。
  • 套打文本不做 HTML 转义(非 HTML 语境);HTML 管线默认转义、 |raw 放行。

7.7 line / rect / image / table / chart

{ "type": "line", "x": 10, "y": 55, "w": 190, "h": 0,
  "stroke_mm": 0.3, "color": "#000000", "dash": [2, 1] }
{ "type": "rect", "x": 10, "y": 15, "w": 190, "h": 35,
  "stroke_mm": 0.3, "fill": null, "corner_mm": 1 }
{ "type": "image", "x": 5, "y": 5, "w": 20, "h": 20,
  "src": "stamp.png", "fit": "contain" }
{ "type": "table", "x": 10, "y": 100, "w": 190,
  "columns": [
    { "header": "品名", "key": "name", "width": 0.5, "align": "left" },
    { "header": "数量", "key": "qty", "width": 0.2, "align": "right" } ],
  "rows": "{{items}}", "row_height_mm": 6, "header_rows": 1,
  "grid": { "stroke_mm": 0.2, "color": "#000000" },
  "font": { "size_pt": 9 } }
元素 字段要点
line stroke_mm 线宽、color、dash 虚线模式(如 [2,1])
rect stroke_mm 边框、fill 填充色(null = 透明)、corner_mm 圆角、边框色
image src:模板资源相对路径 / http(s) / data: URL(打印期拉取受大小/超时限制);fit:contain 等比 / stretch 拉伸
table columns[].width 为列宽占比(合计=1±0.001);rows 绑定数组;单元格支持变量与过滤器;斑马纹 odd_color/even_color;v1 固定行高不跨页(超行截断 + warning),跨页长表用 HTML 管线
chart kind:bar/hbar/line/pie/funnel/gauge/radar/scatter/histogram/candlestick/parallel/polar_bar/theme_river(13 种);categories/series 数据绑定;show_legend/title/theme

7.8 barcode / qrcode 与 HTML 模板

条码与二维码:

{ "type": "barcode", "symbology": "code128", "x": 10, "y": 20, "w": 80, "h": 12,
  "content": "{{order.no}}", "show_text": true, "text_size_pt": 7 }
{ "type": "qrcode", "x": 170, "y": 20, "size": 22, "content": "{{order.url}}",
  "ec_level": "M", "quiet_zone_mm": 2 }
字段 说明
symbology code128 / ean13 / ean8 / code39 / itf14 / upca;矢量路径绘制,无位图锯齿
w / h(条码)、size(二维码) 目标物理尺寸(mm);模块宽度自动取整到设备像素避免糊码
ec_level 二维码纠错:L(7%) / M(15%) / Q(25%) / H(30%)
quiet_zone_mm 二维码静区边距

HTML 模板(kind: "html"):

{ "id": "monthly-report", "kind": "html", "entry": "report.html.twig",
  "engine": "twig",
  "assets": ["styles.css", "logo.png"],
  "page": { "size": "A4", "margin_mm": { "top": 15, "bottom": 15,
            "left": 12, "right": 12 } },
  "sample_data": "sample.json" }
  • entry 为 Twig 模板:{{ }} 插值、{% for %} / {% if %} 控制、常用过滤器(date、number_format、nl2br、raw、 escape)与 include 函数。数据加载全部在服务端完成, 模板内无任何 JS(<script> 一律忽略)。
  • 分页控制:CSS 类 .wxprint-page-break(元素前强制断页)、 .wxprint-avoid-break(行级防孤行)。
  • CSS 支持现代子集:盒模型、表格、flex、float、字体/颜色/背景、 斑马纹等;不支持 @page 分页媒体(用分页类)。

7.9 底图校准(设计期)

底图仅在设计器内提供”描红”参照;校准建立像素→毫米映射后, 拖放坐标直接就是物理毫米。三种模式按扫描质量递进:

模式 输入 换算 适用
physical-size 扫描图实际物理宽/高 mm 等比 px_per_mm = img_px_w / width_mm 无歪斜的规整扫描
reference 图上一条已知物理长度的线段(两点) px_per_mm = 两点像素距 / 已知 mm 有已知尺寸参照物
affine-3pt ≥3 个点及其物理 mm 坐标 解 2D 仿射 6 参数(>3 点最小二乘) 歪斜/透视扫描纠偏
"background": {
  "image": "waybill-scan.png",
  "calibration": { "type": "affine-3pt", "points": [
    { "px": [18, 22],   "mm": [0, 0] },
    { "px": [2010, 25], "mm": [209.8, 0.3] },
    { "px": [21, 2831], "mm": [0.2, 296.5] } ] },
  "opacity": 0.6, "print": false }

print: false(默认)= 套打语义,底图不打印;true 仅用于 打样校对。校准结果连同原始输入存入模板,运行期不需要底图参与。

7.10 打印机校准(运行期)

每打印机校准(吸收机械重复性 ±0.1~0.3mm 的系统性偏差):

~/.wxprint/calibration/<printer>.json
{ "offset_mm": { "x": 0.35, "y": -0.20 }, "scale": { "x": 1.0002, "y": 1.0002 } }

来源:POST /api/printers/{name}/calibration-page 打印校准页(十字 规线 + mm 标尺网格)→ 量测偏差 → PUT /api/printers/{name}/ calibration 写入(端点见 4.10 节)。渲染前对全部元素坐标 p' = offset + scale·p。

渲染叠加顺序:模板坐标 → 打印机校准(全局)→ 模板级 calibration_offset({x, y} mm,个别单据对特定机的微调)。

7.11 校验规则(上传/注册时执行)

  • id 命名规则(^[a-z0-9][a-z0-9._-]{0,63}$);
  • 语义检查(Template::warnings):坐标/尺寸合法性、列宽合计、 未命中变量等,以 warning 列表随注册响应与作业返回(不阻断);
  • id 非法或目录创建失败返回 E_TEMPLATE_INVALID。

7.12 完整示例(快递面单,连续纸)

{
  "id": "sf-waybill-100x93",
  "name": "顺丰面单 100×93.5 连续纸",
  "kind": "overlay",
  "version": 1,
  "page": { "width_mm": 100, "height_mm": 93.5, "continuous": true },
  "background": {
    "image": "scan.png",
    "calibration": { "type": "physical-size", "width_mm": 100, "height_mm": 93.5 },
    "print": false },
  "elements": [
    { "type": "barcode", "symbology": "code128", "x": 8, "y": 6, "w": 60, "h": 10,
      "content": "{{waybill_no}}", "show_text": false },
    { "type": "qrcode", "x": 74, "y": 4, "size": 18, "content": "{{pickup_url}}",
      "ec_level": "M" },
    { "type": "text", "x": 8, "y": 20, "w": 40, "h": 5, "origin": "baseline",
      "text": "{{receiver.name}}  {{receiver.phone}}",
      "font": { "size_pt": 9, "weight": 700 } },
    { "type": "text", "x": 8, "y": 26, "w": 84, "h": 10, "overflow": "shrink",
      "text": "{{receiver.province}}{{receiver.city}}{{receiver.district}}{{receiver.addr}}",
      "font": { "size_pt": 9 } },
    { "type": "table", "x": 8, "y": 40, "w": 84, "row_height_mm": 5.5,
      "header_rows": 1,
      "columns": [
        { "header": "物品", "key": "name", "width": 0.6 },
        { "header": "数量", "key": "qty", "width": 0.2, "align": "right" },
        { "header": "保价", "key": "insured", "width": 0.2, "align": "right" } ],
      "rows": "{{items}}" },
    { "type": "text", "x": 8, "y": 88, "w": 50, "h": 4, "origin": "baseline",
      "text": "{{printed_at|date:\"Y-m-d H:i\"}} 寄件 {{sender.name}}",
      "font": { "size_pt": 7 } }
  ]
}
← Previous Article 第 6 章 命令式脚本 API 参考 Next Article → 第 8 章 打印设计器

Comments

No comments yet