vg-print 1.0.224 → 1.0.226
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 +148 -0
- package/dist/style.css +1 -1
- package/dist/vg-print.es.js +3596 -3564
- package/dist/vg-print.umd.js +7 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -177,6 +177,154 @@ tpl.toPdf({ name: '示例' }, '打印预览pdf', { paperWidth: 210, paperHeight:
|
|
|
177
177
|
tpl.toImage({ name: '示例' }, { isDownload: true, name: '图片', limit: 1, type: 'image/jpeg', pixelRatio: 2 })
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
+
## 轻量运行时 API(无需设计器 UI)
|
|
181
|
+
|
|
182
|
+
当你只想“拿模板 + 数据”直接做预览、浏览器打印、导出 PDF/图片或进行客户端直连打印,而不引入页面设计器时,使用以下 API:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
import {
|
|
186
|
+
createTemplate,
|
|
187
|
+
getHtml,
|
|
188
|
+
printBrowser,
|
|
189
|
+
exportPdf,
|
|
190
|
+
exportImage,
|
|
191
|
+
getPrinterList,
|
|
192
|
+
setHiwebSocket,
|
|
193
|
+
connect,
|
|
194
|
+
disconnect
|
|
195
|
+
} from 'vg-print'
|
|
196
|
+
|
|
197
|
+
// 1) 创建模板实例(可复用)
|
|
198
|
+
const tpl = createTemplate(tmplJson)
|
|
199
|
+
|
|
200
|
+
// 2) 生成预览 HTML,自行渲染到容器
|
|
201
|
+
const html = getHtml(tpl, printData)
|
|
202
|
+
document.querySelector('#preview').innerHTML = html
|
|
203
|
+
|
|
204
|
+
// 3) 浏览器打印(不需要客户端)
|
|
205
|
+
printBrowser(tpl, printData)
|
|
206
|
+
|
|
207
|
+
// 4) 导出 PDF(示例参数)
|
|
208
|
+
exportPdf(tpl, printData, '打印预览pdf', {
|
|
209
|
+
paperWidth: 210,
|
|
210
|
+
paperHeight: 297,
|
|
211
|
+
scale: 2,
|
|
212
|
+
perPage: 6,
|
|
213
|
+
leftOffset: -1,
|
|
214
|
+
topOffset: -1,
|
|
215
|
+
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
// 5) 导出图片(示例参数)
|
|
219
|
+
await exportImage(tpl, printData, {
|
|
220
|
+
isDownload: true,
|
|
221
|
+
name: 'A4排版',
|
|
222
|
+
limit: 1, // 导出多张时设置每张合成的页数;不传或设为 0 合成整图
|
|
223
|
+
type: 'image/jpeg',
|
|
224
|
+
pixelRatio: 2,
|
|
225
|
+
quality: 0.8,
|
|
226
|
+
toType: 'url',
|
|
227
|
+
onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
// 6) 客户端直连打印(需要本地客户端)
|
|
231
|
+
setHiwebSocket('http://127.0.0.1:17521', 'your-token')
|
|
232
|
+
connect()
|
|
233
|
+
// 可选:查看打印机列表
|
|
234
|
+
const printers = getPrinterList(tpl)
|
|
235
|
+
// 使用模板对象直接发起直连打印
|
|
236
|
+
tpl.print2(printData, { printer: printers?.[0]?.name || '' })
|
|
237
|
+
|
|
238
|
+
// 7) 断开连接
|
|
239
|
+
disconnect()
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### 导出 PDF 两种模式
|
|
243
|
+
|
|
244
|
+
- 普通导出(按模板自身纸张与分页设置):
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
// 不传纸张尺寸,使用模板的面板尺寸与分页配置
|
|
248
|
+
exportPdf(tpl, printData, '打印预览pdf', {
|
|
249
|
+
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
250
|
+
})
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
- A4 排版导出(把小模板排到指定纸张):
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
// 传入纸张尺寸与排版参数,将多个模板排到一张 A4
|
|
257
|
+
exportPdf(tpl, printData, '打印预览pdf', {
|
|
258
|
+
paperWidth: 210, // mm
|
|
259
|
+
paperHeight: 297, // mm
|
|
260
|
+
scale: 2, // 缩放系数(影响清晰度与排版)
|
|
261
|
+
perPage: 6, // 每页排版的模板数量
|
|
262
|
+
leftOffset: -1, // 左对齐偏移(mm)
|
|
263
|
+
topOffset: -1, // 上对齐偏移(mm)
|
|
264
|
+
onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### 导出图片 两种模式
|
|
269
|
+
|
|
270
|
+
- 普通导出(按模板自身纸张与分页设置):
|
|
271
|
+
|
|
272
|
+
```js
|
|
273
|
+
// 不传纸张尺寸,按模板分页导出图片
|
|
274
|
+
await exportImage(tpl, printData, {
|
|
275
|
+
isDownload: true,
|
|
276
|
+
name: '图片',
|
|
277
|
+
limit: 1, // 多少页合成一张图片;0 或不传则导出整图
|
|
278
|
+
type: 'image/jpeg', // 或 'image/png'
|
|
279
|
+
pixelRatio: 2, // 像素比例(清晰度)
|
|
280
|
+
quality: 0.8, // 仅 JPEG 有效(0-1)
|
|
281
|
+
toType: 'url', // 不下载时返回类型:'url' 或 'blob'
|
|
282
|
+
onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
|
|
283
|
+
})
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
- A4 排版导出(把小模板排到指定纸张):
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
// 传入纸张尺寸以 A4 方式排版导出
|
|
290
|
+
await exportImage(tpl, printData, {
|
|
291
|
+
paperWidth: 210, // mm
|
|
292
|
+
paperHeight: 297, // mm
|
|
293
|
+
limit: 6, // 每张合成 6 页
|
|
294
|
+
isDownload: true,
|
|
295
|
+
name: 'A4排版',
|
|
296
|
+
type: 'image/jpeg',
|
|
297
|
+
pixelRatio: 2,
|
|
298
|
+
quality: 0.8
|
|
299
|
+
})
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### 参数说明(PDF/图片通用)
|
|
303
|
+
|
|
304
|
+
- `paperWidth`、`paperHeight`:整数或浮点数,单位 mm;传入后按该纸张排版,不传则使用模板自身尺寸与分页。
|
|
305
|
+
- `scale`:缩放系数;用于提高渲染清晰度或控制排版密度(PDF)。
|
|
306
|
+
- `perPage`:每页排版的模板数量(PDF);通常用于小模板拼接到 A4。
|
|
307
|
+
- `leftOffset`、`topOffset`:对齐偏移(mm),用于微调整体排版位置(PDF)。
|
|
308
|
+
- `limit`:图片导出的合并页数。1 表示每张合 1 页;6 表示每张合 6 页;0 或不传表示导出整图。
|
|
309
|
+
- `type`:导出图片格式,`'image/jpeg'` 或 `'image/png'`。
|
|
310
|
+
- `pixelRatio`:像素比例;越大越清晰,文件越大(图片)。
|
|
311
|
+
- `quality`:图片质量(0-1,仅 JPEG 有效)。
|
|
312
|
+
- `toType`:不下载时返回类型(`'url'` 或 `'blob'`)。
|
|
313
|
+
- `isDownload`:是否直接下载文件(图片)。
|
|
314
|
+
- `name`:文件名或下载名。
|
|
315
|
+
- `onProgress(cur, total)`:导出进度回调;`cur/total` 计算百分比。
|
|
316
|
+
|
|
317
|
+
### 导入说明
|
|
318
|
+
|
|
319
|
+
- 上述 API 已由库入口直接导出,无需单独安装 `vue-plugin-hiprint`。
|
|
320
|
+
- 若需要自定义元素提供器,可在调用前执行 `hiprint.init({ providers: [...] })`;默认已初始化内置提供器。
|
|
321
|
+
|
|
322
|
+
### 适用场景
|
|
323
|
+
|
|
324
|
+
- 后台页面、弹窗、任务流等,只做“数据 + 模板”的快速预览/打印/导出
|
|
325
|
+
- 服务端或脚本批量导出(结合 `exportPdf`/`exportImage`)
|
|
326
|
+
|
|
327
|
+
|
|
180
328
|
## 注意事项
|
|
181
329
|
|
|
182
330
|
- 建议在 `index.html` 注入 `print-lock.css`,保证打印样式稳定
|