1. 文档介绍
本文档详细说明基于 JSON 格式的两种打印排版规则:票据打印(如小票、收款)和 标签打印(如商品标签、物流标签)。通过定义标准化的 JSON 属性,可灵活控制打印内容的类型、样式、位置等参数,适用于热敏打印机、标签打印机等设备。
注意事项:
- 图片类(png/bmp)需遵守大小限制:png 不超过 60K,bmp 不超过 6K
- 条码类(bc128/CODE128)需遵守字符规则:CODE128A 支持 14 个数字/大写字母,CODE128C 仅支持纯数字
- 坐标单位:标签打印中 1mm = 8 dot(dot 为打印机最小像素单位)
2. JSON 票据打印排版
票据打印适用于小票、收款等场景,支持标题、文本、图片、条码、二维码、表格、分割线等元素,核心通过 JSON 属性定义打印内容和样式。
2.1 核心属性一览表
| 属性名 | 值类型 | 取值范围/说明 | 适用场景 |
|---|---|---|---|
| type | string |
|
所有打印元素 |
| align | string | left(居左,默认)、center(居中)、right(居右) | text、qrcode、bc128、图片 |
| bold | boolean | true(加粗)、false(不加粗,默认) | text |
| size | string |
|
text、表格表体、qrcode、bc128 |
| thead_size | string | 可选。格式与表格 size 相同("AB")。仅作用于多列表头;省略时表头与表体共用 size | 多列表头字号 |
| line_space | number | 表格行间走纸点数 0-255,单位 dot,约 8 点 = 1mm。备注与商品行之间用 note_space | 多列表格 |
| line_div | number | 1:每行前打印 '-' 分割线;0 或不写:不打印。对象行可单独覆盖 | 多列表格 |
| both_sides | array | 数组两项:左对齐、右对齐。元素可以是字符串,也可以是对象 字符串:["收款金额","258.00"],整行共用 size / bold 对象:{"t":"¥133.20","size":"11","bold":true},该侧单独设字号/加粗 如 {"both_sides":["应收", {"t":"¥133.20","size":"11","bold":true}]} 未写的样式继承本行 size / bold |
左右对齐文本(如金额显示) |
| cont | string/array |
|
所有打印元素 |
| thead | array/object |
|
多列表格(表头) |
| tbody | array |
|
多列表格(内容) |
| col_align | array | 可选。每列对齐方式,取 left / center / right,如["left","center","right","right"] 未指定的列沿用默认规则:末列右对齐,其余左对齐 |
多列表格 |
| overflow | string | wrap:超宽折行(默认);clip:超宽截断;ellipsis:超宽截断并以 .. 结尾 可在表级或单元格级指定 |
多列表格 |
| underline strike |
boolean/number |
|
text、多列表格 |
| feed | type | 走纸元素。n:走纸点行数 1-255(约 8 点 = 1mm);lines:走纸行数 1-255;都不写按 n=1 如 {"type":"feed","n":24} |
区块之间的纵向留白 |
| hri | number | 条码字符位置:0 不显示,1 上方,2 下方(默认),3 上下都显示 | bc128、code39 |
备注:
- png 图片建议为黑白图片,图片宽度像素不超过目标打印宽度最大点数(80mm 对应最大 576 像素,58mm 最大对应 384 像素,110mm 最大对应 832 像素)。
- bmp 只支持位深度为 1 的黑白图片。
- 如图片内容为固定的(如品牌 logo 等),推荐传 URL 链接方式:打印机有类似浏览器的缓存策略,首次打印下载后,如 URL 不变,后续打印直接使用本地缓存图片,省去下载环节。
2.2 分场景示例
完整票据(全貌)
餐饮结账单,覆盖表格备注、行内混排、左右对齐、二维码、条码、走纸和切刀。
{
"print": [
{"type": "title", "cont": "云味小厨"},
{"type": "text", "cont": "堂食结账单", "align": "center", "size": "01"},
{"type": "feed", "n": 8},
{"type": "text", "cont": "桌号 A12 人数 4"},
{"type": "text", "cont": "单号 20260920-0087"},
{"type": "text", "cont": "开单 2026-09-20 12:18 收银 王小明"},
{"type": "div_line"},
{
"size": "00",
"thead_size": "00",
"line_space": 12,
"col_align": ["left", "center", "right", "right"],
"thead": {"菜品": "42%", "数量": "16%", "单价": "20%", "小计": "22%"},
"tbody": [
{"note": "—— 凉菜 ——", "note_align": "center"},
["拍黄瓜", 1, "12.00", "12.00"],
{
"cols": ["口水鸡", 1, "32.00", "32.00"],
"note": "备注:少放花椒",
"note_indent": 2
},
{"note": "—— 热菜 ——", "note_align": "center"},
["可乐鸡翅", 1, "38.00", "38.00"],
{
"cols": ["水煮鱼", 1, "58.00", "58.00"],
"note": ["备注:微辣、不要香菜", "做法:鱼头单独装盘"],
"note_indent": 2,
"note_space": 6
},
{
"cols": [
{"t": "地三鲜(已退)", "strike": true},
1,
"26.00",
{"t": "-26.00", "strike": true}
]
},
["米饭", 4, "2.00", "8.00"]
]
},
{"type": "div_line"},
{"both_sides": ["菜品合计", "¥148.00"]},
{"both_sides": ["会员折扣 9折", "-14.80"]},
{"type": "feed", "n": 6},
{"type": "text", "align": "right",
"cont": [{"t": "应收 "}, {"t": "¥133.20", "size": "11", "bold": true}]},
{"type": "feed", "n": 6},
{"both_sides": ["微信支付", "133.20"], "bold": true},
{"type": "div_line"},
{"type": "text", "align": "left",
"cont": [{"t": "本单积分 "}, {"t": "133", "bold": true}, {"t": " 累计 "}, {"t": "3560", "bold": true}]},
{"type": "feed", "n": 8},
{"type": "text", "cont": "扫码开发票", "align": "center"},
{"type": "qrcode", "cont": "https://www.sw-aiot.com/i/20260920-0087", "align": "center", "size": "50"},
{"type": "bc128", "cont": "202609200087", "align": "center", "size": "12"},
{"type": "feed", "n": 8},
{"type": "text", "cont": "谢谢光临 欢迎再次品尝", "align": "center", "bold": true},
{"type": "div_star", "cont": "本单已结清"},
{"type": "feed", "lines": 2},
{"type": "cut", "cont": "1"}
]
}
专项:文本样式
对齐、字号、加粗、下划线、删除线。
{
"print": [
{
"type": "title",
"cont": "文本样式"
},
{
"type": "text",
"cont": "左对齐",
"align": "left"
},
{
"type": "text",
"cont": "居中",
"align": "center"
},
{
"type": "text",
"cont": "右对齐",
"align": "right"
},
{
"type": "text",
"cont": "常规字号 size 00"
},
{
"type": "text",
"cont": "宽高放大 size 11",
"size": "11"
},
{
"type": "text",
"cont": "加粗 bold",
"bold": true
},
{
"type": "text",
"cont": "细下划线",
"underline": 1
},
{
"type": "text",
"cont": "粗下划线",
"underline": 2
},
{
"type": "text",
"cont": "删除线(当前按下划线呈现)",
"strike": true
}
]
}
专项:行内混排
同一行内混合字号、加粗和删除线。人民币符号请用全角 ¥。
{
"print": [
{
"type": "text",
"align": "left",
"cont": [
{
"t": "会员:"
},
{
"t": "王小明",
"bold": true
},
{
"t": " 余额 "
},
{
"t": "¥356.80",
"size": "11"
}
]
},
{
"type": "text",
"align": "left",
"cont": [
{
"t": "原价 "
},
{
"t": "¥158.00",
"strike": true
},
{
"t": " 现价 "
},
{
"t": "¥128.00",
"size": "11",
"bold": true
}
]
},
{
"type": "text",
"align": "right",
"cont": [
{
"t": "合计 "
},
{
"t": "¥128.00",
"size": "11",
"bold": true
}
]
}
]
}
专项:多列表
表头放大、列对齐、行距、行分割线。
{
"print": [
{
"size": "00",
"thead_size": "11",
"line_space": 16,
"line_div": 1,
"col_align": [
"left",
"center",
"right",
"right"
],
"thead": {
"名称": "40%",
"数量": "20%",
"单价": "20%",
"金额": "20%"
},
"tbody": [
[
"番茄炒蛋",
2,
"24.00",
"48.00"
],
[
"西红柿炒番茄",
1,
"28.00",
"28.00"
],
[
"米饭",
2,
"2.00",
"4.00"
]
]
}
]
}
专项:备注行
分组标题、多行备注、行级删除线。
{
"print": [
{
"size": "00",
"col_align": [
"left",
"center",
"right",
"right"
],
"thead": {
"商品": "42%",
"数量": "16%",
"单价": "20%",
"小计": "22%"
},
"tbody": [
{
"note": "—— 热菜 ——",
"note_align": "center"
},
[
"可乐鸡翅",
1,
"38.00",
"38.00"
],
{
"cols": [
"水煮鱼",
1,
"58.00",
"58.00"
],
"note": [
"备注:微辣、不要香菜",
"做法:鱼头单独装盘"
],
"note_indent": 2,
"note_space": 6
},
{
"cols": [
"拍黄瓜",
1,
"12.00",
"12.00"
],
"strike": true
}
]
}
]
}
专项:单元格与 overflow
单元格独立样式;wrap 折行、ellipsis 省略。
{
"print": [
{
"size": "00",
"overflow": "wrap",
"thead": {
"商品": "50%",
"数量": "20%",
"金额": "30%"
},
"tbody": [
[
"有机黄瓜特级精选装每箱十斤新鲜直供",
"10",
"350.00"
],
{
"cols": [
{
"t": "超长名称截断并加省略号",
"overflow": "ellipsis"
},
{
"t": "2",
"bold": true,
"align": "center"
},
{
"t": "36.00",
"size": "11",
"bold": true
}
]
},
{
"cols": [
{
"t": "已退货商品",
"strike": true
},
"1",
{
"t": "-12.00",
"strike": true,
"align": "right"
}
]
}
]
}
]
}
专项:左右对齐
金额类汇总行。字符串整行同字号;对象可让右侧单独放大。
{
"print": [
{"both_sides": ["菜品合计", "¥148.00"]},
{"both_sides": ["会员折扣 9折", "-14.80"]},
{"both_sides": ["实收金额", "¥133.20"], "size": "11", "bold": true},
{"both_sides": ["应收", {"t": "¥133.20", "size": "11", "bold": true}]}
]
}
专项:条码与二维码
CODE128、CODE39、单排/双排二维码。
{
"print": [
{
"type": "bc128",
"cont": "123456789",
"align": "center",
"size": "12"
},
{
"type": "code39",
"cont": "CODE39",
"align": "center",
"hri": 2
},
{
"type": "qrcode",
"cont": "https://www.sw-aiot.com",
"align": "center",
"size": "50"
},
{
"type": "qrcode",
"cont": [
"https://www.sw-aiot.com",
"202609200087"
],
"align": "center",
"size": "04"
}
]
}
专项:走纸、图片与切刀
feed 点行/整行、PNG、半切。
{
"print": [
{
"type": "text",
"cont": "走纸 24 点(约 3mm)",
"align": "center"
},
{
"type": "feed",
"n": 24
},
{
"type": "text",
"cont": "走纸 2 行",
"align": "center"
},
{
"type": "feed",
"lines": 2
},
{
"type": "png",
"cont": "https://www.sw-aiot.com/images/example_logo.png",
"align": "center"
},
{
"type": "feed",
"lines": 2
},
{
"type": "cut",
"cont": "1"
}
]
}
3. JSON 标签打印排版
标签打印适用于商品标签、物流标签等场景,通过 坐标定位 精确控制每个元素的位置,核心属性包括坐标(x/y)、尺寸(w/h)、旋转(r)等。
3.1 核心属性一览表
| 属性名 | 值类型 | 取值范围/说明 | 适用场景 |
|---|---|---|---|
| type | string |
|
所有标签元素 |
| x/y | int | 元素左上角坐标(单位dot,1mm=8dot),如x=20表示距左2.5mm | 所有标签元素 |
| w/h | int |
|
对应元素 |
| r | int | 旋转角度:0(默认)、90、180、270(顺时针) | TEXT、QRCODE、BC128、BAR |
| cont | string | 打印内容(文本/条码内容/位图 Base64 或图片 URL) | TEXT、QRCODE、BC128、BITMAP |
| bold | boolean | true(加粗)、false(不加粗,默认) | TEXT |
| show | int | BC128专用:1(显示字符)、0(不显示,默认) | BC128 |
备注:
- png 图片建议为黑白图片,图片宽度像素不超过目标打印宽度最大点数(80mm 对应最大 576 像素,58mm 最大对应 384 像素,110mm 最大对应 832 像素);解码硬上限为宽 1200 × 高 3000 像素。
- bmp 只支持位深度为 1 的黑白图片。
- 如图片内容为固定的(如品牌 logo 等),推荐传 URL 链接方式:打印机有类似浏览器的缓存策略,首次打印下载后,如 URL 不变,后续打印直接使用本地缓存图片,省去下载环节。
3.2 分场景示例(附效果图说明)
示例1:基础文本 + 线条(边框效果)
通过坐标定位实现商品名称、边框线条的精确排版
{
"SIZE": [76, 130], // 标签尺寸:76mm×130mm(608dot×1040dot)
"DIRECTION": 0,
"DENSITY": 10, // 高浓度(清晰)
"label": [
// 1. 商品名称(居中放大)
{
"type": "TEXT",
"x": 100,
"y": 20,
"w": 2,
"h": 2,
"r": 0,
"cont": "无线鼠标"
},
// 2. 边框线(顶部)
{
"type": "BAR",
"x": 20,
"y": 70,
"w": 568,
"h": 2
},
// 3. 边框线(底部)
{
"type": "BAR",
"x": 20,
"y": 970,
"w": 568,
"h": 2
}
]
}
示例2:二维码 + 条码(带字符显示)
展示商品二维码、条码(显示字符)的坐标定位和尺寸设置
{
"SIZE": [76, 130],
"DIRECTION": 0,
"DENSITY": 10,
"label": [
// 1. 二维码(高容错)
{
"type": "QRCODE",
"x": 20,
"y": 20,
"e": "H", // 高容错(适合磨损)
"w": 6,
"cont": "https://www.example.com/goods/987654321"
},
// 2. 条码(显示字符)
{
"type": "BC128",
"x": 20,
"y": 350,
"h": 40,
"show": 1,
"narrow": 2,
"wide": 4,
"cont": "987654321012"
}
]
}
示例3:旋转文本 + 位图(Logo)
展示文本旋转(如生产日期贴边显示)、位图(BITMAP,支持 PNG 与 1 位深度 BMP)的使用方法
{
"SIZE": [76, 130],
"DIRECTION": 0,
"DENSITY": 10,
"label": [
// 1. 生产日期(旋转90度,贴边显示)
{
"type": "TEXT",
"x": 580,
"y": 200,
"w": 0,
"h": 0,
"r": 90,
"cont": "生产日期:2024-03-01"
},
// 2. LOGO位图(支持 1 位深度 BMP 或 PNG,也可用图片 URL)
{
"type": "BITMAP",
"x": 20,
"y": 20,
"cont": "Qk06AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABgAAAAAAAQAAADDDwAAww8AAAAAAAAAAAAA//////////////////////////////////////////////////////8="
}
]
}
4. 完整示例代码
以下提供两个完整JSON示例,分别对应票据打印和标签打印的实际应用场景。
4.1 票据打印完整示例(餐饮结账单)
标准 print 数组格式,可直接下发打印。
{
"print": [
{"type": "title", "cont": "云味小厨"},
{"type": "text", "cont": "堂食结账单", "align": "center", "size": "01"},
{"type": "feed", "n": 8},
{"type": "text", "cont": "桌号 A12 人数 4"},
{"type": "text", "cont": "单号 20260920-0087"},
{"type": "text", "cont": "开单 2026-09-20 12:18 收银 王小明"},
{"type": "div_line"},
{
"size": "00",
"thead_size": "00",
"line_space": 12,
"col_align": ["left", "center", "right", "right"],
"thead": {"菜品": "42%", "数量": "16%", "单价": "20%", "小计": "22%"},
"tbody": [
{"note": "—— 凉菜 ——", "note_align": "center"},
["拍黄瓜", 1, "12.00", "12.00"],
{
"cols": ["口水鸡", 1, "32.00", "32.00"],
"note": "备注:少放花椒",
"note_indent": 2
},
{"note": "—— 热菜 ——", "note_align": "center"},
["可乐鸡翅", 1, "38.00", "38.00"],
{
"cols": ["水煮鱼", 1, "58.00", "58.00"],
"note": ["备注:微辣、不要香菜", "做法:鱼头单独装盘"],
"note_indent": 2,
"note_space": 6
},
{
"cols": [
{"t": "地三鲜(已退)", "strike": true},
1,
"26.00",
{"t": "-26.00", "strike": true}
]
},
["米饭", 4, "2.00", "8.00"]
]
},
{"type": "div_line"},
{"both_sides": ["菜品合计", "¥148.00"]},
{"both_sides": ["会员折扣 9折", "-14.80"]},
{"type": "feed", "n": 6},
{"type": "text", "align": "right",
"cont": [{"t": "应收 "}, {"t": "¥133.20", "size": "11", "bold": true}]},
{"type": "feed", "n": 6},
{"both_sides": ["微信支付", "133.20"], "bold": true},
{"type": "div_line"},
{"type": "text", "align": "left",
"cont": [{"t": "本单积分 "}, {"t": "133", "bold": true}, {"t": " 累计 "}, {"t": "3560", "bold": true}]},
{"type": "feed", "n": 8},
{"type": "text", "cont": "扫码开发票", "align": "center"},
{"type": "qrcode", "cont": "https://www.sw-aiot.com/i/20260920-0087", "align": "center", "size": "50"},
{"type": "bc128", "cont": "202609200087", "align": "center", "size": "12"},
{"type": "feed", "n": 8},
{"type": "text", "cont": "谢谢光临 欢迎再次品尝", "align": "center", "bold": true},
{"type": "div_star", "cont": "本单已结清"},
{"type": "feed", "lines": 2},
{"type": "cut", "cont": "1"}
]
}
4.2 标签打印完整示例(商品标签)
{
"SIZE": [76, 130], // 标签尺寸:76mm×130mm(608dot×1040dot)
"DIRECTION": 0,
"DENSITY": 10, // 高浓度(清晰)
"SOUND": 2,
"label": [
// 1. 商品名称(放大2倍)
{
"type": "TEXT",
"x": 20,
"y": 20,
"w": 2,
"h": 2,
"r": 0,
"cont": "无线鼠标"
},
// 2. 品牌(默认字体)
{
"type": "TEXT",
"x": 20,
"y": 60,
"w": 0,
"h": 0,
"r": 0,
"cont": "品牌:XX科技"
},
// 3. 规格(默认字体)
{
"type": "TEXT",
"x": 20,
"y": 80,
"w": 0,
"h": 0,
"r": 0,
"cont": "规格:黑色 | 蓝牙5.0"
},
// 4. 价格(加粗,放大1.5倍)
{
"type": "TEXT",
"x": 20,
"y": 110,
"w": 1,
"h": 1,
"r": 0,
"cont": "售价:¥99.00",
"bold": true
},
// 5. 二维码(链接到商品页)
{
"type": "QRCODE",
"x": 20,
"y": 150,
"e": "H", // 高容错(适合磨损)
"w": 6,
"cont": "https://www.example.com/goods/987654321"
},
// 6. 条码(显示字符)
{
"type": "BC128",
"x": 20,
"y": 350,
"h": 40,
"show": 1,
"narrow": 2,
"wide": 4,
"cont": "987654321012"
},
// 7. 生产日期(旋转90度,贴边显示)
{
"type": "TEXT",
"x": 580,
"y": 200,
"w": 0,
"h": 0,
"r": 90,
"cont": "生产日期:2024-03-01"
},
// 8. 有效期(旋转90度)
{
"type": "TEXT",
"x": 580,
"y": 300,
"w": 0,
"h": 0,
"r": 90,
"cont": "有效期至:2026-02-28"
},
// 9. 底部线条(分隔)
{
"type": "BAR",
"x": 20,
"y": 420,
"w": 560,
"h": 5
}
]
}
5. 常见问题(FAQ)
可能原因及解决方案:
- 1. 二维码内容过长:BC128条码最大支持512字节,若内容超量,需精简链接或文本(如使用短链接)。
- 2. 尺寸过小:调整
size属性(qrcode的size范围"01"-"09",默认"04"),建议设置为"05"-"07"以保证清晰。 - 3. 对齐方式错误:确保
align为"center"(居中),避免边缘被截断。
核心原因:混淆mm和dot单位,标签打印的x/y属性单位为dot,且1mm = 8dot。
解决方案:
- 1. 计算坐标:若需在10mm×10mm位置放置元素,需转换为
x=80(10×8)、y=80(10×8)。 - 2. 调整DIRECTION:若整体偏移,检查
DIRECTION是否为0(正常方向),1为反向出纸,可能导致位置颠倒。 - 3. 检查元素尺寸:文本/条码的放大倍数(w/h)会影响实际占用宽度,需预留足够空间。
解决方案:
- 1. 列宽占比总和:确保
thead的列宽占比总和为100%(如50%+15%+15%+20%=100%),否则会自动调整。 - 2. 内容长度:若某列内容过长(如商品名称),需缩小字体尺寸(调整
size;表头可用thead_size单独设置)或增加列宽占比。 - 3. 行间距离:通过
line_space属性增加行间距离(单位dot,8dot=1mm),避免内容重叠。
需满足2个核心条件:
- 1. 位图格式:支持 PNG 与 位深度为1的BMP图(黑白二值图)。PNG 不限位深(彩色/灰度均可解码),但打印机端会按固定阈值转为黑白——建议直接使用黑白图片,避免浅色内容丢失或深色区块糊死。1 位 BMP 可通过Windows画图工具生成:打开图片→另存为→格式选择“单色位图(*.bmp;*.dib)”。
- 2. Base64编码:确保
cont属性的值为图片的完整Base64编码(无缺失/多余字符),可通过在线工具(如Base64 Guru)转换。 - 3. 图片尺寸:宽度像素不超过目标打印宽度最大点数(80mm 对应最大 576 像素,58mm 最大对应 384 像素,110mm 最大对应 832 像素);解码硬上限为宽 1200 × 高 3000 像素。