版本: 2.0 | 最后更新: 2025年10月
只需三步,即可在您的网页应用中集成打印功能:
socket-link.js 文件。SocketLink 实例,连接到打印服务。首先,将 socket-link.js 文件放到您的项目中(例如 js/ 目录下),然后在您的 HTML 页面的 或 底部引入它:
引入后,全局将可用 SocketLink 类。
创建一个 SocketLink 实例,它会自动尝试连接到指定的 WebSocket 服务器地址。
功能: 创建一个自动管理连接的 WebSocket 客户端。
参数:
url (string): WebSocket 服务器地址,例如 "ws://localhost:13526"。options (object, 可选): 配置选项。
reconnectInterval: 重连间隔(毫秒),默认 3000。maxReconnectAttempts: 最大重连尝试次数,默认 10。onOpen: 连接成功时的回调函数。onMessage: 收到消息时的回调函数。onError: 发生错误时的回调函数。onClose: 连接关闭时的回调函数。示例:
// 创建连接
const printLink = new SocketLink("ws://localhost:13526", {
reconnectInterval: 5000, // 5秒重连一次
maxReconnectAttempts: 5,
onOpen: () => {
console.log("链接成功");
},
onMessage: (event) => {
console.log("收到消息", event);
},
onError: (e) => console.error("❌ Socket错误", e),
onClose: (event) => {
console.log("连接已关闭", event);
}
});
所有与后端服务的通信都必须遵循统一的指令格式。指令是一个包含 Command 和 Data 两个字段的 JSON 对象。
Command: 指令类型,如 "print", "getprintlist"。Data: 指令附带的数据,通常是字符串化的 JSON。发送此指令以执行打印任务。
"print"示例:
// 定义一个打印任务
const printTask = {
"TaskId": "ORDER_123456",
"Template": {
"config": { "width": 80, "height": 60 },
"nodes": [
{
"type": "label",
"rect": { "x": 5, "y": 5, "width": 70, "height": 10 },
"text": "Hello, YbPrintPlug!",
"font": { "size": 14, "bold": true }
}
]
},
"data": {},
"PrintName": ""
};
// 包装成标准指令并发送
const printInstruction = {
Command: "print",
Data: JSON.stringify(printTask)
};
printLink.send(printInstruction);
发送此指令以查询系统中所有可用的打印机。
"getprintlist"示例:
const getPrintersInstruction = {
Command: "getprintlist"
};
printLink.send(getPrintersInstruction);
服务端响应示例:
{
"printers": ["Microsoft Print to PDF", "HP LaserJet MFP"]
}
发送此指令以获取屏幕物理 DPI,用于前端精确渲染。
"getscreen"示例:
const getScreenInstruction = {
Command: "getscreen"
};
printLink.send(getScreenInstruction);
服务端响应示例:
{
"PixelsPerMillimeterX": 3.78,
"PixelsPerMillimeterY": 3.78
}
打印模板是一个 JSON 对象,它精确地定义了打印内容的布局和样式。所有尺寸单位均为毫米 (mm),确保所见即所得。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
TaskId |
string | 是 | 任务唯一标识,用于日志追踪。 |
Template |
object | 是 | 包含页面配置和节点数组的核心模板对象。 |
data |
object | 是 | 数据源对象,用于模板中的数据绑定。 |
PrintName |
string | 否 | 指定打印机名称。留空则使用系统默认打印机。 |
Template 对象{
"config": {
"width": 80,
"height": 60
},
"nodes": [ ... ]
}
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
config |
object | 是 | 页面配置,定义纸张的物理尺寸。 |
nodes |
array | 是 | 节点数组,定义页面上的所有元素。 |
config 对象| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
width |
number | 是 | 页面宽度,单位为毫米 (mm)。 |
height |
number | 是 | 页面高度,单位为毫米 (mm)。 |
所有节点都必须包含一个 type 字段和一个 rect 字段,用于定义其类型和位置大小。
{
"type": "label", // 节点类型
"rect": { // 位置和大小
"x": 10,
"y": 10,
"width": 50,
"height": 20
},
// ... 其他特定于类型的配置
}
label - 文本标签用于显示静态或动态文本。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text |
string | "" |
显示的文本,支持数据绑定,如 "订单号: <%=orderId%>"。 |
font |
object | {size: 12} |
字体配置,详见 4.1 节。 |
stringFormat |
object | {align: "center"} |
文本格式配置,如对齐、行高,详见 4.2 节。 |
color.text |
string | "#000000" |
文本颜色,HEX 格式。 |
示例:
{
"type": "label",
"rect": { "x": 5, "y": 5, "width": 70, "height": 10 },
"text": "客户姓名: <%=customer.name%>",
"font": { "size": 12, "bold": true, "family": "微软雅黑" },
"stringFormat": { "align": "left", "valign": "middle" },
"color": { "text": "#333333" }
}
barcode - 条形码支持生成高精度条形码。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text |
string | "" |
条码内容,支持数据绑定。 |
displayValue |
boolean | false |
是否在条码下方显示编码的文本。 |
barcode.rotate |
number | 0 |
旋转角度,支持 0, 90, 180, 270。 |
barcode.mirror |
boolean | false |
是否水平镜像翻转条码。 |
示例:
{
"type": "barcode",
"rect": { "x": 10, "y": 20, "width": 60, "height": 20 },
"text": "<%=product.sku%>",
"displayValue": true,
"barcode": {
"rotate": 0,
"mirror": false
}
}
qrcode - 二维码用于生成二维码。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text |
string | "" |
二维码内容,支持数据绑定。 |
示例:
{
"type": "qrcode",
"rect": { "x": 50, "y": 5, "width": 25, "height": 25 },
"text": "https://your-domain.com/item/<%=itemId%>"
}
image - 图片支持 Base64、网络 URL 和本地文件路径。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text |
string | "" |
图片地址或 Base64 数据。 |
background.size |
string | "contain" |
缩放方式: "fill", "cover", "contain"。 |
示例 (Base64):
{
"type": "image",
"rect": { "x": 5, "y": 35, "width": 20, "height": 20 },
"text": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
}
line - 线条用于绘制横线或竖线。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
line.type |
string | "horizontal" |
线条类型: "horizontal" 或 "vertical"。 |
line.width |
number | 0.5 |
线条粗细,单位为毫米 (mm)。 |
line.color |
string | "#000000" |
线条颜色,HEX 格式。 |
示例:
{
"type": "line",
"rect": { "x": 0, "y": 30, "width": 80, "height": 0 },
"line": {
"type": "horizontal",
"width": 0.3,
"color": "#cccccc"
}
}
table - 表格用于绘制数据表格,支持自动行高。
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text |
string | "" |
指向 data 中数据数组的路径,如 "items"。 |
columns |
array | - | 列配置数组,每项定义一列的标题、字段、宽度等。 |
rowsHeight.header |
number | 8 |
表头行高,单位为毫米 (mm)。 |
示例:
{
"type": "table",
"rect": { "x": 5, "y": 45, "width": 70, "height": 30 },
"text": "orderItems",
"columns": [
{ "title": "商品", "field": "name", "width": 30, "align": "left" },
{ "title": "数量", "field": "qty", "width": 15, "align": "center" },
{ "title": "价格", "field": "price", "width": 25, "align": "right" }
],
"rowsHeight": { "header": 8 }
}
box - 矩形框通常用于绘制背景色或边框。
{
"type": "box",
"rect": { "x": 0, "y": 0, "width": 80, "height": 60 },
"color": {
"enabled": true,
"background": "#f0f0f0"
},
"border": {
"enabled": true,
"width": 0.5,
"color": "#000000",
"radius": 2
}
}
font - 字体配置| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
size |
number | 12 |
字体大小,单位为点 (pt)。 |
family |
string | "Arial" |
字体名称,如 "微软雅黑"。 |
bold |
boolean | false |
是否加粗。 |
stringFormat - 文本格式| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
align |
string | "center" |
水平对齐: "left", "center", "right"。 |
valign |
string | "middle" |
垂直对齐: "top", "middle", "bottom"。 |
lineHeight |
number | 1.0 |
行高因子。实际行高 = 字体大小 * lineHeight。 |
trim |
string | "" |
文本溢出处理,设为 "ellipsis" 显示省略号。 |
在模板的 text 字段中,使用 <%= ... %> 语法可以绑定到 data 对象中的数据。
前端发送的数据:
const printTask = {
"Template": { ... }, // 模板定义
"data": {
"customer": {
"name": "张三",
"phone": "13800138000"
},
"items": [
{ "name": "苹果", "price": 5.5, "qty": 3 },
{ "name": "香蕉", "price": 3.2, "qty": 2 }
]
}
};
模板中的绑定:
{
"type": "label",
"rect": { "x": 5, "y": 5, "width": 70, "height": 10 },
"text": "客户: <%=customer.name%> (<%=customer.phone%>)"
},
{
"type": "table",
"rect": { "x": 5, "y": 20, "width": 70, "height": 30 },
"text": "items", // 绑定到 data.items 数组
"columns": [
{ "title": "品名", "field": "name", "width": 30 },
{ "title": "单价", "field": "price", "width": 20 },
{ "title": "数量", "field": "qty", "width": 20 }
]
}
最终打印效果:
客户: 张三 (13800138000) +----------------+--------+--------+ | 品名 | 单价 | 数量 | +----------------+--------+--------+ | 苹果 | 5.5 | 3 | | 香蕉 | 3.2 | 2 | +----------------+--------+--------+<<<<<<< HEAD