vg-print 1.0.13 → 1.0.14
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.
- package/README.md +1 -399
- package/dist/css/print-lock.css +181 -252
- package/dist/{vg-print.cjs.js → static/js/index-Brm9wb-r.js} +179 -179
- package/dist/{vg-print.es.js → static/js/index-DbWGr7Bv.js} +5002 -5190
- package/dist/{vg-print.umd.js → static/js/index-mEQdYWWs.js} +175 -175
- package/dist/style.css +1 -1
- package/package.json +9 -10
- package/dist/css/hiprint.css +0 -997
- /package/dist/{purify.es-CKk_t3XZ.js → static/js/purify.es-CKk_t3XZ.js} +0 -0
- /package/dist/{purify.es-CYVvLED6.cjs → static/js/purify.es-CYVvLED6.js} +0 -0
package/README.md
CHANGED
|
@@ -1,399 +1 @@
|
|
|
1
|
-
vg-print (基于 [hiprint 2.5.4]当时只是为了方便 <span style="color: red">我(并非 hiprint 原作者)</span> 在 vue 项目中引入使用,所以以此命名。
|
|
2
|
-
|
|
3
|
-
此 <span style="color: red">插件</span> 仅仅是一个 <span style="color: red">JavaScript【工具库】</span> 而非 <span style="color: yellow">Vue【组件库】</span>,所以它默认不包含 demo 中的那些组件页面(demo 代码可复制使用)。
|
|
4
|
-
|
|
5
|
-
由于 hiprint 官网最后一次更新时间为 2019 年【hiprint 2.5.4 是 [LGPL](#关于lgpl协议) 协议】,后在诸多使用者及反馈下进行了许多优化调整。
|
|
6
|
-
|
|
7
|
-
> [!IMPORTANT]
|
|
8
|
-
|
|
9
|
-
# vg-print 使用与 API 说明
|
|
10
|
-
|
|
11
|
-
## 安装
|
|
12
|
-
|
|
13
|
-
```
|
|
14
|
-
npm i vg-print
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## 样式
|
|
18
|
-
|
|
19
|
-
```
|
|
20
|
-
import 'vg-print/dist/style.css'
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
在 `index.html` 添加:
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
<link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## 核心 API
|
|
30
|
-
|
|
31
|
-
```
|
|
32
|
-
import { hiprint, register, setConfig, createPrintTemplate, print, print2, toPdf, toImage } from 'vg-print'
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
- `register({ authKey, plugins })`
|
|
36
|
-
- `setConfig(config)`
|
|
37
|
-
- `createPrintTemplate(options)`
|
|
38
|
-
- `print({ templateJson, data, options, provider })`
|
|
39
|
-
- `print2({ templateJson, data, options, provider })`
|
|
40
|
-
- `toPdf({ template, data, name, pdfOptions })`
|
|
41
|
-
- `toImage({ template, data, imageOptions })`
|
|
42
|
-
- 直接访问 `hiprint.hiwebSocket` 获取连接与打印机列表
|
|
43
|
-
|
|
44
|
-
### 快速示例
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
import { register, createPrintTemplate } from 'vg-print'
|
|
48
|
-
import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
|
|
49
|
-
|
|
50
|
-
register({ authKey: '', plugins: [pluginEleBwip({})] })
|
|
51
|
-
|
|
52
|
-
const templateJson = { /* 模板json */ }
|
|
53
|
-
const t = createPrintTemplate({ template: templateJson })
|
|
54
|
-
t.print({ name: '测试' })
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Provider 插件示例
|
|
58
|
-
|
|
59
|
-
```
|
|
60
|
-
import { hiprint } from 'vg-print'
|
|
61
|
-
|
|
62
|
-
export default function(options) {
|
|
63
|
-
const addElementTypes = function(context) {
|
|
64
|
-
context.removePrintElementTypes('vgModule')
|
|
65
|
-
context.addPrintElementTypes('vgModule', [
|
|
66
|
-
new hiprint.PrintElementTypeGroup('自定义', [
|
|
67
|
-
{
|
|
68
|
-
tid: 'vgModule.text',
|
|
69
|
-
type: 'text',
|
|
70
|
-
title: '文本',
|
|
71
|
-
options: { title: '文本', field: 'text', testData: '123' }
|
|
72
|
-
}
|
|
73
|
-
])
|
|
74
|
-
])
|
|
75
|
-
}
|
|
76
|
-
return { addElementTypes }
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
注册:
|
|
81
|
-
|
|
82
|
-
```
|
|
83
|
-
import provider from './provider'
|
|
84
|
-
register({ plugins: [() => provider] })
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### 连接客户端与直接打印
|
|
88
|
-
|
|
89
|
-
```
|
|
90
|
-
import { hiprint, createPrintTemplate } from 'vg-print'
|
|
91
|
-
|
|
92
|
-
hiprint.init({ host: 'https://v4.printjs.cn:17521', token: 'hiprint-17521' })
|
|
93
|
-
|
|
94
|
-
const t = createPrintTemplate({ template })
|
|
95
|
-
t.print2({ name: '测试' }, { title: '标题', printer: 'HP LaserJet' })
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
`hiprint.hiwebSocket` 可读取打印机列表与客户端信息:
|
|
99
|
-
|
|
100
|
-
```
|
|
101
|
-
const list = hiprint.hiwebSocket.getPrinterList()
|
|
102
|
-
const opened = hiprint.hiwebSocket.opened
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
## Vue3 组件
|
|
106
|
-
|
|
107
|
-
```
|
|
108
|
-
import { Designer, Header, Preview, DragBox } from 'vg-print/vue3'
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Designer
|
|
112
|
-
|
|
113
|
-
属性:`template`、`printData`、`templateKey`、`title`、`designOptions`、`autoConnect`、`paperList`、`config`、`plugins`、`authKey`
|
|
114
|
-
|
|
115
|
-
方法:`init(template)`、`getTarget()`、`getTemplateJson()`、`setTemplateJson(json)`、`exportJson()`
|
|
116
|
-
|
|
117
|
-
插槽:`header`、`other`
|
|
118
|
-
|
|
119
|
-
示例:
|
|
120
|
-
|
|
121
|
-
```
|
|
122
|
-
<template>
|
|
123
|
-
<Designer :template="template" :config="config" :plugins="plugins">
|
|
124
|
-
<template #header>
|
|
125
|
-
<Header title="vg-print" />
|
|
126
|
-
</template>
|
|
127
|
-
</Designer>
|
|
128
|
-
<Preview ref="previewRef" />
|
|
129
|
-
<button @click="openPreview">预览</button>
|
|
130
|
-
<button @click="saveJson">导出JSON</button>
|
|
131
|
-
<button @click="loadJson">加载JSON</button>
|
|
132
|
-
<DragBox target="SVPrint" drag-key="tip" drag-style="width:160px;height:100px;left:20px;top:60px" :show="true">
|
|
133
|
-
<template #iconTitle>
|
|
134
|
-
<span>提示</span>
|
|
135
|
-
</template>
|
|
136
|
-
<template #body>
|
|
137
|
-
<div>自定义辅助区域</div>
|
|
138
|
-
</template>
|
|
139
|
-
</DragBox>
|
|
140
|
-
|
|
141
|
-
</template>
|
|
142
|
-
<script setup>
|
|
143
|
-
import { ref } from 'vue'
|
|
144
|
-
import { Designer, Header, Preview, DragBox } from 'vg-print/vue3'
|
|
145
|
-
import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
|
|
146
|
-
|
|
147
|
-
const template = ref({})
|
|
148
|
-
const config = ref({ showAdsorbLine: true, showPosition: true, showSizeBox: true })
|
|
149
|
-
const plugins = ref([pluginEleBwip({})])
|
|
150
|
-
const previewRef = ref(null)
|
|
151
|
-
|
|
152
|
-
function openPreview(){
|
|
153
|
-
const t = previewRef.value
|
|
154
|
-
const designerTarget = document.getElementById('SVPrint')
|
|
155
|
-
const printTemplate = t.getTarget() || null
|
|
156
|
-
// 若未手动设置,可从 Designer 暴露的方法获取模板实例
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function saveJson(){
|
|
160
|
-
// 通过 Designer 暴露方法获取 json
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function loadJson(){
|
|
164
|
-
// 设置新的模板 json
|
|
165
|
-
}
|
|
166
|
-
</script>
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### Header
|
|
170
|
-
|
|
171
|
-
属性:`logoHtml`、`title`、`reEle`、`eleList`、`reMenu`、`menuList`、`onPreviewClick`
|
|
172
|
-
|
|
173
|
-
插槽:`headerLeft`、`headerCenter`、`headerRight`
|
|
174
|
-
|
|
175
|
-
示例:
|
|
176
|
-
|
|
177
|
-
```
|
|
178
|
-
<Header title="vg-print" :onPreviewClick="onPreviewClick" />
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
```
|
|
182
|
-
function onPreviewClick(e){
|
|
183
|
-
// 返回 true/false 控制是否阻止预览按钮默认行为
|
|
184
|
-
return true
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### Preview
|
|
189
|
-
|
|
190
|
-
属性:`printTemplate`、`printData`、`printerList`、`selectedPrinter`、`showPdf`、`showImg`、`showPrint2`、`modalShow`
|
|
191
|
-
|
|
192
|
-
方法:`init(template, data)`、`show()`、`getTarget()`、`print()`、`directPrint()`、`toPdf(name, options)`、`toImage(options)`、`refreshPrinters()`
|
|
193
|
-
|
|
194
|
-
示例:
|
|
195
|
-
|
|
196
|
-
```
|
|
197
|
-
<Preview ref="previewRef" :modalShow="modalShow" />
|
|
198
|
-
|
|
199
|
-
// 代码调用
|
|
200
|
-
previewRef.value.init(templateJson, { name: '预览数据' })
|
|
201
|
-
previewRef.value.show()
|
|
202
|
-
previewRef.value.print()
|
|
203
|
-
previewRef.value.toPdf('文件名')
|
|
204
|
-
previewRef.value.toImage({ type: 'image/jpeg', pixelRatio: 2 })
|
|
205
|
-
previewRef.value.refreshPrinters()
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### DragBox
|
|
209
|
-
|
|
210
|
-
属性:`target`、`dragKey`、`dragStyle`、`mode`、`show`
|
|
211
|
-
|
|
212
|
-
插槽:`iconTitle`、`body`
|
|
213
|
-
|
|
214
|
-
示例:
|
|
215
|
-
|
|
216
|
-
```
|
|
217
|
-
<DragBox target="SVPrint" drag-key="demo" drag-style="width:200px;height:200px;left:238px;top:95px" :show="true">
|
|
218
|
-
<template #iconTitle>
|
|
219
|
-
<span>自定义拖拽框</span>
|
|
220
|
-
</template>
|
|
221
|
-
<template #body>
|
|
222
|
-
<div>内容</div>
|
|
223
|
-
</template>
|
|
224
|
-
</DragBox>
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
## 使用示例
|
|
228
|
-
|
|
229
|
-
```
|
|
230
|
-
import { register, createPrintTemplate } from 'vg-print'
|
|
231
|
-
import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
|
|
232
|
-
|
|
233
|
-
register({ plugins: [pluginEleBwip({})] })
|
|
234
|
-
const t = createPrintTemplate({ template })
|
|
235
|
-
t.print(data)
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
```
|
|
239
|
-
import { Designer, Header, Preview } from 'vg-print/vue3'
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## 选项与参数对齐
|
|
243
|
-
|
|
244
|
-
- 设计器 `designOptions`:`{ grid: true, activePanel: true }`
|
|
245
|
-
- 配置 `config`:`showAdsorbLine`、`showPosition`、`showSizeBox` 等在渲染前设置(与站点一致)
|
|
246
|
-
- 纸张列表 `paperList`:传入 `{ type, width, height }[]` 控制常用纸张
|
|
247
|
-
- 插件 `plugins`:函数数组,返回 provider 并向 `hiprint` 注册元素分组
|
|
248
|
-
- 授权 `authKey`:保留参数位
|
|
249
|
-
|
|
250
|
-
## 客户端与打印机
|
|
251
|
-
|
|
252
|
-
```
|
|
253
|
-
import { hiprint } from 'vg-print'
|
|
254
|
-
hiprint.init({ host: 'https://v4.printjs.cn:17521', token: 'hiprint-17521' })
|
|
255
|
-
|
|
256
|
-
const printers = hiprint.hiwebSocket.getPrinterList()
|
|
257
|
-
const opened = hiprint.hiwebSocket.opened
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
`print2` 传入 `options.printer` 指定打印机;如存在多客户端场景,结合 `client` 信息传入。
|
|
261
|
-
|
|
262
|
-
## 源码位置参考
|
|
263
|
-
|
|
264
|
-
- 入口与核心导出:`src/index.js:88-128`
|
|
265
|
-
- 预览组件:`src/vue3/Preview.vue:1-63`
|
|
266
|
-
- 设计器组件:`src/vue3/Designer.vue:1-42`
|
|
267
|
-
- 头部组件:`src/vue3/Header.vue:1-38`
|
|
268
|
-
- 拖拽组件:`src/vue3/DragBox.vue:1-28`
|
|
269
|
-
|
|
270
|
-
## 方法使用案例集合
|
|
271
|
-
|
|
272
|
-
### 注册与 Provider 插件
|
|
273
|
-
|
|
274
|
-
```
|
|
275
|
-
import { register } from 'vg-print'
|
|
276
|
-
import pluginEleBwip from '@vg-print/plugin-ele-bwip-js'
|
|
277
|
-
|
|
278
|
-
register({
|
|
279
|
-
authKey: '',
|
|
280
|
-
plugins: [
|
|
281
|
-
pluginEleBwip({ /* 插件参数 */ })
|
|
282
|
-
]
|
|
283
|
-
})
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
### 全局配置 setConfig
|
|
287
|
-
|
|
288
|
-
```
|
|
289
|
-
import { setConfig } from 'vg-print'
|
|
290
|
-
|
|
291
|
-
setConfig({
|
|
292
|
-
showAdsorbLine: true,
|
|
293
|
-
showPosition: true,
|
|
294
|
-
showSizeBox: true
|
|
295
|
-
})
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
### 设计器渲染与获取模板
|
|
299
|
-
|
|
300
|
-
```
|
|
301
|
-
import { createPrintTemplate } from 'vg-print'
|
|
302
|
-
|
|
303
|
-
const t = createPrintTemplate({ template: templateJson })
|
|
304
|
-
t.design('#SVPrint', { grid: true })
|
|
305
|
-
const html = t.getHtml(printData) // 参考 src/views/preview.vue:95-101
|
|
306
|
-
const json = t.getJson() // 参考 src/views/index.vue:882
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
### 设置与导出模板 JSON
|
|
310
|
-
|
|
311
|
-
```
|
|
312
|
-
t.setJson(newJson)
|
|
313
|
-
const exported = t.getJson()
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
### 浏览器打印
|
|
317
|
-
|
|
318
|
-
```
|
|
319
|
-
t.print(printData, { /* options */ })
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
### 直接打印(需客户端)
|
|
323
|
-
|
|
324
|
-
```
|
|
325
|
-
t.print2(printData, {
|
|
326
|
-
title: '打印标题',
|
|
327
|
-
printer: 'HP LaserJet'
|
|
328
|
-
})
|
|
329
|
-
// 判断连接与读取打印机列表
|
|
330
|
-
hiprint.hiwebSocket.opened // 参考 src/views/preview.vue:59-66
|
|
331
|
-
const printers = hiprint.hiwebSocket.getPrinterList() // 参考 src/hiprint/hiprint.bundle.js:7779
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
### 导出 PDF(两类模式)
|
|
335
|
-
|
|
336
|
-
1) 普通模板,导出预览全部页:
|
|
337
|
-
|
|
338
|
-
```
|
|
339
|
-
t.toPdf(printData, '打印预览pdf')
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
2) 小模板 A4 排版(把小模板排到 A4)参考 `src/views/index.vue:704-744`:
|
|
343
|
-
|
|
344
|
-
```
|
|
345
|
-
t.toPdf(printData, '打印预览pdf', {
|
|
346
|
-
paperWidth: 210,
|
|
347
|
-
paperHeight: 297,
|
|
348
|
-
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
349
|
-
})
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
### 导出图片(多页合成、清晰度、排版)
|
|
353
|
-
|
|
354
|
-
```
|
|
355
|
-
// 多张图片,每张合成 6 页,自动下载 JPEG(参考 src/views/index.vue:704-744)
|
|
356
|
-
await t.toImage(printData, {
|
|
357
|
-
limit: 6,
|
|
358
|
-
isDownload: true,
|
|
359
|
-
name: 'A4排版',
|
|
360
|
-
type: 'image/jpeg',
|
|
361
|
-
pixelRatio: 2,
|
|
362
|
-
quality: 0.8,
|
|
363
|
-
paperWidth: 210,
|
|
364
|
-
paperHeight: 297,
|
|
365
|
-
toType: 'url'
|
|
366
|
-
})
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
### 客户端高级 API(选用)
|
|
370
|
-
|
|
371
|
-
```
|
|
372
|
-
// 刷新打印机列表
|
|
373
|
-
hiprint.hiwebSocket.refreshPrinterList() // 参考 src/hiprint/hiprint.bundle.js:7782
|
|
374
|
-
|
|
375
|
-
// 获取客户端列表与信息
|
|
376
|
-
hiprint.hiwebSocket.getClients() // 参考 src/hiprint/hiprint.bundle.js:7789
|
|
377
|
-
hiprint.hiwebSocket.getClientInfo() // 参考 src/hiprint/hiprint.bundle.js:12551-12554
|
|
378
|
-
|
|
379
|
-
// IPP 打印与请求
|
|
380
|
-
hiprint.hiwebSocket.ippPrint({ /* options */ }) // 参考 src/hiprint/hiprint.bundle.js:7803
|
|
381
|
-
hiprint.hiwebSocket.ippRequest({ /* options */ }) // 参考 src/hiprint/hiprint.bundle.js:7810
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
### Vue 插件全局快捷:$print / $print2
|
|
385
|
-
|
|
386
|
-
```
|
|
387
|
-
// main.js
|
|
388
|
-
import { createApp } from 'vue'
|
|
389
|
-
import App from './App.vue'
|
|
390
|
-
import { hiPrintPlugin } from 'vg-print'
|
|
391
|
-
|
|
392
|
-
const app = createApp(App)
|
|
393
|
-
app.use(hiPrintPlugin) // 全局挂载 this.$print / this.$print2 参考 src/index.js:34-75
|
|
394
|
-
app.mount('#app')
|
|
395
|
-
|
|
396
|
-
// 组件中使用
|
|
397
|
-
this.$print(undefined, templateJson, printData, options)
|
|
398
|
-
this.$print2(undefined, templateJson, printData, options)
|
|
399
|
-
```
|
|
1
|
+
vg-print (基于 [hiprint 2.5.4](http://hiprint.io/)) 当时只是为了方便 <span style="color: red">我(并非 hiprint 原作者)</span> 在 vue 项目中引入使用,所以以此命名。
|