yh-hiprint 2.6.1 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,799 @@
1
+ # electron-hiprint
2
+
3
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/CcSimple/electron-hiprint)
4
+
5
+ <div align="center">
6
+
7
+ ![logo](./build/icons/100x100.png)
8
+
9
+ </div>
10
+
11
+ 该项目是为 [vue-plugin-hiprint](https://github.com/CcSimple/vue-plugin-hiprint) 配套开发的静默打印解决方案。我们发现部分使用此项目的开发者拥有自定义的设计器或渲染方案,或者仅需要静默打印一段 HTML、PDF。如果您也有类似需求,electron-hiprint 将是您的理想选择。
12
+
13
+ <div align="center">
14
+
15
+ ![主界面](./res/electron-hiprint.png)
16
+
17
+ </div>
18
+
19
+ ## 快速开始
20
+
21
+ #### 下载发行版
22
+
23
+ 1. [github releases](https://github.com/CcSimple/electron-hiprint/releases)
24
+ 2. [gitee releases](https://gitee.com/CcSimple/electron-hiprint/releases)
25
+
26
+ #### 本地调试与打包
27
+
28
+ ```shell
29
+ git clone https://github.com/CcSimple/electron-hiprint.git
30
+ # or
31
+ git clone https://gitee.com/CcSimple/electron-hiprint.git
32
+
33
+ # init
34
+ cd electron-hiprint
35
+ npm install
36
+
37
+ # 调试预览
38
+ npm run start
39
+
40
+ # 打包 win x64,其余平台版本详情见 package.json
41
+ npm run build-w-64
42
+ ```
43
+
44
+ ## 推荐的 Web 打印设计渲染插件
45
+
46
+ [vue-plugin-hiprint](https://github.com/CcSimple/vue-plugin-hiprint.git)
47
+
48
+ ## 打印原理
49
+
50
+ 1. **客户端服务**:通过 socket.io (默认端口 17521)提供服务。
51
+
52
+ - 使用 `socket.io-client@4.x` 连接: `http://localhost:17521`
53
+
54
+ 2. **发送打印数据**:通过 `socket.emit` 方法发送打印数据
55
+
56
+ - 示例:
57
+
58
+ ```js
59
+ socket.emit("news", { html, templateId, printer, pageSize });
60
+ ```
61
+
62
+ - 参数说明:
63
+ - `html`: HTML 字符串。
64
+ - `templateId`: 用于标识成功或失败回调的 ID。
65
+ - `printer`: 打印机名称。
66
+ - `pageSize`: 打印纸张大小。
67
+
68
+ ## 拓扑结构
69
+
70
+ <div align="center">
71
+
72
+ ![image](./res/electron-hiprint_LAN_network_topology.png)
73
+
74
+ </div>
75
+
76
+ ## 客户端设置
77
+
78
+ 在 `v1.0.7` 后续版本中,支持这些设置
79
+
80
+ <div align="center">
81
+
82
+ ![image](./res/electron-hiprint_set.png)
83
+
84
+ </div>
85
+
86
+ ```json
87
+ {
88
+ "mainTitle": "Electron-hiprint",
89
+ "nickName": "",
90
+ "openAtLogin": true,
91
+ "openAsHidden": true,
92
+ "port": "17521",
93
+ "token": null,
94
+ "connectTransit": true,
95
+ "transitUrl": "https://v4.printjs.cn:17521",
96
+ "transitToken": "hiprint-youcode",
97
+ "allowNotify": true,
98
+ "closeType": "tray",
99
+ "pluginVersion": "0.0.58-fix",
100
+ "logPath": "C:\\Users\\Administrator\\AppData\\Roaming\\hiprint\\logs",
101
+ "pdfPath": "C:\\Users\\Administrator\\AppData\\Local\\Temp",
102
+ "defaultPrinter": "",
103
+ "disabledGpu": false,
104
+ "rePrint": true
105
+ }
106
+ ```
107
+
108
+ ### 配置项说明
109
+
110
+ | 序号 | 字段名 | 类型 | 说明 |
111
+ | ---- | ---------------------- | ---------------- | ----------------------------------------------------- |
112
+ | 1 | mainTitle[[1]](#tips1) | String | 主标题 |
113
+ | 2 | nickName | String | 可设置的便于识别的友好设备名称 |
114
+ | 3 | openAtLogin | Boolean | 系统登录时启动 |
115
+ | 4 | openAsHidden | Boolean | 启动时隐藏窗口 |
116
+ | 5 | connectTransit | Boolean | 连接中转服务 |
117
+ | 6 | port | String \| Number | 端口号(10000 - 65535),默认为 17521 |
118
+ | 7 | token[[2]](#tips2) | String \| null | 身份验证令牌,支持固定 Token |
119
+ | 8 | transitUrl | String | 中转服务地址 |
120
+ | 9 | transitToken | String | 中转服务令牌 |
121
+ | 10 | closeType | String | 窗口关闭行为(tray 或 quit) |
122
+ | 11 | pluginVersion | String | vue-plugin-hiprint 插件版本 |
123
+ | 12 | logPath | String | 日志路径 |
124
+ | 13 | pdfPath | String | 临时文件路径 |
125
+ | 14 | defaultPrinter | String | 默认打印机 |
126
+ | 15 | disabledGpu | Boolean | 禁用 GPU 加速,可解决部分设备打印模糊问题,默认 false |
127
+ | 16 | rePrint[[1]](#tips1) | Boolean | 是否允许重打,默认 true |
128
+
129
+ > <span id="tips1">[1]</span> `mainTitle` 和 `rePrint` 字段在设置页面中未显式提供设置,方便各位可以在不修改源码二开的情况下通过配置快速实现定制化和高级功能,且不易被客户篡改,详见下方[覆盖默认配置方法](#覆盖默认配置方法)。
130
+
131
+ > <span id="tips2">[2]</span> `vue-plugin-hiprint` 需要使用 ^0.0.55 版本才可使用 `token` 否则请勿设置 `token`。
132
+
133
+ ### 覆盖默认配置方法
134
+
135
+ 1. 二开项目,直接修改 [项目源码 /tools/utils.js](./tools/utils.js) 并重新打包
136
+ 2. win `v1.0.12-beta6` 后续版本可在 `exe` 安装包路径添加 `config.json`,安装包会自动检测并使用该配置
137
+
138
+ ```
139
+ hiprint_win_x64-1.0.12-beta7.exe
140
+ config.json
141
+ ```
142
+
143
+ ## 中转服务
144
+
145
+ 项目支持 `node-hiprint-transit`,可解决跨域问题并实现云打印功能,详见 [node-hiprint-transit](https://github.com/Xavier9896/node-hiprint-transit)
146
+
147
+ <div align="center">
148
+
149
+ ![image](./res/electron-hiprint_set_transit.png)
150
+
151
+ </div>
152
+
153
+ <div align="center">
154
+
155
+ ![image](./res/electron-hiprint_transit_WAN_network_topology.png)
156
+
157
+ </div>
158
+
159
+ # 与非 `vue-plugin-hiprint` 项目的兼容
160
+
161
+ 目标用户:只需要实现 HTML、PDF 静默打印,未使用 `vue-plugin-hiprint` 设计插件。
162
+
163
+ ## 客户端连接
164
+
165
+ 1. 安装依赖
166
+
167
+ ```console
168
+ npm install socket.io-client@4 --save
169
+ ```
170
+
171
+ 2. 连接示例
172
+
173
+ ```js
174
+ import { io } from "socket.io-client";
175
+
176
+ const socket = io("http://localhost:17521", {
177
+ transports: ["websocket"],
178
+ auth: {
179
+ token: "vue-plugin-hiprint",
180
+ },
181
+ });
182
+
183
+ socket.on("connect", () => {
184
+ globalThis.connect = true;
185
+ // TODO: Do something for your project
186
+ });
187
+ ```
188
+
189
+ 3. 获取客户端信息
190
+
191
+ <details open>
192
+ <summary>
193
+ 与 <b>electron-hiprint</b> 建立连接
194
+ </summary>
195
+
196
+ - 连接成功后 `electron-hiprint` 会主动发送 `clientInfo`、`printerList` 事件,你只需要监听这两个事件即可获取到客户端信息与打印机列表。
197
+
198
+ ```js
199
+ socket.on("clientInfo", (clientInfo) => {
200
+ globalThis.clientInfo = clientInfo;
201
+ });
202
+
203
+ socket.on("printerList", (printerList) => {
204
+ globalThis.printerList = printerList;
205
+ });
206
+ ```
207
+
208
+ ```js
209
+ // clientInfo
210
+ {
211
+ "hostname": "Admin", // 主机名
212
+ "version": "1.0.12-beta9", // 客户端版本
213
+ "platform": "win32", // 平台类型
214
+ "arch": "x64", // 系统架构
215
+ "mac": "d0:46:0c:97:4b:68", // mac 地址
216
+ "ip": "192.168.0.114", // 设备 ip
217
+ "ipv6": "fe80::2157:4b26:1c2f:c4ca", // 设备 ipv6
218
+ "clientUrl": "http://192.168.0.114:17521", // 本地服务地址
219
+ "machineId": "0e8b222e-517b-491e-883a-b6283a62e280", // 设备唯一 ID
220
+ "nickName": "打印客户端", // 友好昵称
221
+ }
222
+
223
+ // printerList
224
+ [{
225
+ description: "",
226
+ displayName: "Microsoft Print to PDF",
227
+ isDefault: true,
228
+ name: "Microsoft Print to PDF",
229
+ options: {,
230
+ "printer-location": "",
231
+ "printer-make-and-model": "Microsoft Print To PDF",
232
+ "system_driverinfo": "Microsoft Print To PDF;10.0.19041.3570 (WinBuild.160101.0800);Microsoft® Windows® Operating System;10.0.19041.3570"
233
+ },
234
+ status: 0
235
+ }, {…}, {…}, {…}, {…}, {…}]
236
+ ```
237
+
238
+ 你也可以主动向 `electron-hiprint` 发送 `getClientInfo`、`refreshPrinterList` 事件,来获取客户端打印机列表。
239
+
240
+ ```js
241
+ socket.emit("getClientInfo");
242
+ socket.emit("refreshPrinterList");
243
+ ```
244
+
245
+ </details>
246
+
247
+ <details>
248
+ <summary>
249
+ 与 <b>node-hiprint-transit</b> 建立连接
250
+ </summary>
251
+
252
+ - 连接成功后 `node-hiprint-transit` 会主动发送 `clients`、`printerList` 事件,你只需要监听这两个事件即可获取到客户端信息与打印机列表。
253
+
254
+ ```js
255
+ socket.on("clients", (clients) => {
256
+ globalThis.clients = clients;
257
+ });
258
+
259
+ socket.on("printerList", (printerList) => {
260
+ globalThis.printerList = printerList;
261
+ });
262
+ ```
263
+
264
+ ```js
265
+ // clients
266
+ {
267
+ "AlBaUCNs3AIMFPLZAAAh": {
268
+ "hostname": "Admin", // 主机名
269
+ "version": "1.0.12-beta9", // 客户端版本
270
+ "platform": "win32", // 平台类型
271
+ "arch": "x64", // 系统架构
272
+ "mac": "d0:46:0c:97:4b:68", // mac 地址
273
+ "ip": "192.168.0.114", // 设备 ip
274
+ "ipv6": "fe80::2157:4b26:1c2f:c4ca", // 设备 ipv6
275
+ "clientUrl": "http://192.168.0.114:17521", // 本地服务地址
276
+ "machineId": "0e8b222e-517b-491e-883a-b6283a62e280", // 设备唯一 ID
277
+ "nickName": "打印客户端", // 友好昵称
278
+ printerList: [{
279
+ description: "",
280
+ displayName: "Microsoft Print to PDF",
281
+ isDefault: true,
282
+ name: "Microsoft Print to PDF",
283
+ options: {,
284
+ "printer-location": "",
285
+ "printer-make-and-model": "Microsoft Print To PDF",
286
+ "system_driverinfo": "Microsoft Print To PDF;10.0.19041.3570 (WinBuild.160101.0800);Microsoft® Windows® Operating System;10.0.19041.3570"
287
+ },
288
+ status: 0
289
+ }, {…}, {…}, {…}, {…}, {…}],
290
+ version: "1.0.7",
291
+ },
292
+ "clientid": {…},
293
+ ...
294
+ }
295
+
296
+ // printerList
297
+ [{
298
+ clientId: "AlBaUCNs3AIMFPLZAAAh",
299
+ description: "",
300
+ displayName: "Microsoft Print to PDF",
301
+ isDefault: true,
302
+ name: "Microsoft Print to PDF",
303
+ options: {,
304
+ "printer-location": "",
305
+ "printer-make-and-model": "Microsoft Print To PDF",
306
+ "system_driverinfo": "Microsoft Print To PDF;10.0.19041.3570 (WinBuild.160101.0800);Microsoft® Windows® Operating System;10.0.19041.3570"
307
+ },
308
+ status: 0
309
+ }, {…}, {…}, {…}, {…}, {…}]
310
+ ```
311
+
312
+ 你也可以主动向 `node-hiprint-transit` 发送 `getClients`、`refreshPrinterList` 事件,来获取客户端打印机列表。
313
+
314
+ ```js
315
+ socket.emit("getClients");
316
+
317
+ socket.emit("refreshPrinterList");
318
+ // node-hiprint-transit 会将这个请求再转发给所有连接的 electron-hiprint ,以获取最新的打印机列表,但是并没有等待所有 electron-hiprint 响应结束,而是在延迟 2s 后直接返回了缓存及新获取到的打印机列表。并且 node-hiprint-transit 每 10min 都会主动向 electron-hiprint 请求一次 printerList,所以这应该并无大碍。或者你也可以优化这一个功能。
319
+
320
+ // https://github.com/Xavier9896/node-hiprint-transit/blob/main/index.js#L139
321
+ ```
322
+
323
+ </details>
324
+
325
+ ## 获取打印机纸张信息
326
+
327
+ > [!IMPORTANT]
328
+ > 该功能暂时只在 Window 环境下安装的 `electron-hiprint` 中支持,`node-hiprint-transit` 中转暂时也未支持!
329
+
330
+ <details>
331
+ <summary>
332
+ 连接为 <b>electron-hiprint</b>
333
+ </summary>
334
+
335
+ ```js
336
+ // printerName: 打印机名称 可选值,缺省时返回所有打印机的纸张信息
337
+ if (globalThis.connect) {
338
+ socket.emit("getPaperSizeInfo", printerName);
339
+ } else {
340
+ alert("未连接客户端!");
341
+ window.open("hiprint://");
342
+ }
343
+
344
+ socket.on("paperSizeInfo", (paperSizes) => {
345
+ console.log(paperSizes);
346
+ });
347
+ ```
348
+
349
+ ```js
350
+ [
351
+ {
352
+ "PrinterName": "Microsoft Print to PDF",
353
+ "TaskNumber": 0, // 打印队列任务数
354
+ "Status": 0, // 打印机状态码
355
+ "StatusMsg": "准备就绪(Ready)", // 打印机状态信息
356
+ "PaperSizes": [
357
+ {
358
+ "Height": 1100,
359
+ "Kind": 1,
360
+ "PaperName": "信纸",
361
+ "RawKind": 1,
362
+ "Width": 850
363
+ },
364
+ {...}, {...}, {...}
365
+ ]
366
+ }
367
+ ]
368
+ ```
369
+
370
+ </details>
371
+
372
+ ## 打印 HTML
373
+
374
+ <details open>
375
+ <summary>
376
+ 连接为 <b>electron-hiprint</b>
377
+ </summary>
378
+
379
+ ```js
380
+ /**
381
+ * @description: 打印 html 字符串
382
+ * @param {String} html 打印的html字符串
383
+ * @param {String|number} templateId vue-plugin-hiprint 中的模板id,你可以自定义传入一个 Id,用于回调 success/error 判断
384
+ * @param {String} printer 打印机名称 printer.name 可为空,缺省默认使用设备默认打印机
385
+ * @param {pageSize} pageSize 打印纸张大小 { height: 80 * 1000, width: 60 * 1000 } 可为空,缺省默认使用打印机默认纸张
386
+ * @description: 其他参数参考 默认打印参数说明
387
+ */
388
+ if (globalThis.connect) {
389
+ socket.emit("news", { html, templateId, printer, pageSize });
390
+ } else {
391
+ alert("未连接客户端!");
392
+ window.open("hiprint://");
393
+ }
394
+ ```
395
+
396
+ [打印回调](#打印回调)
397
+
398
+ </details>
399
+
400
+ <details>
401
+ <summary>
402
+ 连接为 <b>node-hiprint-transit</b>
403
+ </summary>
404
+
405
+ > [!IMPORTANT]
406
+ > 当你连接中转服务时,需要在参数中指定 `client`
407
+
408
+ ```js
409
+ // 你可以自行在项目中实现一个选择客户端、打印机的功能
410
+ const clientId = "AlBaUCNs3AIMFPLZAAAh";
411
+ const client = globalThis.clients[clientId];
412
+ const printer = globalThis.clients[0].printerList[0];
413
+
414
+ if (globalThis.connect) {
415
+ socket.emit("news", {
416
+ html,
417
+ client: clientId,
418
+ templateId,
419
+ printer,
420
+ pageSize,
421
+ });
422
+ socket.emit("news", {
423
+ html,
424
+ client: client.clientId,
425
+ templateId,
426
+ printer: printer.name,
427
+ pageSize,
428
+ });
429
+ } else {
430
+ alert("未连接客户端!");
431
+ window.open("hiprint://");
432
+ }
433
+ ```
434
+
435
+ </details>
436
+
437
+ ### 默认打印参数说明
438
+
439
+ ```js
440
+ // 详见electron文档: https://www.electronjs.org/zh/docs/latest/api/web-contents
441
+ {
442
+ silent: data.silent ?? true, // 静默打印
443
+ printBackground: data.printBackground ?? true, // 是否打印背景
444
+ printer: printer, // 打印机名称
445
+ color: data.color ?? true, // 是否打印颜色
446
+ margins: data.margins ?? {
447
+ marginType: "none",
448
+ }, // 边距
449
+ landscape: data.landscape ?? false, // 是否横向打印
450
+ scaleFactor: data.scaleFactor ?? 100, // 打印缩放比例
451
+ pagesPerSheet: data.pagesPerSheet ?? 1, // 每张纸的页数
452
+ collate: data.collate ?? true, // 是否排序
453
+ copies: data.copies ?? 1, // 打印份数
454
+ pageRanges: data.pageRanges ?? {}, // 打印页数
455
+ duplexMode: data.duplexMode, // 打印模式 simplex,shortEdge,longEdge
456
+ dpi: data.dpi, // 打印机DPI
457
+ header: data.header, // 打印头
458
+ footer: data.footer, // 打印尾
459
+ pageSize: data.pageSize, // 打印纸张 // A0, A1, A2, A3, A4, A5, A6, Legal, Letter, Tabloid
460
+ }
461
+ // 其中纸张大小参数 pageSize 如果传自定义大小, 需要乘以 1000
462
+ { height: 80 * 1000, width: 60 * 1000 }
463
+ ```
464
+
465
+ ## 使用 pdf 打印功能
466
+
467
+ 原理:
468
+
469
+ 1. 通过 electron 的 printToPDF 先导出 pdf 文件
470
+ 2. 再通过 pdf-to-printer 或 unix-print 打印 pdf 文件
471
+
472
+ > 传数据时需要传入: { type:'pdf' }
473
+
474
+ > 如果是自定义的纸张大小, 别忘了传自定义的 paperName (纸张名称)
475
+
476
+ ```js
477
+ {
478
+ client?: string; // 客户端id,连接中转服务必填
479
+ printer?: string; // 打印机名称
480
+ pages?: string; // 打印页数
481
+ subset?: string; // 奇偶页 even、odd
482
+ orientation?: string; // 纸张方向 portrait、landscape
483
+ scale?: string; // 缩放 noscale、shrink、fit
484
+ monochrome?: boolean; // 黑白打印 true、false
485
+ side?: string; // 单双面 duplex, duplexshort, duplexlong, and simplex
486
+ bin?: string; // select tray to print to
487
+ paperName?: string; // 纸张大小 A2, A3, A4, A5, A6, letter, legal, tabloid, statement
488
+ silent?: boolean; // Silences error messages.
489
+ printDialog?: boolean; // 显示打印对话框 true、false
490
+ copies?: number; // 打印份数
491
+ }
492
+
493
+ // vue-plugin-hiprint
494
+ hiprint.hiwebSocket.send({ html, client, printer, type: 'pdf'})
495
+
496
+ // 非vue-plugin-hiprint
497
+ socket.emit("news", { html, client, printer, type: 'pdf'})
498
+ ```
499
+
500
+ ## 下载网络 pdf 打印
501
+
502
+ 原理:
503
+
504
+ 1. 通过 node 的 http 或 https 库下载网络 pdf 文件至用户临时目录
505
+ 2. 后续内容同使用 pdf 打印功能
506
+
507
+ > 因为打印网络 pdf 不存在模板拼接,所以打印时直接如下调用即可
508
+
509
+ > 参数同 pdf 打印功能
510
+
511
+ ```js
512
+ // vue-plugin-hiprint
513
+ hiprint.hiwebSocket.send({
514
+ client,
515
+ printer,
516
+ type: "url_pdf",
517
+ templateId: "自定义Id,用于判断任务是否成功",
518
+ pdf_path: "网络PDF的下载url",
519
+ });
520
+
521
+ // 非vue-plugin-hiprint
522
+ socket.emit("news", {
523
+ client,
524
+ printer,
525
+ type: "url_pdf",
526
+ templateId: "自定义Id,用于判断任务是否成功",
527
+ pdf_path: "网络PDF的下载url",
528
+ });
529
+ ```
530
+
531
+ ## Blob 打印 PDF (🧪实验性功能)
532
+
533
+ > ^1.0.14-beta4
534
+
535
+ ```js
536
+ // vue-plugin-hiprint
537
+ hiprint.hiwebSocket.send({
538
+ client,
539
+ printer,
540
+ type: "blob_pdf",
541
+ templateId: "自定义Id,用于判断任务是否成功",
542
+ pdf_blob: Blob,
543
+ });
544
+
545
+ // 非vue-plugin-hiprint
546
+ socket.emit("news", {
547
+ client,
548
+ printer,
549
+ type: "blob_pdf",
550
+ templateId: "自定义Id,用于判断任务是否成功",
551
+ pdf_blob: Blob,
552
+ });
553
+ ```
554
+
555
+ ## 打印回调
556
+
557
+ ```js
558
+ socket.on("success", (res) => {
559
+ console.log(res.templateId);
560
+ // TODO: Do something for your project
561
+ });
562
+
563
+ socket.on("error", (res) => {
564
+ console.log(res.templateId);
565
+ // TODO: Do something for your project
566
+ });
567
+ ```
568
+
569
+ ## 模板+data 或 html 返回 jpeg、pdf、打印
570
+
571
+ > [!TIP]
572
+ > 该功能依赖 electron-hiprint@^1.0.12-beta7 版本
573
+
574
+ 现在,你可以通过对应 socket 事件,调用 electron-hiprint 生成 jpeg、矢量 pdf 和直接打印了。
575
+
576
+ 对于 vue-plugin-hiprint 模板,只需要提供 template(json、jsonString) 和 data(json) 即可。
577
+
578
+ 非 vue-plugin-hiprint 模板,你需要提供 html(需要提供完整的样式含 UI、项目内部样式)。
579
+
580
+ <div align="center">
581
+
582
+ ![image](./res/electron-hiprint_set_pluginVersion.png)
583
+
584
+ </div>
585
+
586
+ | apiName | 参数 | 说明 |
587
+ | -------------------- | --------------------------- | -------------------------------------------------- |
588
+ | render-jpeg | `template`,`data` / `html` | 调用 electron 生成 jpeg |
589
+ | render-jpeg-success | `templateId`,`buffer`,`msg` | 成功回调,返回 templateId 和生成的 jpeg 二进制数据 |
590
+ | render-jpeg-error | `templateId`,`msg` | 错误回调,返回 templateId 和错误信息 |
591
+ | render-pdf | `template`,`data` / `html` | 调用 electron 生成 pdf |
592
+ | render-pdf-success | `templateId`,`buffer`,`msg` | 成功回调,返回 templateId 和生成的 pdf 二进制数据 |
593
+ | render-pdf-error | `templateId`,`msg` | 错误回调,返回 templateId 和错误信息 |
594
+ | render-print | `template`,`data` / `html` | 调用 electron 打印 |
595
+ | render-print-success | `templateId`,`msg` | 成功回调,返回 templateId 和打印成功信息 |
596
+ | render-print-error | `templateId`,`msg` | 错误回调,返回 templateId 和错误信息 |
597
+
598
+ <details>
599
+ <summary>vue-plugin-hiprint</summary>
600
+
601
+ ```js
602
+ hiprint.hiwebSocket.socket.emit("render-jpeg", {
603
+ template: panel,
604
+ data: printData,
605
+ html: "heml字符串",
606
+ })
607
+ socket.on("render-jpeg-success", (data) => {
608
+ // data.buffer
609
+ });
610
+ socket.on("render-jpeg-error", (data) => {
611
+ // data.error
612
+ });
613
+
614
+ hiprint.hiwebSocket.socket.emit("render-pdf", {
615
+ template: panel,
616
+ data: printData,
617
+ html: "heml字符串",
618
+ })
619
+ socket.on("render-pdf-success", (data) => {
620
+ // data.buffer
621
+ });
622
+ socket.on("render-pdf-error", (data) => {
623
+ // data.error
624
+ });
625
+
626
+ hiprint.hiwebSocket.socket.emit("render-print", {
627
+ template: panel,
628
+ data: printData,
629
+ html: "heml字符串",
630
+ printer: "Microsoft Print to PDF",
631
+ ...
632
+ })
633
+ socket.on("render-print-success", (data) => {
634
+ // data.templateId
635
+ });
636
+ socket.on("render-print-error", (data) => {
637
+ // data.templateId
638
+ });
639
+ ```
640
+
641
+ </details>
642
+
643
+ <details open>
644
+ <summary>node.js demo</summary>
645
+
646
+ ```node
647
+ const io = require("socket.io-client");
648
+ const fs = require("fs");
649
+
650
+ const panel = require("./panel.json");
651
+ const printData = require("./print-data.json");
652
+
653
+ const socket = io("http://localhost:17521", {
654
+ transports: ["websocket"],
655
+ reconnectionAttempts: 5,
656
+ auth: {
657
+ token: "vue-plugin-hiprint",
658
+ },
659
+ });
660
+ socket.on("connect", () => {
661
+ socket.emit("render-jpeg", {
662
+ template: panel,
663
+ data: printData,
664
+ html: "heml字符串",
665
+ });
666
+ socket.on("render-jpeg-success", (data) => {
667
+ fs.writeFile("./capture.jpeg", data.buffer, () => {});
668
+ });
669
+ // render-pdf 同上
670
+ // render-print 同上
671
+ });
672
+ ```
673
+
674
+ </details>
675
+
676
+ 打印参数同上 [默认打印参数说明](#默认打印参数说明)
677
+
678
+ ## 断开连接
679
+
680
+ ```js
681
+ socket.on("disconnect", () => {
682
+ globalThis.connect = false;
683
+ // TODO: Do something for your project
684
+ });
685
+ ```
686
+
687
+ ## 打印记录
688
+
689
+ > [!TIP]
690
+ > 打印记录功能属于 ^1.0.12-beta1 功能
691
+
692
+ 客户端将会记录每一条 `news` ,你可以从这里查询历史打印记录,是否成功,重打操作等。
693
+
694
+ <div align="center">
695
+
696
+ ![打印记录](./res/Print_log.png)
697
+
698
+ </div>
699
+
700
+ ### 禁用重打
701
+
702
+ 1. 通过配置全局禁用重打
703
+
704
+ 适合全局禁用重打,只提供日志查询,需在全局配置中设置禁用(设置页面未显式提供设置)
705
+
706
+ - [覆盖默认配置方法](#覆盖默认配置方法)
707
+
708
+ 2. 通过 api 禁用单条数据重打
709
+
710
+ ```js
711
+ // socket.io-client
712
+ socket.emit("news", {
713
+ html,
714
+ templateId,
715
+ printer,
716
+ pageSize,
717
+ rePrintAble: false,
718
+ });
719
+ socket.emit("render-print", { template, data, rePrintAble: false });
720
+
721
+ // vue-plugin-hiprint
722
+ hiprintTemplate.print2(printData, { printer, title, rePrintAble: false });
723
+ hiprint.hiwebSocket.socket.emit("render-print", {
724
+ template,
725
+ data,
726
+ rePrintAble: false,
727
+ });
728
+ ```
729
+
730
+ ## URL Scheme 支持
731
+
732
+ 通过 `hiprint://` 协议,可以从 Web 项目中调起 `electron-hiprint` 客户端,以便未建立连接时主动唤起客户端。
733
+
734
+ > [!TIP]
735
+ > 注意: 安装客户端时需以管理员身份运行。
736
+
737
+ <div align="center">
738
+
739
+ ![URLScheme](./res/URLScheme.png)
740
+
741
+ ![alert弹框](./res/URLScheme_tips.png)
742
+
743
+ </div>
744
+
745
+ ```js
746
+ // js
747
+ window.open("hiprint://");
748
+
749
+ // element-ui
750
+ this.$alert(
751
+ `连接【${hiwebSocket.host}】失败!<br>请确保目标服务器已<a href="https://gitee.com/CcSimple/electron-hiprint/releases" target="_blank"> 下载 </a> 并 <a href="hiprint://" target="_blank"> 运行 </a> 打印服务!`,
752
+ "客户端未连接",
753
+ { dangerouslyUseHtmlString: true },
754
+ );
755
+
756
+ // ant-design v1
757
+ this.$error({
758
+ title: "客户端未连接",
759
+ content: (h) => (
760
+ <div>
761
+ 连接【{hiwebSocket.host}】失败!
762
+ <br />
763
+ 请确保目标服务器已
764
+ <a
765
+ href="https://gitee.com/CcSimple/electron-hiprint/releases"
766
+ target="_blank"
767
+ >
768
+ 下载
769
+ </a>并<a href="hiprint://" target="_blank">
770
+ 运行
771
+ </a>
772
+ 打印服务!
773
+ </div>
774
+ ),
775
+ });
776
+ ```
777
+
778
+ ## 周边生态项目
779
+
780
+ | 项目名称 | 项目地址 | 下载地址 | 描述 |
781
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- | ------------------------------------------------------------------ |
782
+ | vue-plugin-hiprint | [github](https://github.com/CcSimple/vue-plugin-hiprint)、[gitee](https://gitee.com/CcSimple/vue-plugin-hiprint) | [npm](https://www.npmjs.com/package/vue-plugin-hiprint) | 打印设计器 |
783
+ | electron-hiprint | [github](https://github.com/CcSimple/electron-hiprint)、[gitee](https://gitee.com/CcSimple/electron-hiprint) | [releases](https://github.com/CcSimple/electron-hiprint/releases) | 直接打印客户端 |
784
+ | node-hiprint-transit | [github](https://github.com/Xavier9896/node-hiprint-transit)、[gitee](https://gitee.com/Xavier9896/node-hiprint-transit) | [releases](https://github.com/Xavier9896/node-hiprint-transit/releases) | web 与客户端中转服务 Node 实现 |
785
+ | hiprint-transporter-java | [github](https://github.com/LyingDoc/hiprint-transit-java)、[gitee](https://gitee.com/dut_cc/hiprint-transporter-java) | - | web 与客户端中转服务 Java 实现 |
786
+ | hiprint-transit-java | [github](https://github.com/weaponready/hiprint-transit-java) | - | web 与客户端中转服务 Java 实现 |
787
+ | uni-app-hiprint | [github](https://github.com/Xavier9896/uni-app-hiprint) | - | uni-app 项目通过 webview 使用 vue-plugin-hiprint demo |
788
+ | node-hiprint-pdf | [github](https://github.com/CcSimple/node-hiprint-pdf) | - | 提供通过 node 对 vue-plugin-hiprint 模板生成 矢量 pdf、image、html |
789
+
790
+ ## 参考资源
791
+
792
+ - [electron](https://www.electronjs.org/zh/docs/latest/)
793
+ - [electron-egg](https://gitee.com/wallace5303/electron-egg/)
794
+ - [pdf-to-printer](https://github.com/artiebits/pdf-to-printer)
795
+ - [unix-printer](https://github.com/artiebits/unix-print)
796
+
797
+ ## 感谢
798
+
799
+ logo 设计:[橙色](mailto:tong567@foxmail.com)
package/index.js CHANGED
@@ -192,7 +192,11 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
192
192
  // 发送到客户端打印
193
193
  hiprintTemplate.print2(data || templateData, {
194
194
  printer: this.selectedPrinter,
195
- title: templateName
195
+ title: templateName,
196
+ pageSize: {
197
+ width: (templateJson.width || 210) * 1000,
198
+ height: (templateJson.height || 297) * 1000
199
+ }
196
200
  });
197
201
 
198
202
  // 监听打印结果
@@ -293,7 +297,7 @@ const showPrinterSelectionDialog = (printerList, templateJson, templateData, tem
293
297
  // window.silentHiprint = silentHiprint;
294
298
 
295
299
  export default {
296
- install (app, { router, pinia, isAdmin, clientPrint = false, clientUrl = 'http://localhost:17521' }) {
300
+ install (app, { router, pinia, isAdmin, clientPrint = false, clientUrl = 'http://localhost:17521', clientApp = "https://gitee.com/CcSimple/electron-hiprint/releases" }) {
297
301
  app.provide('$hiprint', hiprintFun);
298
302
  app.provide('$silentHiprint', silentHiprint);
299
303
 
@@ -304,6 +308,20 @@ export default {
304
308
  if (hiprint.hiwebSocket && !hiprint.hiwebSocket.opened) {
305
309
  window.hiwebSocket.host = clientUrl;
306
310
  hiprint.hiwebSocket.start();
311
+
312
+ // 添加连接状态检查和提示逻辑
313
+ setTimeout(() => {
314
+ if (!hiprint.hiwebSocket.opened) {
315
+ ElMessageBox.alert(
316
+ `连接【${clientUrl}】失败!<br>请确保已<a href="${clientApp}" target="_blank">下载</a>并<a href="hiprint://" target="_blank">运行</a>打印客户端!`,
317
+ '客户端未连接',
318
+ {
319
+ dangerouslyUseHtmlString: true,
320
+ confirmButtonText: '确定'
321
+ }
322
+ );
323
+ }
324
+ }, 3000);
307
325
  }
308
326
  };
309
327
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yh-hiprint",
3
- "version": "2.6.1",
3
+ "version": "2.6.2",
4
4
  "description": "Hiprint for Vue3 by NoahLiu in ForceCon in Hunan Changesha",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",