vg-print 1.0.1 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +192 -555
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,638 +1,275 @@
1
- vue-plugin-hiprint (基于 [hiprint 2.5.4](http://hiprint.io/)) 当时只是为了方便 <span style="color: red">我(并非 hiprint 原作者)</span> 在 vue 项目中引入使用,所以以此命名。
1
+ vg-print (基于 [hiprint 2.5.4](http://hiprint.io/)) 当时只是为了方便 <span style="color: red">我(并非 hiprint 原作者)</span> 在 vue 项目中引入使用,所以以此命名。
2
2
 
3
3
  此 <span style="color: red">插件</span> 仅仅是一个 <span style="color: red">JavaScript【工具库】</span> 而非 <span style="color: yellow">Vue【组件库】</span>,所以它默认不包含 demo 中的那些组件页面(demo 代码可复制使用)。
4
4
 
5
5
  由于 hiprint 官网最后一次更新时间为 2019 年【hiprint 2.5.4 是 [LGPL](#关于lgpl协议) 协议】,后在诸多使用者及反馈下进行了许多优化调整。
6
6
 
7
- ## vue-plugin-hiprint
8
-
9
- > [✨ 立即体验(Github 访问慢)](https://ccsimple.github.io/vue-plugin-hiprint/) <br/><br/> [✨ 国内访问(www.ibujian.cn)](https://www.ibujian.cn/) <br/><br/> [🌈 更新日志 (页面支持 Ctrl + F 搜索)](CHANGELOG.md) <br/><br/> [🐛 常见问题(入门必看!)](#常见问题) ⚠️⚠️ [📕 系列教程文章(入门必看!)](#文章链接) <br/><br/> [🚀 项目生态(打印客户端、node 服务端、uniapp)](#插件生态)
10
-
11
- > hiprint for Vue2.x / Vue3.x (基于 jQuery, 理论上其他框架可用。[react demo 分支](https://github.com/CcSimple/vue-plugin-hiprint/tree/react_demo))
12
-
13
- > **jQuery/uniapp (html/h5)** 项目 见下方 [jQuery/uniapp 项目使用](#jqueryuniapp-项目使用)
14
-
15
7
  > [!IMPORTANT]
16
8
  >
17
9
  > **注意事项**
18
10
  >
19
- > - NodeJs 需要 16.x 版本 (开发使用 16.18.1)
11
+ > - NodeJs 需要 18.x 版本 (开发使用 18.18.0)
20
12
  > - <div style="color: red">【vue-plugin-hiprint】与【hiprint.io官网】差异甚多,请忽混用!请忽混用!请忽混用!</div>
21
13
  > - <div style="color: orange">请使用项目关联的打印客户端,或者自行修改打印客户端的源码,以适配本项目的模板!</div>
22
- > - 主分支是融合版本的最新代码,如果你不需要修改 hiprint 相关代码. 请使用 npm 包的方式安装.
23
- > - 使用直接客户端时,本地开发连接没问题,部署到线上出现跨域无法连接打印客户端问题:
24
- > - [线上跨域问题,请升级 https! 说明:https://www.cnblogs.com/daysme/p/15493523.html](https://www.cnblogs.com/daysme/p/15493523.html)
25
- > - 如需提交 PR 请前往 github 合并后可自动发布 npm 包并同步代码到 gitee
26
- > - vue-plugin-hiprint 包不包含 UI 界面,需要自行处理。如果想更快速引入请查看 [sv-print 组件库](https://www.ibujian.cn/svp/)
27
-
28
- ## 文章链接
29
-
30
- 2元快速入门: [一文彻底入门可视化打印 vue-plugin-hiprint](https://mp.weixin.qq.com/s/R8juiaQsLhMmyZebv55tUA)
31
-
32
- 6元解锁云打印: [三端通用的云打印方案: uniapp + nodejs中转服务!(附源码)](https://mp.weixin.qq.com/s/3_zEsr6nIYAqKdDsKqhg7A)
33
-
34
- > 更多文章见公众号、小程序(文章,服务端预览,云打印)
14
+ > - 主分支是融合版本的最新代码,如果你不需要修改 hiprint 相关代码. 请使用 npm 包的方式安装
35
15
 
36
- <table>
37
- <tr>
38
- <td><img height="160" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/ww.png"/></td>
39
- <td><img height="160" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/wmp.jpg"/></td>
40
- </tr>
41
- </table>
16
+ # vg-print 使用与 API 说明
42
17
 
43
- ## 安装使用
18
+ ## 安装
44
19
 
45
20
  ```
46
- npm install vue-plugin-hiprint
21
+ npm i vg-print
47
22
  ```
48
23
 
49
- ```html
50
- <!--【必须】在index.html 文件中添加打印所需样式(cdn可能不稳定):-->
51
- <link
52
- rel="stylesheet"
53
- type="text/css"
54
- media="print"
55
- href="https://npmmirror.com/package/vue-plugin-hiprint/files/dist/print-lock.css"
56
- />
57
- <!-- OR -->
58
- <link
59
- rel="stylesheet"
60
- type="text/css"
61
- media="print"
62
- href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css"
63
- />
64
- <!-- 可以调整成 相对链接/自有链接, 【重要】名称需要一致 【print-lock.css】-->
65
- <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
66
- ```
24
+ ## 样式
67
25
 
68
- ## 项目截图
69
-
70
- <table>
71
- <tr>
72
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/img_0.png"/></td>
73
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/img_1.png"/></td>
74
- </tr>
75
- <tr>
76
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/img_2.png"/></td>
77
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/img_3.png"/></td>
78
- </tr>
79
- <tr>
80
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/gif00.gif"/></td>
81
- <td><img src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/gif01.gif"/></td>
82
- </tr>
83
- </table>
84
-
85
- ## 拖拽设计使用(推荐)
86
-
87
- ```javascript
88
- import { hiprint, defaultElementTypeProvider } from "vue-plugin-hiprint";
89
- // 初始化可拖拽的元素
90
- hiprint.init({
91
- providers: [new defaultElementTypeProvider()],
92
- });
93
- // $('.ep-draggable-item') 包含 tid, 与上边的 provider 中的 tid 对应 才能正常拖拽生成元素
94
- hiprint.PrintElementTypeManager.buildByHtml($(".ep-draggable-item"));
95
- hiprintTemplate = new hiprint.PrintTemplate({
96
- template: {}, // 模板json
97
- settingContainer: "#PrintElementOptionSetting", // 元素参数容器
98
- paginationContainer: ".hiprint-printPagination", // 多面板的容器, 实现多面板, 需要在添加一个 <div class="hiprint-printPagination"/>
99
- // ------- 下列是可选功能 -------
100
- // ------- 下列是可选功能 -------
101
- // ------- 下列是可选功能 -------
102
- // 图片选择功能
103
- onImageChooseClick: (target) => {
104
- // 测试 3秒后修改图片地址值
105
- setTimeout(() => {
106
- // target.refresh(url,options,callback)
107
- // callback(el, width, height) // 原元素,宽,高
108
- // target.refresh(url,false,(el,width,height)=>{
109
- // el.options.width = width;
110
- // el.designTarget.css('width', width + "pt");
111
- // el.designTarget.children('.resize-panel').trigger($.Event('click'));
112
- // })
113
- target.refresh(
114
- "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtAAAAIIAQMAAAB99EudAAAABlBMVEUmf8vG2O41LStnAAABD0lEQVR42u3XQQqCQBSAYcWFS4/QUTpaHa2jdISWLUJjjMpclJoPGvq+1WsYfiJCZ4oCAAAAAAAAAAAAAAAAAHin6pL9c6H/fOzHbRrP0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0u/SY9LS0tLS0tLS0tLS0n+edm+UlpaWlpaWlpaWlpaW/tl0Ndyzbno7/+tPTJdd1wal69dNa6abx+Lq6TSeYtK7BX/Diek0XULSZZrakPRtV0i6Hu/KIt30q4fM0pvBqvR9mvsQkZaW9gyJT+f5lsnzjR54xAk8mAUeJyMPwYFH98ALx5Jr0kRLLndT7b64UX9QR/0eAAAAAAAAAAAAAAAAAAD/4gpryzr/bja4QgAAAABJRU5ErkJggg==",
115
- {
116
- // auto: true, // 根据图片宽高自动等比(宽>高?width:height)
117
- // width: true, // 按宽调整高
118
- // height: true, // 按高调整宽
119
- real: true, // 根据图片实际尺寸调整(转pt)
120
- }
121
- );
122
- }, 3000);
123
- // target.getValue()
124
- // target.refresh(url)
125
- },
126
- // 自定义可选字体
127
- // 或者使用 hiprintTemplate.setFontList([])
128
- // 或元素中 options.fontList: []
129
- fontList: [
130
- { title: "微软雅黑", value: "Microsoft YaHei" },
131
- { title: "黑体", value: "STHeitiSC-Light" },
132
- { title: "思源黑体", value: "SourceHanSansCN-Normal" },
133
- { title: "王羲之书法体", value: "王羲之书法体" },
134
- { title: "宋体", value: "SimSun" },
135
- { title: "华为楷体", value: "STKaiti" },
136
- { title: "cursive", value: "cursive" },
137
- ],
138
- dataMode: 1, // 1:getJson 其他:getJsonTid 默认1
139
- history: true, // 是否需要 撤销重做功能
140
- onDataChanged: (type, json) => {
141
- // 模板发生改变回调
142
- console.log(type); // 新增、移动、删除、修改(参数调整)、大小、旋转
143
- console.log(json); // 返回 template
144
- },
145
- onUpdateError: (e) => {
146
- // 更新失败回调
147
- console.log(e);
148
- },
149
- });
150
- // 设计器的容器
151
- hiprintTemplate.design("#hiprint-printTemplate");
152
- ```
153
-
154
- ## 代码模式使用(不推荐)
155
-
156
- ```javascript
157
- import { hiprint, defaultElementTypeProvider } from "vue-plugin-hiprint";
158
- // 引入后使用示例
159
- hiprint.init();
160
- // 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
161
- var hiprintTemplate = new hiprint.PrintTemplate();
162
- var panel = hiprintTemplate.addPrintPanel({
163
- width: 100,
164
- height: 130,
165
- paperFooter: 340,
166
- paperHeader: 10,
167
- });
168
- //文本
169
- panel.addPrintText({
170
- options: {
171
- width: 140,
172
- height: 15,
173
- top: 20,
174
- left: 20,
175
- title: "hiprint插件手动添加text",
176
- textAlign: "center",
177
- },
178
- });
179
- //条形码
180
- panel.addPrintText({
181
- options: {
182
- width: 140,
183
- height: 35,
184
- top: 40,
185
- left: 20,
186
- title: "123456",
187
- textType: "barcode",
188
- },
189
- });
190
- //二维码
191
- panel.addPrintText({
192
- options: {
193
- width: 35,
194
- height: 35,
195
- top: 40,
196
- left: 165,
197
- title: "123456",
198
- textType: "qrcode",
199
- },
200
- });
201
- //长文本
202
- panel.addPrintLongText({
203
- options: {
204
- width: 180,
205
- height: 35,
206
- top: 90,
207
- left: 20,
208
- title: "长文本:hiprint是一个很好的webjs打印,浏览器在的地方他都可以运行",
209
- },
210
- });
211
- //打印
212
- hiprintTemplate.print({});
213
- ```
214
-
215
- ## vue/vue3 全局引入
216
-
217
- > 全局引入,方便在任何地方不引入直接调用打印
218
-
219
- ```javascript
220
- // main.js中 引入安装
221
- import { hiPrintPlugin } from "vue-plugin-hiprint";
222
- Vue.use(hiPrintPlugin, "$pluginName"); // $pluginName 为自定义名称
223
- hiPrintPlugin.disAutoConnect();
224
-
225
- /// 提供的全局方法:
226
-
227
- // this.$pluginName == hiprint 全局对象
228
- let hiprintTemplate = this.$pluginName.PrintTemplate({
229
- template: {}, // 模板json [对象]
230
- });
231
- hiprintTemplate.print({name:'i不简'});
232
-
233
- /// provider 不能为null, 可以为 undefined args: 同模板对应调用 print 方法
234
-
235
- // 1. 打印
236
- this.$print(undefined, templateJson, ...args);
237
- this.$print(provider, templateJson, ...args);
238
- // 2. 直接打印
239
- this.print2(undefined, templateJson, ...args);
240
- this.print2(provider, templateJson, ...args);
241
- ```
242
-
243
- ## jQuery/uniapp 项目使用
244
-
245
- > uniapp 需要嵌入到 web 浏览器中.(需要支持 window 全局对象环境)
246
-
247
- ```html
248
- <!-- index.html -->
249
- <head>
250
- <!-- 打印样式是必须的,你可以调整成自由链接, 注意 media="print" 名称 print-lock.css -->
251
- <link
252
- rel="stylesheet"
253
- type="text/css"
254
- media="print"
255
- href="https://unpkg.com/vue-plugin-hiprint@latest/dist/print-lock.css"
256
- />
257
- <!-- 下列使用的都是 unpkg提供的 稳定性未知, 建议下载自行处理 -->
258
- <!-- jquery 必须 -->
259
- <script src="https://unpkg.com/jquery@3.6.1/dist/jquery.js"></script>
260
- <!-- 条形码 -->
261
- <script src="https://unpkg.com/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
262
- <!-- 二维码、条形码 bwip-js -->
263
- <script src="https://unpkg.com/bwip-js@4.5.1/dist/bwip-js.js"></script>
264
- <!-- 数字转大写 -->
265
- <script src="https://unpkg.com/nzh@1.0.14/dist/nzh.min.js"></script>
266
- <!-- 颜色选择器 -->
267
- <script src="https://unpkg.com/@claviska/jquery-minicolors@2.3.6/jquery.minicolors.min.js"></script>
268
- <!-- 直接打印(print2)需要 -->
269
- <script src="https://unpkg.com/socket.io-client@4.5.1/dist/socket.io.min.js"></script>
270
- <!-- toPdf需要 -->
271
- <script src="https://unpkg.com/canvg@3.0.10/lib/umd.js"></script>
272
- <script src="https://unpkg.com/jspdf@2.5.1/dist/jspdf.umd.min.js"></script>
273
- <script src="https://unpkg.com/html2canvas@1.4.1/dist/html2canvas.js"></script>
274
- <!-- vue-plugin-hiprint 😃 -->
275
- <script src="https://unpkg.com/vue-plugin-hiprint@latest/dist/vue-plugin-hiprint.js"></script>
276
- </head>
277
- <body>
278
- <!-- 注意 defer -->
279
- <script defer>
280
- console.log("vue-plugin-hiprint");
281
- console.log(window["vue-plugin-hiprint"]);
282
- console.log("hiprint");
283
- // hiprint 以注入 全局
284
- console.log(hiprint);
285
- var autoConnect = window["vue-plugin-hiprint"].autoConnect,
286
- disAutoConnect = window["vue-plugin-hiprint"].disAutoConnect,
287
- defaultElementTypeProvider =
288
- window["vue-plugin-hiprint"].defaultElementTypeProvider;
289
- </script>
290
- </body>
291
- ```
292
-
293
- ## 常见问题
294
-
295
- > 打印重叠 / 样式问题
296
-
297
- ```javascript
298
- /**
299
- * 从 在index.html添加:
300
- * <link rel="stylesheet" type="text/css" media="print" href="https://npmmirror.com/package/vue-plugin-hiprint/files/dist/print-lock.css">
301
- * 或者
302
- * <link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css">
303
- * 以处理打印所需css, 当然你也可以自行处理
304
- * 比如: index.html目录下放一个print-lock.css, 然后在index.html添加:
305
- * <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css">
306
- */
307
-
308
- // 添加自定义样式
309
- hiprintTemplate.print(
310
- this.printData,
311
- {},
312
- {
313
- styleHandler: () => {
314
- // 这里拼接成放html->head标签内的css/style
315
- // 1.例如:使用hiprin官网的样式
316
- let css =
317
- '<link href="http://hiprint.io/Content/hiprint/css/print-lock.css" media="print" rel="stylesheet">';
318
- // 2.重写样式:所有文本红色
319
- css += "<style>.hiprint-printElement-text{color:red !important;}</style>";
320
- return css;
321
- },
322
- }
323
- );
324
- // 直接打印
325
- hiprintTemplate.print2(this.printData, {
326
- styleHandler: () => {
327
- // 这里拼接成放html->head标签内的css/style
328
- // 1.例如:使用hiprin官网的样式
329
- let css =
330
- '<link href="http://hiprint.io/Content/hiprint/css/print-lock.css" media="print" rel="stylesheet">';
331
- // 2.重写样式:所有文本红色
332
- css += "<style>.hiprint-printElement-text{color:red !important;}</style>";
333
- return css;
334
- },
335
- });
336
- ```
337
-
338
- > 取消自动 socket 连接 / socket 连接报错问题
339
-
340
- ```javascript
341
- /**
342
- * 取消自动连接
343
- */
344
- // 在main.js中设置
345
- import { hiPrintPlugin } from "vue-plugin-hiprint";
346
- Vue.use(hiPrintPlugin, "$hiprint", false);
347
- // hiPrintPlugin 同时提供了 disAutoConnect 方法
348
- hiPrintPlugin.disAutoConnect();
349
- // 在组件中使用 见: demo/design/index.vue
350
- import { disAutoConnect, autoConnect, hiprint } from "vue-plugin-hiprint";
351
- disAutoConnect();
352
- // 同时 export了 autoConnect,disAutoConnect 方法
353
- /**
354
- * 连接回调及打印
355
- */
356
- autoConnect((status, msg) => {
357
- if (status) {
358
- hiprintTemplate.print2(printData, {
359
- printer: "",
360
- title: "hiprint测试打印",
361
- });
362
- }
363
- });
364
- /**
365
- * socket连接报错?
366
- * 由于npm包更新到socket.io 3.x版本,官网提供的客户端,npm包是无法连接的
367
- * 请使用gitee提供的客户端, 同时gitee客户端可传更多的参数, 如是否打印颜色/打印份数/DPI等
368
- * 详情electron见:https://www.electronjs.org/zh/docs/latest/api/web-contents
369
- */
370
- ```
371
-
372
- > print/print2 打印回调
373
-
374
- ```javascript
375
- // 浏览器预览打印, 无法监听是否点击了 打印/取消 按钮
376
- hiprintTemplate.print(
377
- this.printData,
378
- {},
379
- {
380
- callback: () => {
381
- console.log("浏览器打印窗口已打开");
382
- },
383
- }
384
- );
385
- // 直接打印
386
- // 打印机名称: 通过 hiprintTemplate.getPrinterList() 获取 其中的 name
387
- hiprintTemplate.print2(printData, { printer: "打印机名称", title: "打印标题" });
388
- hiprintTemplate.on("printSuccess", function (data) {
389
- console.log("打印完成");
390
- });
391
- hiprintTemplate.on("printError", function (data) {
392
- console.log("打印失败");
393
- });
394
26
  ```
395
-
396
- > i18n 设置 ⬆️ 0.0.55-beta8
397
-
398
- 原生为简体中文,英语、德语、西班牙语、法语、意大利语、日语、俄语、繁体中文皆为 AI 机翻,欢迎帮助 [订正](https://github.com/CcSimple/vue-plugin-hiprint/tree/main/src/i18n)。
399
-
400
- 可在 init 时传入语言进行设置,默认为 `cn` 。
401
-
402
- ```js
403
- hiprint.init({
404
- lang: "en", // 设置语言 ['cn', 'en', 'de', 'es', 'fr', 'it', 'ja', 'ru', 'cn_tw']
405
- });
27
+ import 'vg-print/dist/style.css'
406
28
  ```
407
29
 
408
- > 直接打印 地址端口 与 Token 设置
30
+ `index.html` 添加:
409
31
 
410
- ```js
411
- hiprint.init({
412
- host: "http://localhost:17521", // 可在此处设置连接地址与端口号
413
- token: "token", // 可在此处设置连接 token 可缺省
414
- });
415
32
  ```
416
-
417
- ## 参与项目
418
-
419
- ```console
420
- git clone https://gitee.com/CcSimple/vue-plugin-hiprint.git
421
-
422
- // init
423
- cd vue-plugin-hiprint && npm i
424
-
425
- // 调试预览demo
426
- npm run serve
427
-
428
- // 打包demo (打包后生成在 demo 目录)
429
- npm run build-demo
430
-
431
- // 打包插件(vue-plugin-hiprint 插件资源)
432
- npm run build
33
+ <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
433
34
  ```
434
35
 
435
- ## demo 调试(显示打印 iframe)
36
+ ## 核心 API
436
37
 
437
- ```javascript
438
- // 快速显示/隐藏 打印iframe 方便调试  ̄□ ̄||
439
- // 在浏览器控制台输入:
440
- // 显示打印页面
441
- $("#app").css("display", "none");
442
- $("#hiwprint_iframe").css("visibility", "visible");
443
- $("#hiwprint_iframe").css("width", "100%");
444
- $("#hiwprint_iframe").css("height", "251.09mm"); // 这里替换个实际高度才能显示完
445
- // 显示vue页面
446
- $("#app").css("display", "block");
447
- $("#hiwprint_iframe").css("visibility", "hidden");
38
+ ```
39
+ import { hiprint, register, setConfig, createPrintTemplate, print, print2, toPdf, toImage } from 'vg-print'
448
40
  ```
449
41
 
450
- ## 配套直接打印客户端[electron-hiprint](https://gitee.com/CcSimple/electron-hiprint)
451
-
452
- > 使用本项目,请使用如下样子的直接打印客户端
453
-
454
- 支持 win、mac、linux 系统
42
+ - `register({ authKey, plugins })`
43
+ - `setConfig(config)`
44
+ - `createPrintTemplate(options)`
45
+ - `print({ templateJson, data, options, provider })`
46
+ - `print2({ templateJson, data, options, provider })`
47
+ - `toPdf({ template, data, name, pdfOptions })`
48
+ - `toImage({ template, data, imageOptions })`
49
+ - 直接访问 `hiprint.hiwebSocket` 获取连接与打印机列表
455
50
 
456
- > [国内 Gitee 下载](https://gitee.com/CcSimple/electron-hiprint/releases) <br/><br/> [Github 下载](https://github.com/CcSimple/electron-hiprint/releases)
51
+ ### 快速示例
457
52
 
458
- ![image](./res/tool.jpeg)
53
+ ```
54
+ import { register, createPrintTemplate } from 'vg-print'
55
+ import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
459
56
 
460
- ### URLScheme `hiprint://`
57
+ register({ authKey: '', plugins: [pluginEleBwip({})] })
461
58
 
462
- > 安装客户端时请 `以管理员身份运行` ,才能成功添加 URLScheme
59
+ const templateJson = { /* 模板json */ }
60
+ const t = createPrintTemplate({ template: templateJson })
61
+ t.print({ name: '测试' })
62
+ ```
463
63
 
464
- 使用:浏览器地址栏输入 `hiprint://` 并回车
64
+ ### Provider 插件示例
465
65
 
466
- ![URLScheme](./res/URLScheme.png)
66
+ ```
67
+ import { hiprint } from 'vg-print'
467
68
 
468
- ```js
469
- // js
470
- window.open("hiprint://");
69
+ export default function(options) {
70
+ const addElementTypes = function(context) {
71
+ context.removePrintElementTypes('vgModule')
72
+ context.addPrintElementTypes('vgModule', [
73
+ new hiprint.PrintElementTypeGroup('自定义', [
74
+ {
75
+ tid: 'vgModule.text',
76
+ type: 'text',
77
+ title: '文本',
78
+ options: { title: '文本', field: 'text', testData: '123' }
79
+ }
80
+ ])
81
+ ])
82
+ }
83
+ return { addElementTypes }
84
+ }
85
+ ```
471
86
 
472
- // element-ui
473
- this.$alert(
474
- `连接【${hiwebSocket.host}】失败!<br>请确保目标服务器已<a href="https://gitee.com/CcSimple/electron-hiprint/releases" target="_blank"> 下载 </a> 并 <a href="hiprint://" target="_blank"> 运行 </a> 打印服务!`,
475
- "客户端未连接",
476
- { dangerouslyUseHtmlString: true }
477
- );
87
+ 注册:
478
88
 
479
- // ant-design
480
- this.$error({
481
- title: "客户端未连接",
482
- content: (h) => (
483
- <div>
484
- 连接【{hiwebSocket.host}】失败!
485
- <br />
486
- 请确保目标服务器已
487
- <a
488
- href="https://gitee.com/CcSimple/electron-hiprint/releases"
489
- target="_blank"
490
- >
491
- 下载
492
- </a>并<a href="hiprint://" target="_blank">
493
- 运行
494
- </a>
495
- 打印服务!
496
- </div>
497
- ),
498
- });
89
+ ```
90
+ import provider from './provider'
91
+ register({ plugins: [() => provider] })
499
92
  ```
500
93
 
501
- ## 使用 [中转服务 node-hiprint-transit](https://github.com/Xavier9896/node-hiprint-transit) 实现代理
502
-
503
- 配套客户端打印一直存在跨域、无法连接局域网其余打印机、跨网段无法连接的问题,所以诞生了这个中转代理服务。在 `electron-hiprint` [v1.0.0.7](https://gitee.com/CcSimple/electron-hiprint/releases) 版本中添加了连接中转服务代理的设置,将会在 `electron-hiprint` 与 `node-hiprint-transit` 间建立通信,`vue-plugin-hiprint` 只需连接中转服务就能获取到所有连接中转服务的打印端信息,并且选择任意打印机进行打印。
94
+ ### 连接客户端与直接打印
504
95
 
505
- 连接中转服务只需要修改 host, 添加 token
96
+ ```
97
+ import { hiprint, createPrintTemplate } from 'vg-print'
506
98
 
507
- ```js
508
- import { hiprint } from "vue-plugin-hiprint";
99
+ hiprint.init({ host: 'https://v4.printjs.cn:17521', token: 'hiprint-17521' })
509
100
 
510
- hiprint.init({
511
- host: "https://v4.printjs.cn:17521", // 此处输入服务启动后的地址
512
- token: "hiprint-17521", // 用于鉴权的token,hiprint* (*可替换为[0-9a-zA-Z\-_]字符)
513
- });
101
+ const t = createPrintTemplate({ template })
102
+ t.print2({ name: '测试' }, { title: '标题', printer: 'HP LaserJet' })
103
+ ```
514
104
 
515
- // or
105
+ `hiprint.hiwebSocket` 可读取打印机列表与客户端信息:
516
106
 
517
- hiwebSocket.setHost("https://printjs.cn:17521", "vue-plugin-hiprint");
107
+ ```
108
+ const list = hiprint.hiwebSocket.getPrinterList()
109
+ const opened = hiprint.hiwebSocket.opened
518
110
  ```
519
111
 
520
- 具体使用请转至 [node-hiprint-transit](https://github.com/Xavier9896/node-hiprint-transit)
112
+ ## Vue3 组件
521
113
 
522
- 为此你需要作出这些改变:
114
+ ```
115
+ import { Designer, Header, Preview, DragBox } from 'vg-print/vue3'
116
+ ```
523
117
 
524
- 1. 你可以从 `hiwebSocket` 中获取到 `clients`、`printerList` ,里面都将包含 `client` 信息
525
- 2. print2、ippRequest、ippRequest api options 中需要添加 `client` 指定客户端
118
+ ### Designer
526
119
 
527
- eg:
120
+ 属性:`template`、`printData`、`templateKey`、`title`、`designOptions`、`autoConnect`、`paperList`、`config`、`plugins`、`authKey`
528
121
 
529
- ```js
530
- var clientId = "AlBaUCNs3AIMFPLZAAAh";
531
- var client = hiwebSocket.clients[clientId];
532
- var printer = hiwebSocket.printerList[0];
122
+ 方法:`init(template)`、`getTarget()`、`getTemplateJson()`、`setTemplateJson(json)`、`exportJson()`
533
123
 
534
- hiprintTemplate.print2(printData, {
535
- client: clientId,
536
- printer: client.printerList[n].name,
537
- title: "hiprint测试打印",
538
- });
124
+ 插槽:`header`、`other`
539
125
 
540
- hiprintTemplate.print2(printData, {
541
- client: printer.clientId,
542
- printer: printer.name,
543
- title: "hiprint测试打印",
544
- });
545
- ```
126
+ 示例:
546
127
 
547
- > 如果你不提供 client 中转服务将抛出一个 error
128
+ ```
129
+ <template>
130
+ <Designer :template="template" :config="config" :plugins="plugins">
131
+ <template #header>
132
+ <Header title="vg-print" />
133
+ </template>
134
+ </Designer>
135
+ <Preview ref="previewRef" />
136
+ <button @click="openPreview">预览</button>
137
+ <button @click="saveJson">导出JSON</button>
138
+ <button @click="loadJson">加载JSON</button>
139
+ <DragBox target="SVPrint" drag-key="tip" drag-style="width:160px;height:100px;left:20px;top:60px" :show="true">
140
+ <template #iconTitle>
141
+ <span>提示</span>
142
+ </template>
143
+ <template #body>
144
+ <div>自定义辅助区域</div>
145
+ </template>
146
+ </DragBox>
147
+
148
+ </template>
149
+ <script setup>
150
+ import { ref } from 'vue'
151
+ import { Designer, Header, Preview, DragBox } from 'vg-print/vue3'
152
+ import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
153
+
154
+ const template = ref({})
155
+ const config = ref({ showAdsorbLine: true, showPosition: true, showSizeBox: true })
156
+ const plugins = ref([pluginEleBwip({})])
157
+ const previewRef = ref(null)
158
+
159
+ function openPreview(){
160
+ const t = previewRef.value
161
+ const designerTarget = document.getElementById('SVPrint')
162
+ const printTemplate = t.getTarget() || null
163
+ // 若未手动设置,可从 Designer 暴露的方法获取模板实例
164
+ }
165
+
166
+ function saveJson(){
167
+ // 通过 Designer 暴露方法获取 json
168
+ }
169
+
170
+ function loadJson(){
171
+ // 设置新的模板 json
172
+ }
173
+ </script>
174
+ ```
548
175
 
549
- ## 插件生态
176
+ ### Header
550
177
 
551
- | 项目名称 | 项目地址 | 下载地址 | 描述 |
552
- |-----------------------| ------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |-----------------------------------------------------------------|
553
- | 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) | 打印设计器 |
554
- | electron-hiprint | [github](https://github.com/CcSimple/electron-hiprint)、[gitee](https://gitee.com/CcSimple/electron-hiprint) | [releases](https://github.com/CcSimple/electron-hiprint/releases) | 直接打印客户端 |
555
- | *node-hiprint-transit | [github](https://github.com/Xavier9896/node-hiprint-transit)、[gitee](https://gitee.com/Xavier9896/node-hiprint-transit) | - | web 与客户端中转服务 Node 实现 |
556
- | *hiprint-transit-java | [github](https://github.com/weaponready/hiprint-transit-java) | - | web 与客户端中转服务 Java 实现 |
557
- | *uni-app-hiprint | [github](https://github.com/Xavier9896/uni-app-hiprint) | - | uni-app 项目通过 webview 使用 vue-plugin-hiprint demo |
558
- | *node-hiprint-pdf | [github](https://github.com/CcSimple/node-hiprint-pdf) | - | 提供通过 node 对 vue-plugin-hiprint 模板生成 矢量 pdf、image、html |
559
- | *iboot-print | [github](https://github.com/anganing/iboot-print) | - | 提供通过 java HtmlUnit 对 vue-plugin-hiprint 模板生成 html(image、pdf待实现) |
178
+ 属性:`logoHtml`、`title`、`reEle`、`eleList`、`reMenu`、`menuList`、`onPreviewClick`
560
179
 
561
- > 带 * 项目为周边社区维护项目,更新迭代、兼容性、稳定性无法得到保证。
180
+ 插槽:`headerLeft`、`headerCenter`、`headerRight`
562
181
 
563
- ## 交流群
182
+ 示例:
564
183
 
565
- <table>
566
- <tr>
567
- <td><img height="180" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/group.png"/></td>
568
- <td><img height="180" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/wechat.png"/></td>
569
- </tr>
570
- </table>
184
+ ```
185
+ <Header title="vg-print" :onPreviewClick="onPreviewClick" />
186
+ ```
571
187
 
572
- > 群人数已超过 200 请 '加我好友' 备注加群, 邀你进群
188
+ ```
189
+ function onPreviewClick(e){
190
+ // 返回 true/false 控制是否阻止预览按钮默认行为
191
+ return true
192
+ }
193
+ ```
573
194
 
574
- ## 捐赠支持, 或者请我喝杯咖啡 ☕️
195
+ ### Preview
575
196
 
576
- > 如果对您有帮助,请点击右上角 ⭐Star 关注或扫码捐赠,感谢支持开源!
197
+ 属性:`printTemplate`、`printData`、`printerList`、`selectedPrinter`、`showPdf`、`showImg`、`showPrint2`、`modalShow`
577
198
 
578
- <table>
579
- <tr>
580
- <td><img height="180" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/zWechat.png"/></td>
581
- <td><img height="180" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/zAlipay.png"/></td>
582
- </tr>
583
- </table>
199
+ 方法:`init(template, data)`、`show()`、`getTarget()`、`print()`、`directPrint()`、`toPdf(name, options)`、`toImage(options)`、`refreshPrinters()`
584
200
 
585
- 本项目使用 <a href="https://jb.gg/OpenSourceSupport"><img height="40" width="100" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/WebStorm.svg"/></a><a href="https://jb.gg/OpenSourceSupport"><img width="50" height="50" src="https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/WebStorm_icon.svg"/></a> 开发
201
+ 示例:
586
202
 
587
- ## Star History
203
+ ```
204
+ <Preview ref="previewRef" :modalShow="modalShow" />
205
+
206
+ // 代码调用
207
+ previewRef.value.init(templateJson, { name: '预览数据' })
208
+ previewRef.value.show()
209
+ previewRef.value.print()
210
+ previewRef.value.toPdf('文件名')
211
+ previewRef.value.toImage({ type: 'image/jpeg', pixelRatio: 2 })
212
+ previewRef.value.refreshPrinters()
213
+ ```
588
214
 
589
- [![Star History Chart](https://api.star-history.com/svg?repos=CcSimple/vue-plugin-hiprint&type=Date)](https://star-history.com/#CcSimple/vue-plugin-hiprint&Date)
215
+ ### DragBox
590
216
 
591
- ## 分支说明
217
+ 属性:`target`、`dragKey`、`dragStyle`、`mode`、`show`
592
218
 
593
- > main: vue2.x + ant1.7.x 融合版 及 npm 包源代码
219
+ 插槽:`iconTitle`、`body`
594
220
 
595
- > npm_demo: vue2.x + ant1.7.x + npm 包使用 示例
221
+ 示例:
596
222
 
597
- > npm_demo_ele: vue2.x + ElementUi 2.x + npm 包使用 示例
223
+ ```
224
+ <DragBox target="SVPrint" drag-key="demo" drag-style="width:200px;height:200px;left:238px;top:95px" :show="true">
225
+ <template #iconTitle>
226
+ <span>自定义拖拽框</span>
227
+ </template>
228
+ <template #body>
229
+ <div>内容</div>
230
+ </template>
231
+ </DragBox>
232
+ ```
598
233
 
599
- > npm_demo_v3: vue3.x + vite + npm 包(0.0.18)使用 示例
234
+ ## 使用示例
600
235
 
601
- ## 关于如何融合处理
236
+ ```
237
+ import { register, createPrintTemplate } from 'vg-print'
238
+ import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
602
239
 
603
- > 自己融合请查看 vue.config.js 对比 hiprint.bundle.js
240
+ register({ plugins: [pluginEleBwip({})] })
241
+ const t = createPrintTemplate({ template })
242
+ t.print(data)
243
+ ```
604
244
 
605
- > webpack.config.js,是 npm 打包需要处理的
245
+ ```
246
+ import { Designer, Header, Preview } from 'vg-print/vue3'
247
+ ```
606
248
 
607
- ## 开源使用说明
249
+ ## 选项与参数对齐
608
250
 
609
- > npm 包是基于 hiprint 官网 2.5.4 版本基础做的调整及优化;<br/>
610
- > 本人对开源协议理解有限,如有侵权不合理的地方,请联系告知我;<br/>
251
+ - 设计器 `designOptions`:`{ grid: true, activePanel: true }`
252
+ - 配置 `config`:`showAdsorbLine`、`showPosition`、`showSizeBox` 等在渲染前设置(与站点一致)
253
+ - 纸张列表 `paperList`:传入 `{ type, width, height }[]` 控制常用纸张
254
+ - 插件 `plugins`:函数数组,返回 provider 并向 `hiprint` 注册元素分组
255
+ - 授权 `authKey`:保留参数位
611
256
 
612
- hiprint 开源协议如下:
257
+ ## 客户端与打印机
613
258
 
614
259
  ```
615
- /**
616
- * jQuery Hiprint 2.5.4
617
- *
618
- * Copyright (c) 2016-2021 www.hinnn.com. All rights reserved.
619
- *
620
- * Licensed under the LGPL or commercial licenses
621
- * To use it on other terms please contact us: hinnn.com@gmail.com
622
- *
623
- */
624
- ```
625
-
626
- ## 关于 LGPL 协议
260
+ import { hiprint } from 'vg-print'
261
+ hiprint.init({ host: 'https://v4.printjs.cn:17521', token: 'hiprint-17521' })
627
262
 
263
+ const printers = hiprint.hiwebSocket.getPrinterList()
264
+ const opened = hiprint.hiwebSocket.opened
628
265
  ```
629
- LGPL是GPL的一个为主要为类库使用设计的开源协议。和GPL要求任何使用/修改/衍生之GPL类库的的软件必须采用GPL协议不同。
630
-
631
- LGPL允许商业软件通过类库引用(link)方式使用LGPL类库而不需要开源商业软件的代码。这使得采用LGPL协议的开源代码可以被商业软件作为类库引用并发布和销售。
632
266
 
633
- 但是如果修改LGPL协议的代码或者衍生,则所有修改的代码,涉及修改部分的额外代码和衍生的代码都必须采用LGPL协议。
267
+ `print2` 传入 `options.printer` 指定打印机;如存在多客户端场景,结合 `client` 信息传入。
634
268
 
635
- 因此LGPL协议的开源代码很适合作为第三方类库被商业软件引用,但不适合希望以LGPL协议代码为基础,通过修改和衍生的方式做二次开发的商业软件采用。
269
+ ## 源码位置参考
636
270
 
637
- GPL/LGPL都保障原作者的知识产权,避免有人利用开源代码复制并开发类似的产品。
638
- ```
271
+ - 入口与核心导出:`src/index.js:88-128`
272
+ - 预览组件:`src/vue3/Preview.vue:1-63`
273
+ - 设计器组件:`src/vue3/Designer.vue:1-42`
274
+ - 头部组件:`src/vue3/Header.vue:1-38`
275
+ - 拖拽组件:`src/vue3/DragBox.vue:1-28`
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vg-print",
3
3
  "description": "hiprint for Vue2.x / Vue3.x 支持拖拽(分页(不分页)、表头表脚、样式设置、复制粘贴、缩放、撤销重做)生成打印模板、导出json模板数据、静默打印/获取MAC地址(借助客户端)",
4
- "version": "1.0.01",
4
+ "version": "1.0.02",
5
5
  "author": "vgSimple",
6
6
  "license": "MIT",
7
7
  "main": "dist/vg-print.cjs.js",