vg-print 1.0.348 → 1.0.349

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 CHANGED
@@ -1,240 +1,761 @@
1
1
  # vg-print
2
2
 
3
- vg-print Vue3.x 支持拖拽(分页(不分页)、表头表脚、样式设置、复制粘贴、缩放、撤销重做)生成打印模板、导出json模板数据、静默打印/获取MAC地址(借助客户端)
3
+ 一个开箱即用的打印设计器组件库(Vue 3)。现在聚焦于单一组件 `FullDesigner`,同时暴露底层 `hiprint` 能力与客户端直连(`hiwebSocket`)配置,便于快速集成模板设计、预览、浏览器打印、导出 PDF/图片、直接打印等功能。
4
+ ![功能截图](https://gitee.com/gao111/electron-vg-print/raw/master/ScreenShot_2025-12-25_163332_128.png)
5
+ 您可以直接安装引入体验功能.
6
+ 没有域名限制,无网可用. 提供一对一技术支持.
7
+ npm搜索vg-print.
8
+ 联系我加 QQ: 984239686 或使用首页有微信号加入
9
+
10
+ # 静默打印客户端工具安装包
11
+
12
+ ![静默打印客户端工具](https://gitee.com/gao111/electron-vg-print/raw/master/electron-hiprint.png)
13
+
14
+ 更多信息与下载:<https://gitee.com/gao111/electron-vg-print>
15
+
16
+ ## 特性
17
+
18
+ - 全功能页面组件 `FullDesigner`,直接使用,无需拼装子组件
19
+ - 支持预览、浏览器打印、导出 PDF/图片、直接打印(需客户端)
20
+ - 通过属性配置 `hiwebSocket` 的 `host`、`token` 与自动连接
21
+ - 暴露实例方法:预览、打印、导出、连接控制等,外部可直接调用
22
+ - 集成并导出 `hiprint`,可进行模板对象级的高级定制
4
23
 
5
24
  ## 安装
6
25
 
7
- ```bash
8
- npm install vg-print --save
26
+ ```
27
+ npm i vg-print
9
28
  ```
10
29
 
11
- ## 引入
30
+ ## Header 顶部工具栏组件
12
31
 
13
- ```javascript
14
- import { createApp } from 'vue'
15
- import App from './App.vue'
16
- import vgPrint from 'vg-print'
17
- import 'vg-print/dist/style.css'
32
+ - 用于页面一级头部区域,内置常用操作按钮与打印机设置;支持通过 `props` 与 `slots` 定制。
33
+ - 默认嵌入在 `FullDesigner` 中;也可单独使用。
18
34
 
19
- const app = createApp(App)
20
- app.use(vgPrint)
21
- app.mount('#app')
22
- ```
35
+ ### 属性(Props)
36
+
37
+ - `logoHtml: string` 左侧 Logo 区自定义 HTML 片段
38
+ - `title: string` 左侧标题文本
39
+ - `printCount: number` 打印份数,默认 `1`(支持 `v-model:printCount`)
40
+ - `printerName: string` 当前选择的打印机名称(支持 `v-model:printerName`)
41
+ - `printerList: Array<{ name: string; label: string }>` 打印机列表
42
+ - `waitShowPrinter: boolean` 显示“浏览器打印”按钮的加载状态
43
+ - `languages: Array<{ label: string; value: string }>` 语言切换下拉选项
44
+
45
+ ### 事件(Emits)
23
46
 
24
- ## 组件列表
47
+ - `preview` 打开预览
48
+ - `print-view` 浏览器打印
49
+ - `to-pdf` 导出 PDF
50
+ - `to-image` 导出图片
51
+ - `print` 直接打印(需客户端)
52
+ - `clear-paper` 清空(带二次确认)
53
+ - `save` 保存(拆分按钮的主键)
54
+ - `edit-template` 打开“编辑模板数据”对话框
55
+ - `edit-data` 打开“编辑打印数据”对话框
56
+ - `lang-change` 语言切换(值为语言码:`cn`、`en`、`de`、`es`、`fr`、`it`、`ja`、`ru`、`cn_tw`)
57
+ - `feedback` 反馈/帮助
25
58
 
26
- - `FullDesigner`: 完整设计器组件
27
- - `Header`: 头部组件
28
- - `Preview`: 预览组件
59
+ ### 插槽(Slots)
29
60
 
30
- ## FullDesigner 使用
61
+ - `headerLeft` 左侧区域(默认:logo + 标题)
62
+ - `headerCenter` 中间自定义区域(默认空)
63
+ - `headerRight` 右侧操作区(默认:打印机与一组按钮)
64
+
65
+ ### 使用示例:与 FullDesigner 配合
31
66
 
32
67
  ```vue
33
68
  <template>
34
- <FullDesigner />
69
+ <FullDesigner
70
+ ref="designer"
71
+ :initial-template="tpl"
72
+ :initial-print-data="rows"
73
+ default-lang="cn"
74
+ :hi-host="host"
75
+ :hi-token="token"
76
+ :hi-auto-connect="true"
77
+ @save="onSave"
78
+ >
79
+ <!-- 顶部区域(Header 默认内置;也可通过 slot 定制) -->
80
+ <template #headerLeft>
81
+ <div style="display:flex;align-items:center;color:#fff;">
82
+ <img src="/logo.svg" style="height:24px;margin-right:8px;" />
83
+ <span>自定义标题</span>
84
+ </div>
85
+ </template>
86
+ <template #headerCenter>
87
+ <el-tag type="success">中间自定义区域</el-tag>
88
+ </template>
89
+ <!-- 保留默认按钮时无需提供 headerRight;若完全替换按钮则提供该插槽 -->
90
+ <template #headerRight>
91
+ <el-button size="small" @click="designer.value.preView()">预览</el-button>
92
+ <el-button size="small" @click="designer.value.printView()">浏览器打印</el-button>
93
+ <el-button type="primary" size="small" @click="designer.value.toPdf()">导出 PDF</el-button>
94
+ <el-button size="small" @click="designer.value.toImage()">导出图片</el-button>
95
+ <el-button size="small" @click="designer.value.print()">直接打印</el-button>
96
+ <el-button type="warning" size="small" @click="onClear()">清空</el-button>
97
+ <el-button type="success" size="small" @click="onSaveDirect()">保存</el-button>
98
+ </template>
99
+ </FullDesigner>
35
100
  </template>
101
+
102
+ <script setup>
103
+ import { ref } from 'vue'
104
+ const designer = ref(null)
105
+ const host = ref('http://127.0.0.1:17521')
106
+ const token = ref('')
107
+ const tpl = { panels: [ /* ... */ ] }
108
+ const rows = [{ title: '病理图文报告', name: '东东' }]
109
+ const onSave = ({ template, data }) => { /* 保存到后端 */ }
110
+ const onClear = () => { /* 清空数据/元素 */ }
111
+ const onSaveDirect = () => designer.value?.save()
112
+ </script>
36
113
  ```
37
114
 
38
- ## 注意事项
115
+ ### 使用示例:单独使用 Header
39
116
 
40
- - 直接打印需配套客户端(Electron 或服务端),跨域部署需启用 HTTPS
41
- - 若使用自定义 `host/token`,确保客户端配置一致且网络可达
42
- - SSR 兼容:插件安装仅在浏览器环境执行(`typeof window !== 'undefined'`),在 SSR 框架中引入不会因 `window` 未定义报错。
117
+ ```vue
118
+ <template>
119
+ <Header
120
+ v-model:printCount="count"
121
+ v-model:printerName="printer"
122
+ :printerList="printers"
123
+ :waitShowPrinter="loading"
124
+ :languages="langs"
125
+ title="打印设计器"
126
+ @preview="onPreview"
127
+ @print-view="onPrintView"
128
+ @to-pdf="onToPdf"
129
+ @to-image="onToImage"
130
+ @print="onPrint"
131
+ @clear-paper="onClear"
132
+ @save="onSave"
133
+ @lang-change="onLangChange"
134
+ @feedback="onFeedback"
135
+ />
136
+ </template>
137
+ ```
43
138
 
44
- ## 构建
139
+ ### 响应式策略(已内置)
45
140
 
46
- 开发与构建:
141
+ - 窗口缩小时自动隐藏中间区域与次要按钮,保留右侧“保存”和“反馈”等关键操作;左侧 logo/标题不受影响。
142
+ - 断点规则:
143
+ - `≤1600px` 隐藏中间区域
144
+ - `≤1400px` 隐藏打印份数、打印机选择、预览、浏览器打印、导出 PDF、导出图片
145
+ - `≤1200px` 隐藏直接打印、语言切换
146
+ - `≤992px` 隐藏清空
147
+ - `≤768px` 隐藏标题文本(保留 Logo)
47
148
 
48
- ```
49
- npm run dev # 开发
50
- npm run build # 应用构建
51
- npm run build:lib# 组件库构建
52
- ```
149
+ > 提示:如需完全自定义按钮,请提供 `headerRight` 插槽实现自己的按钮布局。
150
+
151
+ ---
152
+
153
+ ## Preview 预览组件
154
+
155
+ - 独立的预览弹窗组件,支持渲染模板 HTML、选择打印机、导出 PDF/图片、直接打印等。
156
+ - 可单独使用或与 `FullDesigner` 搭配。
157
+
158
+ ### 属性(Props)
53
159
 
54
- ## 源码位置
160
+ - `printTemplate: object` 模板对象(`hiprint.PrintTemplate` 或 `createTemplate` 返回值)
161
+ - `printData: object | array` 打印数据
162
+ - `printerList: Array<{ name: string; label: string }>` 打印机列表
163
+ - `selectedPrinter: string` 选中的打印机名称
164
+ - `showPdf: boolean` 是否显示“导出 PDF”按钮,默认 `true`
165
+ - `showImg: boolean` 是否显示“导出图片”按钮,默认 `true`
166
+ - `showPrint2: boolean` 是否显示“直接打印”按钮,默认 `true`
167
+ - `modalShow: boolean` 弹窗显隐(支持 `v-model:modalShow`)
168
+ - `width: string | number` 弹窗宽度(默认 `'80%'`)
55
169
 
56
- - 入口与导出:`packages/index.ts`
57
- - 完整页面组件:`src/views/index.vue`
170
+ ### 事件(Emits)
58
171
 
59
- ## 完整示例与头部定制
172
+ - `update:modalShow` 双向绑定弹窗显隐
173
+ - `update:selectedPrinter` 选中打印机变更
174
+ - `close` 关闭弹窗
60
175
 
61
- `Header` 组件已完全内置于 `FullDesigner` 中。您**不需要**单独引入或编写 `<Header>` 标签,只需通过 `FullDesigner` 的属性和插槽即可实现对头部的完全控制。
176
+ ### 暴露方法(defineExpose)
62
177
 
63
- ### 1. 基础定制:标题与 Logo (Props)
178
+ - `show(template, data, width?)` 显示并渲染预览内容;可传入模板/数据与弹窗宽度
179
+ - `hideModal()` 关闭弹窗
180
+ - `init()` 初始化(内部使用)
181
+ - `getTarget()` 获取预览容器 DOM
64
182
 
65
- 最简单的用法,通过属性直接配置。
183
+ ### 使用示例(单独使用)
66
184
 
67
185
  ```vue
68
186
  <template>
69
- <FullDesigner
70
- :initial-template="tpl"
71
- title="我的打印设计"
72
- logo-html="<img src='https://example.com/logo.png' height='28px' />"
73
- @save="onSave"
187
+ <el-button @click="openPreview">显示预览</el-button>
188
+ <Preview
189
+ ref="previewRef"
190
+ v-model:modalShow="visible"
191
+ :printerList="printers"
192
+ :selectedPrinter="printer"
193
+ @update:selectedPrinter="printer = $event"
74
194
  />
75
195
  </template>
76
- ```
77
196
 
78
- ### 2. 进阶定制:使用插槽 (Slots)
197
+ <script setup>
198
+ import { ref } from 'vue'
199
+ import { Preview, createTemplate } from 'vg-print'
200
+ import 'vg-print/style.css'
201
+
202
+ const previewRef = ref(null)
203
+ const visible = ref(false)
204
+ const printers = ref([{ name: 'HP_001', label: 'HP 打印机' }])
205
+ const printer = ref('')
206
+
207
+ const tmplJson = { panels: [ /* ... */ ] }
208
+ const data = { title: '病理图文报告', name: '东东' }
79
209
 
80
- 通过插槽可以替换头部 specific 区域的内容。
210
+ const openPreview = () => {
211
+ const tpl = createTemplate(tmplJson) // 也可传入 hiprint.PrintTemplate
212
+ previewRef.value.show(tpl, data, '80%')
213
+ }
214
+ </script>
215
+ ```
81
216
 
82
- #### 2.1 自定义左侧与中间区域
217
+ ### 使用示例(与 FullDesigner 搭配)
83
218
 
84
219
  ```vue
85
220
  <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>
221
+ <FullDesigner ref="designer" default-lang="cn" />
222
+ <Preview ref="previewRef" v-model:modalShow="visible" />
223
+ <el-button @click="designer.value.preView()">直接使用 FullDesigner 的预览</el-button>
224
+ <el-button @click="showCustomPreview">用 Preview 展示当前模板</el-button>
100
225
  </template>
226
+
227
+ <script setup>
228
+ import { ref } from 'vue'
229
+ const designer = ref(null)
230
+ const previewRef = ref(null)
231
+ const visible = ref(false)
232
+
233
+ const showCustomPreview = () => {
234
+ const tpl = designer.value.getTemplate() // 假设暴露了获取模板的方法
235
+ const data = designer.value.getPrintData() // 假设暴露了获取数据的方法
236
+ previewRef.value.show(tpl, data, '80%')
237
+ }
238
+ </script>
101
239
  ```
102
240
 
103
- #### 2.2 自定义右侧工具栏(完全替换)
241
+ ### 注意
104
242
 
105
- 使用 `#headerRight` 插槽将**完全替换**默认的工具栏按钮。适用于需要完全自定义操作按钮的场景。
243
+ - 使用组件库时请引入 `vg-print/style.css`,其中包含自定义图标字体与预览/设计样式。
244
+ - `Preview` 内部会根据 `printTemplate` 与 `printData` 自动渲染内容;也支持通过 `show(...)` 主动传入。
245
+
246
+
247
+ 样式引入(确保设计与打印样式生效):
248
+
249
+ ```
250
+ // 全局样式(组件库样式)
251
+ import 'vg-print/style.css'
252
+
253
+ // 打印锁样式(推荐;保证打印时样式稳定)
254
+ // 在 `index.html` 中引入:
255
+ <link rel="stylesheet" type="text/css" media="print" href="/css/print-lock.css" />
256
+ ```
257
+
258
+ 环境要求:Vue 3、Node >= 18.18。无需单独安装 `vue-plugin-hiprint`,本库已内置并导出 `hiprint` 能力。
259
+
260
+ ## 快速开始
261
+
262
+ ```js
263
+ // main.ts
264
+ import { createApp } from 'vue'
265
+ import App from './App.vue'
266
+ import vgPrint from 'vg-print'
267
+ import 'vg-print/style.css'
268
+
269
+ createApp(App).use(vgPrint).mount('#app')
270
+ ```
106
271
 
107
272
  ```vue
108
273
  <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>
274
+ <FullDesigner
275
+ ref="designer"
276
+ :hi-host="'http://127.0.0.1:17521'"
277
+ :hi-token="'your-token'"
278
+ :hi-auto-connect="true"
279
+ />
280
+
281
+ <el-button @click="preview">预览</el-button>
282
+ <el-button @click="printBrowser">浏览器打印</el-button>
283
+ <el-button @click="exportPdf">导出 PDF</el-button>
284
+ <el-button @click="exportImage">导出图片</el-button>
285
+ <el-button @click="directPrint">直接打印(需客户端)</el-button>
116
286
  </template>
117
287
 
118
288
  <script setup>
119
289
  import { ref } from 'vue'
120
290
  const designer = ref(null)
121
291
 
122
- const handlePrint = () => designer.value.printView() // 调用组件内部方法
123
- const handleSave = () => designer.value.save() // 调用组件内部方法
292
+ // 预览
293
+ const preview = () => designer.value.preView()
294
+ // 浏览器打印
295
+ const printBrowser = () => designer.value.printView()
296
+ // 直接打印(需客户端已连接)
297
+ const exportPdf = () => designer.value.toPdf()
298
+ // 导出图片
299
+ const exportImage = () => designer.value.toImage()
300
+ // 直接打印(需客户端已连接)
301
+ const directPrint = () => designer.value.print()
302
+
303
+ // 连接控制与参数设置
304
+ const setHostToken = () => designer.value.setHiwebSocket('http://127.0.0.1:17521', 'token')
305
+ // 主动连接客户端
306
+ const connect = () => designer.value.connect()
307
+ // 断开客户端
308
+ const disconnect = () => designer.value.disconnect()
309
+ // 关闭自动连接策略
310
+ const disableAutoConnect = () => designer.value.disAutoConnect()
124
311
  </script>
125
312
  ```
126
313
 
127
- #### 2.3 完全替换头部组件 (Total Header Replacement)
314
+ ## API(FullDesigner)
315
+
316
+ ### 属性(Props,含描述)
317
+
318
+ - `hi-host: string` 客户端服务地址;示例 `http://127.0.0.1:17521`,用于直连打印
319
+ - `hi-token: string` 客户端鉴权令牌;与客户端配置一致时方可直连打印
320
+ - `hi-auto-connect: boolean` 是否自动连接客户端;默认 `true`,为 `false` 时需手动调用 `connect()`
321
+ - `initial-template: object` 初始化模板 JSON;为空时使用内置示例模板
322
+ - `initial-print-data: object | array` 初始化打印数据;支持对象或数组,运行时变更会即时生效
323
+ - `default-lang: string` 默认语言;支持 `cn`、`en`、`de`、`es`、`fr`、`it`、`ja`、`ru`、`cn_tw`;默认 `cn`
324
+
325
+ 以上属性支持运行时动态修改;组件内部已监听并应用。
128
326
 
129
- 使用 `#header` 插槽完全替换内置的 `Header` 组件。这对应于您可能看到的某些示例(如 `template #header` 包裹整个 Header 组件)。
327
+ ### 方法(通过组件 ref 调用,含描述)
328
+
329
+ - `preView()` 打开预览弹窗;将当前模板与数据渲染为预览 HTML
330
+ - `printView()` 浏览器打印;不依赖客户端,调起系统打印对话框
331
+ - `print()` 直接打印;需客户端已连接,按当前选定打印机输出
332
+ - `toPdf()` 导出 PDF;使用内置 A4 排版示例参数导出
333
+ - `toImage()` 导出图片;使用内置 A4 排版示例参数导出
334
+ - `setHiwebSocket(host: string, token?: string, cb?: Function)` 设置客户端地址与令牌并尝试重连;`cb(status, msg)` 为连接回调
335
+ - `connect(cb?: Function)` 主动连接客户端;`cb(status, msg)` 为连接回调
336
+ - `disconnect()` 断开客户端连接
337
+ - `disAutoConnect()` 关闭自动连接策略;阻止自动重连
338
+
339
+ ### 事件(Emits,含描述)
340
+
341
+ - `save` 保存模板与数据;回调参数结构:
342
+
343
+ ```js
344
+ {
345
+ template, // 模板 JSON
346
+ data, // 当前打印数据(对象或数组)
347
+ templateId // 模板标识(若存在)
348
+ }
349
+ ```
350
+
351
+ 示例:
130
352
 
131
353
  ```vue
132
354
  <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>
355
+ <FullDesigner
356
+ :initial-template="tpl"
357
+ :initial-print-data="rows"
358
+ default-lang="cn"
359
+ @save="onSave"
360
+ />
144
361
  </template>
145
362
 
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;
363
+ <script setup>
364
+ const onSave = ({ template, data, templateId }) => {
365
+ // 调用接口持久化到数据库
366
+ // await api.saveTemplate({ template, data, templateId })
155
367
  }
156
- </style>
368
+ </script>
157
369
  ```
370
+ ### 语言与国际化
158
371
 
159
- ### 3. 综合集成示例
372
+ - 通过 `default-lang` 设置初始语言(`cn` 默认为中文);内部会同步 `vue-i18n` 与 `hiprint` 的语言。
373
+ - 运行时可在页面右上角语言切换菜单切换(无需刷新)。
160
374
 
161
- 包含数据加载、连接客户端、自定义头部样式的完整代码。
375
+ ## 高级用法(hiprint)
162
376
 
163
- ```vue
164
- <script setup>
165
- import { ref, onMounted } from 'vue'
166
- // import { FullDesigner } from 'vg-print' // 实际使用时引入
377
+ 本库导出 `hiprint`,可直接创建与操作模板对象(`hiprint.PrintTemplate`):
167
378
 
168
- const tpl = ref({}) // 初始模板
169
- const token = ref('')
379
+ ```
380
+ import { hiprint, defaultElementTypeProvider } from 'vg-print'
381
+ // 全局配置
382
+ hiprint.setConfig({ showAdsorbLine: true, showPosition: true, showSizeBox: true })
383
+ // 注册插件
384
+ hiprint.register({ authKey: '', plugins: [] })
385
+ // 初始化元素提供器 (默认元素提供器)
386
+ hiprint.init({
387
+ providers: [new defaultElementTypeProvider()],
388
+ host: 'http://localhost:17521',
389
+ token: '',
390
+ lang: 'en'
391
+ })
392
+ // 创建模板对象
393
+ const tpl = new hiprint.PrintTemplate({ template: {} })
394
+ // 设计模板
395
+ tpl.design('#hiprint-printTemplate', { grid: true })
396
+ // 浏览器打印
397
+ tpl.print({ name: '示例' }, {}, {
398
+ callback: () => {
399
+ this.waitShowPrinter = false
400
+ },
401
+ })
402
+
403
+ // 直接打印(需客户端已连接)
404
+ tpl.print2({ name: '示例' }, {
405
+ printer: '', // 打印机 名称
406
+ title: '打印预览pdf'
407
+ });
408
+
409
+
410
+ // 导出 PDF(内置示例参数)
411
+ // 模式1: 小模版需要在一页中排列的话,增加增加页面宽高参数(想把多订单放在一页中排列)----小模板拼到大纸张(例如 A4)
412
+ tpl.toPdf([{ name: '示例1' },{ name: '示例2' },{ name: '示例3' }], '打印预览pdf', {
413
+ paperWidth: 210,
414
+ paperHeight: 297,
415
+ scale: 2,
416
+ perPage: 6, // 每页模板数
417
+ leftOffset:-1, // 对齐偏移
418
+ topOffset:-1, // 对齐偏移
419
+ onProgress: (cur, total) => {
420
+ console.log('toPdf 进度', Math.floor((cur/total)*100))
421
+ }
422
+ })
423
+ // 模式2:普通模板,导出预览全部页:
424
+ tpl.toPdf(this.printDataVar, '打印预览pdf',{
425
+ onProgress: (cur, total) => {
426
+ console.log('toPdf 进度', Math.floor((cur/total)*100))
427
+ }
428
+ });
429
+
430
+ // 导出图片(内置示例参数)
431
+ // 导出为多张图片,每张合成 6 页,自动下载 JPEG
432
+ tpl.toImage({ name: '示例' }, { isDownload: true, name: '图片名称', limit: 1, type: 'image/jpeg', pixelRatio: 2, quality: 0.8, toType: 'url', onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100)) })
433
+ //按 A4 纸张排版导出(把小模板排到 A4):
434
+ tpl.toImage({ name: '示例' }, { paperWidth: 210, paperHeight: 297, limit: 6, isDownload: true, name: 'A4排版', type: 'image/jpeg', pixelRatio: 2 })
435
+
436
+ ```
437
+
438
+ 水印与授权行为说明:设计态与运行态分离。设计态不自动显示默认水印(由面板设置项决定);运行态按授权和面板内容合并控制。
170
439
 
171
- const onSave = ({ template }) => {
172
- console.log('Template JSON:', template)
173
- ElMessage.success('保存成功')
440
+ ### 授权密钥(authKey)与默认水印
441
+
442
+ - 行为说明:
443
+ - 设计态(FullDesigner 设计界面):不自动注入默认水印;是否显示水印由面板中的“水印功能”设置项决定,与 `authKey` 无关。
444
+ - 运行态(预览、浏览器打印、导出 PDF/图片、直连打印):
445
+ - 无授权(未注册 `authKey`):
446
+ - 面板未填写内容 → 显示默认水印 `{ content: 'vg-print', timestamp: true }`
447
+ - 面板填写了内容 → 显示面板内容 + "试用版" 后缀
448
+ - 有授权(已注册有效 `authKey`):
449
+ - 面板未填写内容 → 不显示水印(默认水印被移除)
450
+ - 面板填写了内容 → 显示面板内容(无后缀)
451
+
452
+ - 注册授权密钥:
453
+ 在项目入口(如 `main.js` 或 `App.vue`)尽早注册:
454
+
455
+ ```js
456
+ import { hiprint } from 'vg-print';
457
+
458
+ // 填入生成的 Key
459
+ hiprint.register({ authKey: 'eyJrIjoiZ21jNTc2MDMzNyJ9' });
460
+ ```
461
+
462
+ 注册后,以下所有使用方式均会自动生效(移除默认水印):
463
+ - 完整设计器组件 `FullDesigner`
464
+ - 核心类 `new hiprint.PrintTemplate(...)`
465
+ - 轻量运行时 helper `createTemplate(...)`
466
+
467
+ - 示例:轻量运行时使用授权
468
+
469
+ ```js
470
+ import { hiprint, createTemplate, printBrowser } from 'vg-print'
471
+
472
+ hiprint.register({ authKey: 'eyJrIjoiZ21jNTc2MDMzNyJ9' })
473
+ const tpl = createTemplate(tmplJson) // 不再自动注入默认水印
474
+ printBrowser(tpl, data)
475
+ ```
476
+
477
+ - 自定义或显式控制水印:
478
+
479
+ ```js
480
+ // 无授权时,库会注入默认水印;如需自定义水印样式/内容
481
+ const tpl = new hiprint.PrintTemplate({
482
+ template: {},
483
+ watermarkOptions: { content: 'your-brand', timestamp: true, rotate: 20 }
484
+ })
485
+
486
+ // 有授权时,运行态不会注入默认水印;若仍需水印,请显式传入
487
+ const tpl2 = createTemplate(tmplJson, {
488
+ watermarkOptions: { content: 'your-brand', timestamp: true }
489
+ })
490
+
491
+ // 设计态由面板设置项决定水印,不受 authKey 控制。
492
+ // 运行态的默认水印由授权控制,不建议通过传入空对象取消;如需取消,请注册有效 authKey。
493
+ const tpl3 = new hiprint.PrintTemplate({ template: {}, watermarkOptions: { content: 'your-brand', timestamp: true } })
494
+ ```
495
+
496
+ - 插件注册(可选):
497
+
498
+ ```js
499
+ import { hiprint } from 'vg-print'
500
+
501
+ // 两种形式的插件均支持
502
+ function pluginFn(h) { /* 扩展逻辑 */ }
503
+ const pluginObj = { install(h) { /* 扩展逻辑 */ } }
504
+
505
+ hiprint.register({
506
+ authKey: 'YOUR_AUTH_KEY',
507
+ plugins: [pluginFn, pluginObj]
508
+ })
509
+ ```
510
+
511
+ 默认水印参数参考:
512
+
513
+ ```js
514
+ { content: 'vg-print', timestamp: true }
174
515
  }
175
- </script>
176
516
 
177
- <style scoped>
178
- .app-container { height: 100vh; }
179
- .info-bar { line-height: 40px; color: #999; font-size: 12px; }
180
- </style>
517
+
518
+
519
+
520
+ ## 轻量运行时 API(无需设计器 UI)
521
+
522
+ 当你只想“拿模板 + 数据”直接做预览、浏览器打印、导出 PDF/图片或进行客户端直连打印,而不引入页面设计器时,使用以下 API:
523
+
524
+ ```js
525
+ import {
526
+ createTemplate,
527
+ getHtml,
528
+ printBrowser,
529
+ exportPdf,
530
+ exportImage,
531
+ getPrinterList,
532
+ setHiwebSocket,
533
+ connect,
534
+ disconnect
535
+ } from 'vg-print'
536
+
537
+ // 1) 创建模板实例(可复用)
538
+ const tpl = createTemplate(tmplJson)
539
+
540
+ // 2) 生成预览 HTML,自行渲染到容器
541
+ const preview = async () => {
542
+ visible.value = true // 弹出框容器显示
543
+ await nextTick() // 等容器渲染出来
544
+
545
+ const tpl = createTemplate(tmplJson)
546
+ const html = getHtml(tpl, printData) // 始终为字符串
547
+
548
+ const container = document.getElementById('preview_content_custom') // 显示内容的div的id
549
+ if (!container) {
550
+ console.warn('预览容器不存在')
551
+ return
552
+ }
553
+ container.innerHTML = html
554
+ }
555
+
556
+ // 3) 浏览器打印(不需要客户端)
557
+ printBrowser(tpl, printData)
558
+
559
+ // 4) 导出 PDF(示例参数)
560
+ exportPdf(tpl, printData, '打印预览pdf', {
561
+ paperWidth: 210,
562
+ paperHeight: 297,
563
+ scale: 2,
564
+ perPage: 6,
565
+ leftOffset: -1,
566
+ topOffset: -1,
567
+ onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
568
+ })
569
+
570
+ // 5) 导出图片(示例参数)
571
+ await exportImage(tpl, printData, {
572
+ isDownload: true,
573
+ name: 'A4排版',
574
+ limit: 1, // 导出多张时设置每张合成的页数;不传或设为 0 合成整图
575
+ type: 'image/jpeg',
576
+ pixelRatio: 2,
577
+ quality: 0.8,
578
+ toType: 'url',
579
+ onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
580
+ })
581
+
582
+ // 6) 客户端直连打印(需要本地客户端)
583
+ setHiwebSocket('http://127.0.0.1:17521', 'your-token')
584
+ connect()
585
+ // 可选:查看打印机列表
586
+ const printers = getPrinterList(tpl)
587
+ // 使用模板对象直接发起直连打印
588
+ tpl.print2(printData, { printer: printers?.[0]?.name || '' })
589
+
590
+ // 7) 断开连接
591
+ disconnect()
181
592
  ```
182
593
 
183
- ## Preview 预览组件
594
+ ### 导出 PDF 两种模式
595
+
596
+ - 普通导出(按模板自身纸张与分页设置):
597
+
598
+ ```js
599
+ // 不传纸张尺寸,使用模板的面板尺寸与分页配置
600
+ exportPdf(tpl, printData, '打印预览pdf', {
601
+ onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
602
+ })
603
+ ```
604
+
605
+ - A4 排版导出(把小模板排到指定纸张):
606
+
607
+ ```js
608
+ // 传入纸张尺寸与排版参数,将多个模板排到一张 A4
609
+ exportPdf(tpl, printData, '打印预览pdf', {
610
+ paperWidth: 210, // mm
611
+ paperHeight: 297, // mm
612
+ scale: 2, // 缩放系数(影响清晰度与排版)
613
+ perPage: 6, // 每页排版的模板数量
614
+ leftOffset: -1, // 左对齐偏移(mm)
615
+ topOffset: -1, // 上对齐偏移(mm)
616
+ onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
617
+ })
618
+ ```
619
+
620
+ ### 导出图片 两种模式
621
+
622
+ - 普通导出(按模板自身纸张与分页设置):
623
+
624
+ ```js
625
+ // 不传纸张尺寸,按模板分页导出图片
626
+ await exportImage(tpl, printData, {
627
+ isDownload: true,
628
+ name: '图片',
629
+ limit: 1, // 多少页合成一张图片;0 或不传则导出整图
630
+ type: 'image/jpeg', // 或 'image/png'
631
+ pixelRatio: 2, // 像素比例(清晰度)
632
+ quality: 0.8, // 仅 JPEG 有效(0-1)
633
+ toType: 'url', // 不下载时返回类型:'url' 或 'blob'
634
+ onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
635
+ })
636
+ ```
637
+
638
+ - A4 排版导出(把小模板排到指定纸张):
639
+
640
+ ```js
641
+ // 传入纸张尺寸以 A4 方式排版导出
642
+ await exportImage(tpl, printData, {
643
+ paperWidth: 210, // mm
644
+ paperHeight: 297, // mm
645
+ limit: 6, // 每张合成 6 页
646
+ isDownload: true,
647
+ name: 'A4排版',
648
+ type: 'image/jpeg',
649
+ pixelRatio: 2,
650
+ quality: 0.8
651
+ })
652
+ ```
653
+
654
+ ### 参数说明(PDF/图片通用,含描述)
655
+
656
+ - `paperWidth`、`paperHeight` 纸张尺寸(mm);传入后按该纸张排版,不传则使用模板自身尺寸与分页
657
+ - `scale` 渲染缩放(PDF);提升清晰度或控制排版密度
658
+ - `perPage` 每页模板数量(PDF);用于小模板排版到大纸张
659
+ - `leftOffset`、`topOffset` 对齐偏移(mm);微调整体排版位置(PDF)
660
+ - `limit` 图片合并页数;1 表示每张 1 页,6 表示每张 6 页;0/不传导出整图
661
+ - `type` 图片格式;`'image/jpeg'` 或 `'image/png'`
662
+ - `pixelRatio` 像素比例(图片);越大越清晰但文件更大
663
+ - `quality` 图片质量(0-1,仅 JPEG 有效)
664
+ - `toType` 返回类型(不下载时);`'url'` 或 `'blob'`
665
+ - `isDownload` 是否直接下载图片文件
666
+ - `name` 文件名或下载名
667
+ - `onProgress(cur, total)` 导出进度回调;以 `cur/total` 计算百分比
668
+
669
+ ### 导入说明(含描述)
670
+
671
+ - 所有运行时 API 由库入口直接导出;无需另装 `vue-plugin-hiprint`
672
+ - 如需自定义元素提供器,在调用前执行 `hiprint.init({ providers: [...] })`;默认已初始化内置提供器
184
673
 
185
- `Preview` 组件用于展示打印预览,支持多种导出格式(PDF、图片)及打印机选择。
674
+ ### 适用场景(含描述)
186
675
 
187
- ### 组件参数 (Props)
676
+ - 后台页面、弹窗、任务流:仅需“模板 + 数据”的预览/打印/导出能力
677
+ - 脚本或批量任务:结合 `exportPdf`/`exportImage` 实现批量导出
188
678
 
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%' | 弹窗宽度 | |
200
679
 
201
- ### 方法 (Exposed Methods)
680
+ ## 注意事项
202
681
 
203
- | 方法名 | 参数 | 说明 |
204
- | :--- | :--- | :--- |
205
- | init | (template, data) | 初始化模板和数据 |
206
- | show | (template, data, width) | 显示预览弹窗(会自动调用 render) |
207
- | hideModal | - | 关闭弹窗 |
208
- | getTarget | - | 获取当前模板实例 |
682
+ - 建议在 `index.html` 注入 `print-lock.css`,保证打印样式稳定
683
+ - 直接打印需配套客户端(Electron 或服务端),跨域部署需启用 HTTPS
684
+ - 若使用自定义 `host/token`,确保客户端配置一致且网络可达
685
+ - SSR 兼容:插件安装仅在浏览器环境执行(`typeof window !== 'undefined'`),在 SSR 框架中引入不会因 `window` 未定义报错。
209
686
 
210
- ### 使用示例
687
+ ## 完整示例
211
688
 
212
689
  ```vue
213
690
  <template>
214
- <div>
215
- <el-button @click="testPreview">显示预览组件</el-button>
216
- <Preview ref="previewRef" v-model:modalShow="modalShow" />
691
+ <FullDesigner
692
+ :initial-template="tpl"
693
+ :initial-print-data="rows"
694
+ default-lang="cn"
695
+ :hi-host="host"
696
+ :hi-token="token"
697
+ :hi-auto-connect="true"
698
+ @save="onSave"
699
+ />
700
+
701
+ <div style="margin-top: 12px;">
702
+ <el-input v-model="host" placeholder="客户端地址,如 http://127.0.0.1:17521" style="width: 360px; margin-right: 8px;" />
703
+ <el-input v-model="token" placeholder="鉴权令牌" style="width: 200px; margin-right: 8px;" />
704
+ <el-button type="primary" @click="applyHostToken">应用连接参数</el-button>
705
+ <el-button style="margin-left:8px;" @click="preview">预览</el-button>
706
+ <el-button style="margin-left:8px;" @click="printBrowser">浏览器打印</el-button>
217
707
  </div>
218
708
  </template>
219
709
 
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
- };
710
+ <script setup>
711
+ import { ref } from 'vue'
712
+
713
+ // 模板(示例)
714
+ const tpl = {
715
+ panels: [
716
+ {
717
+ index: 0,
718
+ paperType: 'A4',
719
+ width: 210,
720
+ height: 297,
721
+ printElements: [
722
+ {
723
+ options: { left: 20, top: 20, title: '标题', field: 'title', fontSize: 18 },
724
+ printElementType: { type: 'text' }
725
+ },
726
+ {
727
+ options: { left: 20, top: 50, title: '姓名', field: 'name' },
728
+ printElementType: { type: 'text' }
729
+ }
730
+ ]
731
+ }
732
+ ]
733
+ }
734
+
735
+ // 数据(对象或数组均可)
736
+ const rows = [{ title: '病理图文报告', name: '东东' }]
737
+
738
+ // 连接参数
739
+ const host = ref('http://127.0.0.1:17521')
740
+ const token = ref('')
741
+
742
+ // 组件引用
743
+ const designer = ref(null)
744
+
745
+ const applyHostToken = () => {
746
+ // 设置客户端地址与令牌,并尝试重连
747
+ designer.value?.setHiwebSocket(host.value, token.value)
748
+ }
749
+
750
+ // 预览与浏览器打印
751
+ const preview = () => designer.value?.preView()
752
+ const printBrowser = () => designer.value?.printView()
753
+
754
+ // 保存事件:拿到模板与数据入库
755
+ const onSave = ({ template, data, templateId }) => {
756
+ // 持久化示例
757
+ // await api.save({ template, data, templateId })
758
+ console.log('save payload:', { template, data, templateId })
759
+ }
239
760
  </script>
240
761
  ```