vg-print 1.0.344 → 1.0.346
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 +164 -457
- package/dist/style.css +1 -1
- package/dist/vg-print.es.js +4240 -4045
- package/dist/vg-print.umd.js +91 -91
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,533 +1,240 @@
|
|
|
1
1
|
# vg-print
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-

|
|
5
|
-
您可以直接安装引入体验功能.
|
|
6
|
-
没有域名限制,无网可用. 提供一对一技术支持.
|
|
7
|
-
npm搜索vg-print.
|
|
8
|
-
联系我加 QQ: 984239686 或使用首页有微信号加入
|
|
9
|
-
|
|
10
|
-
# 静默打印客户端工具安装包
|
|
11
|
-
|
|
12
|
-

|
|
13
|
-
|
|
14
|
-
更多信息与下载:<https://gitee.com/gao111/electron-vg-print>
|
|
15
|
-
|
|
16
|
-
## 特性
|
|
17
|
-
|
|
18
|
-
- 全功能页面组件 `FullDesigner`,直接使用,无需拼装子组件
|
|
19
|
-
- 支持预览、浏览器打印、导出 PDF/图片、直接打印(需客户端)
|
|
20
|
-
- 通过属性配置 `hiwebSocket` 的 `host`、`token` 与自动连接
|
|
21
|
-
- 暴露实例方法:预览、打印、导出、连接控制等,外部可直接调用
|
|
22
|
-
- 集成并导出 `hiprint`,可进行模板对象级的高级定制
|
|
3
|
+
vg-print Vue3.x 支持拖拽(分页(不分页)、表头表脚、样式设置、复制粘贴、缩放、撤销重做)生成打印模板、导出json模板数据、静默打印/获取MAC地址(借助客户端)
|
|
23
4
|
|
|
24
5
|
## 安装
|
|
25
6
|
|
|
7
|
+
```bash
|
|
8
|
+
npm install vg-print --save
|
|
26
9
|
```
|
|
27
|
-
npm i vg-print
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
样式引入(确保设计与打印样式生效):
|
|
31
10
|
|
|
32
|
-
|
|
33
|
-
// 全局样式(组件库样式)
|
|
34
|
-
import 'vg-print/style.css'
|
|
11
|
+
## 引入
|
|
35
12
|
|
|
36
|
-
|
|
37
|
-
// 在 `index.html` 中引入:
|
|
38
|
-
<link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css" />
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
环境要求:Vue 3、Node >= 18.18。无需单独安装 `vue-plugin-hiprint`,本库已内置并导出 `hiprint` 能力。
|
|
42
|
-
|
|
43
|
-
## 快速开始
|
|
44
|
-
|
|
45
|
-
```js
|
|
46
|
-
// main.ts
|
|
13
|
+
```javascript
|
|
47
14
|
import { createApp } from 'vue'
|
|
48
15
|
import App from './App.vue'
|
|
49
16
|
import vgPrint from 'vg-print'
|
|
50
|
-
import 'vg-print/style.css'
|
|
17
|
+
import 'vg-print/dist/style.css'
|
|
51
18
|
|
|
52
|
-
createApp(App)
|
|
19
|
+
const app = createApp(App)
|
|
20
|
+
app.use(vgPrint)
|
|
21
|
+
app.mount('#app')
|
|
53
22
|
```
|
|
54
23
|
|
|
55
|
-
|
|
56
|
-
<template>
|
|
57
|
-
<FullDesigner
|
|
58
|
-
ref="designer"
|
|
59
|
-
:hi-host="'http://127.0.0.1:17521'"
|
|
60
|
-
:hi-token="'your-token'"
|
|
61
|
-
:hi-auto-connect="true"
|
|
62
|
-
/>
|
|
24
|
+
## 组件列表
|
|
63
25
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
<el-button @click="exportImage">导出图片</el-button>
|
|
68
|
-
<el-button @click="directPrint">直接打印(需客户端)</el-button>
|
|
69
|
-
</template>
|
|
26
|
+
- `FullDesigner`: 完整设计器组件
|
|
27
|
+
- `Header`: 头部组件
|
|
28
|
+
- `Preview`: 预览组件
|
|
70
29
|
|
|
71
|
-
|
|
72
|
-
import { ref } from 'vue'
|
|
73
|
-
const designer = ref(null)
|
|
30
|
+
## FullDesigner 使用
|
|
74
31
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// 直接打印(需客户端已连接)
|
|
80
|
-
const exportPdf = () => designer.value.toPdf()
|
|
81
|
-
// 导出图片
|
|
82
|
-
const exportImage = () => designer.value.toImage()
|
|
83
|
-
// 直接打印(需客户端已连接)
|
|
84
|
-
const directPrint = () => designer.value.print()
|
|
85
|
-
|
|
86
|
-
// 连接控制与参数设置
|
|
87
|
-
const setHostToken = () => designer.value.setHiwebSocket('http://127.0.0.1:17521', 'token')
|
|
88
|
-
// 主动连接客户端
|
|
89
|
-
const connect = () => designer.value.connect()
|
|
90
|
-
// 断开客户端
|
|
91
|
-
const disconnect = () => designer.value.disconnect()
|
|
92
|
-
// 关闭自动连接策略
|
|
93
|
-
const disableAutoConnect = () => designer.value.disAutoConnect()
|
|
94
|
-
</script>
|
|
32
|
+
```vue
|
|
33
|
+
<template>
|
|
34
|
+
<FullDesigner />
|
|
35
|
+
</template>
|
|
95
36
|
```
|
|
96
37
|
|
|
97
|
-
##
|
|
38
|
+
## 注意事项
|
|
98
39
|
|
|
99
|
-
|
|
40
|
+
- 直接打印需配套客户端(Electron 或服务端),跨域部署需启用 HTTPS
|
|
41
|
+
- 若使用自定义 `host/token`,确保客户端配置一致且网络可达
|
|
42
|
+
- SSR 兼容:插件安装仅在浏览器环境执行(`typeof window !== 'undefined'`),在 SSR 框架中引入不会因 `window` 未定义报错。
|
|
100
43
|
|
|
101
|
-
|
|
102
|
-
- `hi-token: string` 客户端鉴权令牌;与客户端配置一致时方可直连打印
|
|
103
|
-
- `hi-auto-connect: boolean` 是否自动连接客户端;默认 `true`,为 `false` 时需手动调用 `connect()`
|
|
104
|
-
- `initial-template: object` 初始化模板 JSON;为空时使用内置示例模板
|
|
105
|
-
- `initial-print-data: object | array` 初始化打印数据;支持对象或数组,运行时变更会即时生效
|
|
106
|
-
- `default-lang: string` 默认语言;支持 `cn`、`en`、`de`、`es`、`fr`、`it`、`ja`、`ru`、`cn_tw`;默认 `cn`
|
|
44
|
+
## 构建
|
|
107
45
|
|
|
108
|
-
|
|
46
|
+
开发与构建:
|
|
109
47
|
|
|
110
|
-
|
|
48
|
+
```
|
|
49
|
+
npm run dev # 开发
|
|
50
|
+
npm run build # 应用构建
|
|
51
|
+
npm run build:lib# 组件库构建
|
|
52
|
+
```
|
|
111
53
|
|
|
112
|
-
|
|
113
|
-
- `printView()` 浏览器打印;不依赖客户端,调起系统打印对话框
|
|
114
|
-
- `print()` 直接打印;需客户端已连接,按当前选定打印机输出
|
|
115
|
-
- `toPdf()` 导出 PDF;使用内置 A4 排版示例参数导出
|
|
116
|
-
- `toImage()` 导出图片;使用内置 A4 排版示例参数导出
|
|
117
|
-
- `setHiwebSocket(host: string, token?: string, cb?: Function)` 设置客户端地址与令牌并尝试重连;`cb(status, msg)` 为连接回调
|
|
118
|
-
- `connect(cb?: Function)` 主动连接客户端;`cb(status, msg)` 为连接回调
|
|
119
|
-
- `disconnect()` 断开客户端连接
|
|
120
|
-
- `disAutoConnect()` 关闭自动连接策略;阻止自动重连
|
|
54
|
+
## 源码位置
|
|
121
55
|
|
|
122
|
-
|
|
56
|
+
- 入口与导出:`packages/index.ts`
|
|
57
|
+
- 完整页面组件:`src/views/index.vue`
|
|
123
58
|
|
|
124
|
-
|
|
59
|
+
## 完整示例与头部定制
|
|
125
60
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
data, // 当前打印数据(对象或数组)
|
|
130
|
-
templateId // 模板标识(若存在)
|
|
131
|
-
}
|
|
132
|
-
```
|
|
61
|
+
`Header` 组件已完全内置于 `FullDesigner` 中。您**不需要**单独引入或编写 `<Header>` 标签,只需通过 `FullDesigner` 的属性和插槽即可实现对头部的完全控制。
|
|
62
|
+
|
|
63
|
+
### 1. 基础定制:标题与 Logo (Props)
|
|
133
64
|
|
|
134
|
-
|
|
65
|
+
最简单的用法,通过属性直接配置。
|
|
135
66
|
|
|
136
67
|
```vue
|
|
137
68
|
<template>
|
|
138
69
|
<FullDesigner
|
|
139
70
|
:initial-template="tpl"
|
|
140
|
-
|
|
141
|
-
|
|
71
|
+
title="我的打印设计"
|
|
72
|
+
logo-html="<img src='https://example.com/logo.png' height='28px' />"
|
|
142
73
|
@save="onSave"
|
|
143
74
|
/>
|
|
144
75
|
</template>
|
|
145
|
-
|
|
146
|
-
<script setup>
|
|
147
|
-
const onSave = ({ template, data, templateId }) => {
|
|
148
|
-
// 调用接口持久化到数据库
|
|
149
|
-
// await api.saveTemplate({ template, data, templateId })
|
|
150
|
-
}
|
|
151
|
-
</script>
|
|
152
76
|
```
|
|
153
77
|
|
|
154
|
-
###
|
|
155
|
-
|
|
156
|
-
- 保存:`Ctrl/Command + S`
|
|
157
|
-
- 编辑模板 JSON:`Ctrl/Command + E`
|
|
158
|
-
- 编辑打印数据:`Ctrl/Command + Shift + E`
|
|
159
|
-
|
|
160
|
-
### 语言与国际化
|
|
78
|
+
### 2. 进阶定制:使用插槽 (Slots)
|
|
161
79
|
|
|
162
|
-
|
|
163
|
-
- 运行时可在页面右上角语言切换菜单切换(无需刷新)。
|
|
80
|
+
通过插槽可以替换头部 specific 区域的内容。
|
|
164
81
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
本库导出 `hiprint`,可直接创建与操作模板对象(`hiprint.PrintTemplate`):
|
|
168
|
-
|
|
169
|
-
```
|
|
170
|
-
import { hiprint, defaultElementTypeProvider } from 'vg-print'
|
|
171
|
-
// 全局配置
|
|
172
|
-
hiprint.setConfig({ showAdsorbLine: true, showPosition: true, showSizeBox: true })
|
|
173
|
-
// 注册插件
|
|
174
|
-
hiprint.register({ authKey: '', plugins: [] })
|
|
175
|
-
// 初始化元素提供器 (默认元素提供器)
|
|
176
|
-
hiprint.init({
|
|
177
|
-
providers: [new defaultElementTypeProvider()],
|
|
178
|
-
host: 'http://localhost:17521',
|
|
179
|
-
token: '',
|
|
180
|
-
lang: 'en'
|
|
181
|
-
})
|
|
182
|
-
// 创建模板对象
|
|
183
|
-
const tpl = new hiprint.PrintTemplate({ template: {} })
|
|
184
|
-
// 设计模板
|
|
185
|
-
tpl.design('#hiprint-printTemplate', { grid: true })
|
|
186
|
-
// 打印(需客户端已连接)
|
|
187
|
-
tpl.print({ name: '示例' })
|
|
188
|
-
// 打印2(需客户端已连接)
|
|
189
|
-
tpl.print2({ name: '示例' }, { printer: '' })
|
|
190
|
-
// 导出 PDF(内置示例参数)
|
|
191
|
-
tpl.toPdf({ name: '示例' }, '打印预览pdf', { paperWidth: 210, paperHeight: 297, scale: 2, perPage: 6 })
|
|
192
|
-
// 导出图片(内置示例参数)
|
|
193
|
-
tpl.toImage({ name: '示例' }, { isDownload: true, name: '图片', limit: 1, type: 'image/jpeg', pixelRatio: 2 })
|
|
194
|
-
```
|
|
82
|
+
#### 2.1 自定义左侧与中间区域
|
|
195
83
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```js
|
|
214
|
-
import { hiprint } from 'vg-print';
|
|
215
|
-
|
|
216
|
-
// 填入生成的 Key
|
|
217
|
-
hiprint.register({ authKey: 'eyJrIjoiZ21jNTc2MDMzNyJ9' });
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
注册后,以下所有使用方式均会自动生效(移除默认水印):
|
|
221
|
-
- 完整设计器组件 `FullDesigner`
|
|
222
|
-
- 核心类 `new hiprint.PrintTemplate(...)`
|
|
223
|
-
- 轻量运行时 helper `createTemplate(...)`
|
|
224
|
-
|
|
225
|
-
- 示例:轻量运行时使用授权
|
|
226
|
-
|
|
227
|
-
```js
|
|
228
|
-
import { hiprint, createTemplate, printBrowser } from 'vg-print'
|
|
229
|
-
|
|
230
|
-
hiprint.register({ authKey: 'eyJrIjoiZ21jNTc2MDMzNyJ9' })
|
|
231
|
-
const tpl = createTemplate(tmplJson) // 不再自动注入默认水印
|
|
232
|
-
printBrowser(tpl, data)
|
|
84
|
+
```vue
|
|
85
|
+
<template>
|
|
86
|
+
<FullDesigner :initial-template="tpl">
|
|
87
|
+
<!-- #headerLeft: 替换默认的 Logo/标题 -->
|
|
88
|
+
<template #headerLeft>
|
|
89
|
+
<div class="my-brand">
|
|
90
|
+
<i class="el-icon-s-platform"></i>
|
|
91
|
+
<strong>企业报表中心</strong>
|
|
92
|
+
</div>
|
|
93
|
+
</template>
|
|
94
|
+
|
|
95
|
+
<!-- #headerCenter: 在中间空白区域添加内容 -->
|
|
96
|
+
<template #headerCenter>
|
|
97
|
+
<span class="status-badge">运行中</span>
|
|
98
|
+
</template>
|
|
99
|
+
</FullDesigner>
|
|
100
|
+
</template>
|
|
233
101
|
```
|
|
234
102
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
```js
|
|
238
|
-
// 无授权时,库会注入默认水印;如需自定义水印样式/内容
|
|
239
|
-
const tpl = new hiprint.PrintTemplate({
|
|
240
|
-
template: {},
|
|
241
|
-
watermarkOptions: { content: 'your-brand', timestamp: true, rotate: 20 }
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
// 有授权时,运行态不会注入默认水印;若仍需水印,请显式传入
|
|
245
|
-
const tpl2 = createTemplate(tmplJson, {
|
|
246
|
-
watermarkOptions: { content: 'your-brand', timestamp: true }
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
// 设计态由面板设置项决定水印,不受 authKey 控制。
|
|
250
|
-
// 运行态的默认水印由授权控制,不建议通过传入空对象取消;如需取消,请注册有效 authKey。
|
|
251
|
-
const tpl3 = new hiprint.PrintTemplate({ template: {}, watermarkOptions: { content: 'your-brand', timestamp: true } })
|
|
252
|
-
```
|
|
103
|
+
#### 2.2 自定义右侧工具栏(完全替换)
|
|
253
104
|
|
|
254
|
-
|
|
105
|
+
使用 `#headerRight` 插槽将**完全替换**默认的工具栏按钮。适用于需要完全自定义操作按钮的场景。
|
|
255
106
|
|
|
256
|
-
```
|
|
257
|
-
|
|
107
|
+
```vue
|
|
108
|
+
<template>
|
|
109
|
+
<FullDesigner ref="designer">
|
|
110
|
+
<template #headerRight>
|
|
111
|
+
<!-- 自定义按钮组 -->
|
|
112
|
+
<el-button type="primary" size="small" @click="handlePrint">立即打印</el-button>
|
|
113
|
+
<el-button size="small" @click="handleSave">保存</el-button>
|
|
114
|
+
</template>
|
|
115
|
+
</FullDesigner>
|
|
116
|
+
</template>
|
|
258
117
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
118
|
+
<script setup>
|
|
119
|
+
import { ref } from 'vue'
|
|
120
|
+
const designer = ref(null)
|
|
262
121
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
})
|
|
122
|
+
const handlePrint = () => designer.value.printView() // 调用组件内部方法
|
|
123
|
+
const handleSave = () => designer.value.save() // 调用组件内部方法
|
|
124
|
+
</script>
|
|
267
125
|
```
|
|
268
126
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
```js
|
|
272
|
-
{ content: 'vg-print', timestamp: true }
|
|
273
|
-
}
|
|
127
|
+
#### 2.3 完全替换头部组件 (Total Header Replacement)
|
|
274
128
|
|
|
275
|
-
|
|
129
|
+
使用 `#header` 插槽完全替换内置的 `Header` 组件。这对应于您可能看到的某些示例(如 `template #header` 包裹整个 Header 组件)。
|
|
276
130
|
|
|
131
|
+
```vue
|
|
132
|
+
<template>
|
|
133
|
+
<FullDesigner ref="designer">
|
|
134
|
+
<!-- #header: 完全自定义头部结构 -->
|
|
135
|
+
<template #header>
|
|
136
|
+
<div class="my-custom-header">
|
|
137
|
+
<h1>我的专属设计器</h1>
|
|
138
|
+
<div class="actions">
|
|
139
|
+
<button @click="designer.printView()">打印</button>
|
|
140
|
+
</div>
|
|
141
|
+
</div>
|
|
142
|
+
</template>
|
|
143
|
+
</FullDesigner>
|
|
144
|
+
</template>
|
|
277
145
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
exportPdf,
|
|
288
|
-
exportImage,
|
|
289
|
-
getPrinterList,
|
|
290
|
-
setHiwebSocket,
|
|
291
|
-
connect,
|
|
292
|
-
disconnect
|
|
293
|
-
} from 'vg-print'
|
|
294
|
-
|
|
295
|
-
// 1) 创建模板实例(可复用)
|
|
296
|
-
const tpl = createTemplate(tmplJson)
|
|
297
|
-
|
|
298
|
-
// 2) 生成预览 HTML,自行渲染到容器
|
|
299
|
-
const preview = async () => {
|
|
300
|
-
visible.value = true // 弹出框容器显示
|
|
301
|
-
await nextTick() // 等容器渲染出来
|
|
302
|
-
|
|
303
|
-
const tpl = createTemplate(tmplJson)
|
|
304
|
-
const html = getHtml(tpl, printData) // 始终为字符串
|
|
305
|
-
|
|
306
|
-
const container = document.getElementById('preview_content_custom') // 显示内容的div的id
|
|
307
|
-
if (!container) {
|
|
308
|
-
console.warn('预览容器不存在')
|
|
309
|
-
return
|
|
310
|
-
}
|
|
311
|
-
container.innerHTML = html
|
|
146
|
+
<style scoped>
|
|
147
|
+
.my-custom-header {
|
|
148
|
+
height: 60px;
|
|
149
|
+
background: #333;
|
|
150
|
+
color: #fff;
|
|
151
|
+
display: flex;
|
|
152
|
+
justify-content: space-between;
|
|
153
|
+
align-items: center;
|
|
154
|
+
padding: 0 20px;
|
|
312
155
|
}
|
|
313
|
-
|
|
314
|
-
// 3) 浏览器打印(不需要客户端)
|
|
315
|
-
printBrowser(tpl, printData)
|
|
316
|
-
|
|
317
|
-
// 4) 导出 PDF(示例参数)
|
|
318
|
-
exportPdf(tpl, printData, '打印预览pdf', {
|
|
319
|
-
paperWidth: 210,
|
|
320
|
-
paperHeight: 297,
|
|
321
|
-
scale: 2,
|
|
322
|
-
perPage: 6,
|
|
323
|
-
leftOffset: -1,
|
|
324
|
-
topOffset: -1,
|
|
325
|
-
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
326
|
-
})
|
|
327
|
-
|
|
328
|
-
// 5) 导出图片(示例参数)
|
|
329
|
-
await exportImage(tpl, printData, {
|
|
330
|
-
isDownload: true,
|
|
331
|
-
name: 'A4排版',
|
|
332
|
-
limit: 1, // 导出多张时设置每张合成的页数;不传或设为 0 合成整图
|
|
333
|
-
type: 'image/jpeg',
|
|
334
|
-
pixelRatio: 2,
|
|
335
|
-
quality: 0.8,
|
|
336
|
-
toType: 'url',
|
|
337
|
-
onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
|
|
338
|
-
})
|
|
339
|
-
|
|
340
|
-
// 6) 客户端直连打印(需要本地客户端)
|
|
341
|
-
setHiwebSocket('http://127.0.0.1:17521', 'your-token')
|
|
342
|
-
connect()
|
|
343
|
-
// 可选:查看打印机列表
|
|
344
|
-
const printers = getPrinterList(tpl)
|
|
345
|
-
// 使用模板对象直接发起直连打印
|
|
346
|
-
tpl.print2(printData, { printer: printers?.[0]?.name || '' })
|
|
347
|
-
|
|
348
|
-
// 7) 断开连接
|
|
349
|
-
disconnect()
|
|
156
|
+
</style>
|
|
350
157
|
```
|
|
351
158
|
|
|
352
|
-
###
|
|
159
|
+
### 3. 综合集成示例
|
|
353
160
|
|
|
354
|
-
|
|
161
|
+
包含数据加载、连接客户端、自定义头部样式的完整代码。
|
|
355
162
|
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
})
|
|
361
|
-
```
|
|
163
|
+
```vue
|
|
164
|
+
<script setup>
|
|
165
|
+
import { ref, onMounted } from 'vue'
|
|
166
|
+
// import { FullDesigner } from 'vg-print' // 实际使用时引入
|
|
362
167
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
```js
|
|
366
|
-
// 传入纸张尺寸与排版参数,将多个模板排到一张 A4
|
|
367
|
-
exportPdf(tpl, printData, '打印预览pdf', {
|
|
368
|
-
paperWidth: 210, // mm
|
|
369
|
-
paperHeight: 297, // mm
|
|
370
|
-
scale: 2, // 缩放系数(影响清晰度与排版)
|
|
371
|
-
perPage: 6, // 每页排版的模板数量
|
|
372
|
-
leftOffset: -1, // 左对齐偏移(mm)
|
|
373
|
-
topOffset: -1, // 上对齐偏移(mm)
|
|
374
|
-
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
375
|
-
})
|
|
376
|
-
```
|
|
168
|
+
const tpl = ref({}) // 初始模板
|
|
169
|
+
const token = ref('')
|
|
377
170
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
// 不传纸张尺寸,按模板分页导出图片
|
|
384
|
-
await exportImage(tpl, printData, {
|
|
385
|
-
isDownload: true,
|
|
386
|
-
name: '图片',
|
|
387
|
-
limit: 1, // 多少页合成一张图片;0 或不传则导出整图
|
|
388
|
-
type: 'image/jpeg', // 或 'image/png'
|
|
389
|
-
pixelRatio: 2, // 像素比例(清晰度)
|
|
390
|
-
quality: 0.8, // 仅 JPEG 有效(0-1)
|
|
391
|
-
toType: 'url', // 不下载时返回类型:'url' 或 'blob'
|
|
392
|
-
onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
|
|
393
|
-
})
|
|
394
|
-
```
|
|
171
|
+
const onSave = ({ template }) => {
|
|
172
|
+
console.log('Template JSON:', template)
|
|
173
|
+
ElMessage.success('保存成功')
|
|
174
|
+
}
|
|
175
|
+
</script>
|
|
395
176
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
await exportImage(tpl, printData, {
|
|
401
|
-
paperWidth: 210, // mm
|
|
402
|
-
paperHeight: 297, // mm
|
|
403
|
-
limit: 6, // 每张合成 6 页
|
|
404
|
-
isDownload: true,
|
|
405
|
-
name: 'A4排版',
|
|
406
|
-
type: 'image/jpeg',
|
|
407
|
-
pixelRatio: 2,
|
|
408
|
-
quality: 0.8
|
|
409
|
-
})
|
|
177
|
+
<style scoped>
|
|
178
|
+
.app-container { height: 100vh; }
|
|
179
|
+
.info-bar { line-height: 40px; color: #999; font-size: 12px; }
|
|
180
|
+
</style>
|
|
410
181
|
```
|
|
411
182
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
- `paperWidth`、`paperHeight` 纸张尺寸(mm);传入后按该纸张排版,不传则使用模板自身尺寸与分页
|
|
415
|
-
- `scale` 渲染缩放(PDF);提升清晰度或控制排版密度
|
|
416
|
-
- `perPage` 每页模板数量(PDF);用于小模板排版到大纸张
|
|
417
|
-
- `leftOffset`、`topOffset` 对齐偏移(mm);微调整体排版位置(PDF)
|
|
418
|
-
- `limit` 图片合并页数;1 表示每张 1 页,6 表示每张 6 页;0/不传导出整图
|
|
419
|
-
- `type` 图片格式;`'image/jpeg'` 或 `'image/png'`
|
|
420
|
-
- `pixelRatio` 像素比例(图片);越大越清晰但文件更大
|
|
421
|
-
- `quality` 图片质量(0-1,仅 JPEG 有效)
|
|
422
|
-
- `toType` 返回类型(不下载时);`'url'` 或 `'blob'`
|
|
423
|
-
- `isDownload` 是否直接下载图片文件
|
|
424
|
-
- `name` 文件名或下载名
|
|
425
|
-
- `onProgress(cur, total)` 导出进度回调;以 `cur/total` 计算百分比
|
|
183
|
+
## Preview 预览组件
|
|
426
184
|
|
|
427
|
-
|
|
185
|
+
`Preview` 组件用于展示打印预览,支持多种导出格式(PDF、图片)及打印机选择。
|
|
428
186
|
|
|
429
|
-
|
|
430
|
-
- 如需自定义元素提供器,在调用前执行 `hiprint.init({ providers: [...] })`;默认已初始化内置提供器
|
|
187
|
+
### 组件参数 (Props)
|
|
431
188
|
|
|
432
|
-
|
|
189
|
+
| 参数键名 | 数据类型 | 默认值 | 说明 | 备注 |
|
|
190
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
191
|
+
| printTemplate | Object/JSON | undefined | 模板对象 | |
|
|
192
|
+
| printData | Object/JSON | undefined | 打印数据 | |
|
|
193
|
+
| printerList | Array | [] | 打印机列表 | 显示时会自动刷新 |
|
|
194
|
+
| selectedPrinter | String | '' | 默认选中打印机名称 | |
|
|
195
|
+
| showPdf | Boolean | true | 是否显示导出 PDF 按钮 | |
|
|
196
|
+
| showImg | Boolean | true | 是否显示导出图片按钮 | |
|
|
197
|
+
| showPrint2 | Boolean | true | 是否显示直接打印按钮 | |
|
|
198
|
+
| modalShow | Boolean | false | 是否显示弹窗 | 可使用 v-model:modalShow |
|
|
199
|
+
| width | String/Number | '80%' | 弹窗宽度 | |
|
|
433
200
|
|
|
434
|
-
|
|
435
|
-
- 脚本或批量任务:结合 `exportPdf`/`exportImage` 实现批量导出
|
|
201
|
+
### 方法 (Exposed Methods)
|
|
436
202
|
|
|
203
|
+
| 方法名 | 参数 | 说明 |
|
|
204
|
+
| :--- | :--- | :--- |
|
|
205
|
+
| init | (template, data) | 初始化模板和数据 |
|
|
206
|
+
| show | (template, data, width) | 显示预览弹窗(会自动调用 render) |
|
|
207
|
+
| hideModal | - | 关闭弹窗 |
|
|
208
|
+
| getTarget | - | 获取当前模板实例 |
|
|
437
209
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
- 建议在 `index.html` 注入 `print-lock.css`,保证打印样式稳定
|
|
441
|
-
- 直接打印需配套客户端(Electron 或服务端),跨域部署需启用 HTTPS
|
|
442
|
-
- 若使用自定义 `host/token`,确保客户端配置一致且网络可达
|
|
443
|
-
- SSR 兼容:插件安装仅在浏览器环境执行(`typeof window !== 'undefined'`),在 SSR 框架中引入不会因 `window` 未定义报错。
|
|
444
|
-
|
|
445
|
-
## 构建
|
|
446
|
-
|
|
447
|
-
开发与构建:
|
|
448
|
-
|
|
449
|
-
```
|
|
450
|
-
npm run dev # 开发
|
|
451
|
-
npm run build # 应用构建
|
|
452
|
-
npm run build:lib# 组件库构建
|
|
453
|
-
```
|
|
454
|
-
|
|
455
|
-
## 源码位置
|
|
456
|
-
|
|
457
|
-
- 入口与导出:`packages/index.ts`
|
|
458
|
-
- 完整页面组件:`src/views/index.vue`
|
|
459
|
-
## 完整示例
|
|
210
|
+
### 使用示例
|
|
460
211
|
|
|
461
212
|
```vue
|
|
462
213
|
<template>
|
|
463
|
-
<
|
|
464
|
-
|
|
465
|
-
:
|
|
466
|
-
default-lang="cn"
|
|
467
|
-
:hi-host="host"
|
|
468
|
-
:hi-token="token"
|
|
469
|
-
:hi-auto-connect="true"
|
|
470
|
-
@save="onSave"
|
|
471
|
-
/>
|
|
472
|
-
|
|
473
|
-
<div style="margin-top: 12px;">
|
|
474
|
-
<el-input v-model="host" placeholder="客户端地址,如 http://127.0.0.1:17521" style="width: 360px; margin-right: 8px;" />
|
|
475
|
-
<el-input v-model="token" placeholder="鉴权令牌" style="width: 200px; margin-right: 8px;" />
|
|
476
|
-
<el-button type="primary" @click="applyHostToken">应用连接参数</el-button>
|
|
477
|
-
<el-button style="margin-left:8px;" @click="preview">预览</el-button>
|
|
478
|
-
<el-button style="margin-left:8px;" @click="printBrowser">浏览器打印</el-button>
|
|
214
|
+
<div>
|
|
215
|
+
<el-button @click="testPreview">显示预览组件</el-button>
|
|
216
|
+
<Preview ref="previewRef" v-model:modalShow="modalShow" />
|
|
479
217
|
</div>
|
|
480
218
|
</template>
|
|
481
219
|
|
|
482
|
-
<script setup>
|
|
483
|
-
import { ref } from
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
]
|
|
503
|
-
}
|
|
504
|
-
]
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
// 数据(对象或数组均可)
|
|
508
|
-
const rows = [{ title: '病理图文报告', name: '东东' }]
|
|
509
|
-
|
|
510
|
-
// 连接参数
|
|
511
|
-
const host = ref('http://127.0.0.1:17521')
|
|
512
|
-
const token = ref('')
|
|
513
|
-
|
|
514
|
-
// 组件引用
|
|
515
|
-
const designer = ref(null)
|
|
516
|
-
|
|
517
|
-
const applyHostToken = () => {
|
|
518
|
-
// 设置客户端地址与令牌,并尝试重连
|
|
519
|
-
designer.value?.setHiwebSocket(host.value, token.value)
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// 预览与浏览器打印
|
|
523
|
-
const preview = () => designer.value?.preView()
|
|
524
|
-
const printBrowser = () => designer.value?.printView()
|
|
525
|
-
|
|
526
|
-
// 保存事件:拿到模板与数据入库
|
|
527
|
-
const onSave = ({ template, data, templateId }) => {
|
|
528
|
-
// 持久化示例
|
|
529
|
-
// await api.save({ template, data, templateId })
|
|
530
|
-
console.log('save payload:', { template, data, templateId })
|
|
531
|
-
}
|
|
220
|
+
<script setup lang="ts">
|
|
221
|
+
import { ref } from "vue";
|
|
222
|
+
import { Preview } from "vg-print"; // 假设这是您的包名
|
|
223
|
+
|
|
224
|
+
// 假设 template 是已经创建好的 hiprint 模板实例
|
|
225
|
+
// import { hiprint } from 'vg-print';
|
|
226
|
+
// const template = new hiprint.PrintTemplate({...});
|
|
227
|
+
|
|
228
|
+
const previewRef = ref(null);
|
|
229
|
+
const modalShow = ref(false);
|
|
230
|
+
|
|
231
|
+
const testPreview = () => {
|
|
232
|
+
// 方式1: 直接调用 show 并传入参数
|
|
233
|
+
// 假设 printData 是您的打印数据
|
|
234
|
+
previewRef.value.show(template, printData);
|
|
235
|
+
|
|
236
|
+
// 方式2: 配合 Props 使用
|
|
237
|
+
// modalShow.value = true;
|
|
238
|
+
};
|
|
532
239
|
</script>
|
|
533
240
|
```
|