vg-print 1.0.343 → 1.0.345

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