vg-print 1.0.362 → 1.0.364

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
@@ -20,6 +20,8 @@ npm搜索vg-print.
20
20
  - 通过属性配置 `hiwebSocket` 的 `host`、`token` 与自动连接
21
21
  - 暴露实例方法:预览、打印、导出、连接控制等,外部可直接调用
22
22
  - 集成并导出 `hiprint`,可进行模板对象级的高级定制
23
+ - 内置 `@sv-print/plugin-api-pdf3` 与 `@sv-print/plugin-api-image` 插件,提供更强大的导出功能
24
+
23
25
 
24
26
  ## 安装
25
27
 
@@ -387,7 +389,7 @@ hiprint.setConfig({ showAdsorbLine: true, showPosition: true, showSizeBox: true
387
389
  hiprint.register({ authKey: '', plugins: [] })
388
390
  // 初始化元素提供器 (默认元素提供器)
389
391
  hiprint.init({
390
- providers: [new defaultElementTypeProvider()],
392
+ // providers: [new defaultElementTypeProvider()],
391
393
  host: 'http://localhost:17521',
392
394
  token: '',
393
395
  lang: 'en'
@@ -411,30 +413,67 @@ hiprint.init({
411
413
 
412
414
 
413
415
  // 导出 PDF(内置示例参数)
414
- // 模式1: 小模版需要在一页中排列的话,增加增加页面宽高参数(想把多订单放在一页中排列)----小模板拼到大纸张(例如 A4)
415
- tpl.toPdf([{ name: '示例1' },{ name: '示例2' },{ name: '示例3' }], '打印预览pdf', {
416
+ // 方式 1: 原生导出 (toPdf)
417
+ // 模式1: 小模版需要在一页中排列的话...数据是数组格式
418
+ tpl.toPdf([...], '打印预览pdf', {
419
+ isDownload: true, // 不自动下载
420
+ type: "blob", // 默认 blob 支持: blob、bloburl、pdfobjectnewwindow、dataurl --> jspdf.output(type)
421
+ onProgress: (cur, total) => {
422
+ const percent = Math.floor((cur / total) * 100);
423
+ console.log("toPdf 进度", percent);
424
+ // 导出进度100%的时候关闭等待
425
+ if (cur === total) {
426
+ // 导出完成
427
+ }
428
+ },
429
+
430
+ })
431
+
432
+ // 方式 2: 插件导出 (toPdf2) - 推荐
433
+ // 支持更多选项,如 isDownload, type (blob/url/...)
434
+ tpl.toPdf2(this.printDataVar, '文件名', {
416
435
  paperWidth: 210,
417
436
  paperHeight: 297,
418
437
  scale: 2,
419
- perPage: 6, // 每页模板数
420
- leftOffset:-1, // 对齐偏移
421
- topOffset:-1, // 对齐偏移
422
- onProgress: (cur, total) => {
423
- console.log('toPdf 进度', Math.floor((cur/total)*100))
424
- }
425
- })
426
- // 模式2:普通模板,导出预览全部页:
427
- tpl.toPdf(this.printDataVar, '打印预览pdf',{
428
- onProgress: (cur, total) => {
438
+ perPage: 6,
439
+ leftOffset:-1,
440
+ topOffset:-1,
441
+ onProgress: (cur: number, total: number) => {
429
442
  console.log('toPdf 进度', Math.floor((cur/total)*100))
443
+ if (cur === total) {
444
+ // 导出完成
430
445
  }
431
- });
446
+ }
447
+ })
432
448
 
433
449
  // 导出图片(内置示例参数)
434
- // 导出为多张图片,每张合成 6 页,自动下载 JPEG
435
- 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)) })
436
- //按 A4 纸张排版导出(把小模板排到 A4):
437
- tpl.toImage({ name: '示例' }, { paperWidth: 210, paperHeight: 297, limit: 6, isDownload: true, name: 'A4排版', type: 'image/jpeg', pixelRatio: 2 })
450
+ // 方式 1: 原生导出 (toImage)
451
+ tpl.toImage({ name: '示例' }, {
452
+ isDownload: true, // 不自动下载
453
+ name: "图片名称",
454
+ limit: 1, // 默认 10 多少页为一个 图片文件, 太多 图片文件就很长
455
+ type: "image/jpeg", // 默认 image/jpeg --> canvas.toDataURL(type, quality)
456
+ pixelRatio: 2, // 图片像素比 2: 提高清晰度 默认 window.devicePixelRatio.
457
+ quality: 0.8, // 默认 0.92 图片质量 0-1
458
+ toType: "url", // 默认 url 支持: url、blob
459
+ onProgress: (cur, total) => {
460
+ const percent = Math.floor((cur / total) * 100);
461
+ console.log("toImage 进度", percent);
462
+ if (cur === total) {
463
+ // 导出完成
464
+ }
465
+ },
466
+ })
467
+
468
+ // 方式 2: 插件导出 (toImage2) - 推荐
469
+ // 支持跨域图片(useCORS), 更多格式控制
470
+ tpl.toImage2(this.printDataVar, {
471
+ isDownload: true,
472
+ name: '图片名称',
473
+ type: 'image/jpeg',
474
+ useCORS: true, // 开启跨域支持
475
+ onProgress: (cur, total) => { ... }
476
+ })
438
477
 
439
478
  ```
440
479
 
@@ -530,7 +569,9 @@ import {
530
569
  getHtml,
531
570
  printBrowser,
532
571
  exportPdf,
572
+ exportPdf2, // 插件导出
533
573
  exportImage,
574
+ exportImage2, // 插件导出
534
575
  getPrinterList,
535
576
  setHiwebSocket,
536
577
  connect,
@@ -559,27 +600,57 @@ const preview = async () => {
559
600
  // 3) 浏览器打印(不需要客户端)
560
601
  printBrowser(tpl, printData)
561
602
 
562
- // 4) 导出 PDF(示例参数)
603
+ // 4) 导出 PDF
604
+ // 插件方法 (推荐)
563
605
  exportPdf(tpl, printData, '打印预览pdf', {
564
- paperWidth: 210,
565
- paperHeight: 297,
566
- scale: 2,
567
- perPage: 6,
568
- leftOffset: -1,
569
- topOffset: -1,
570
- onProgress: (cur, total) => console.log('toPdf 进度', Math.floor((cur/total)*100))
606
+ isDownload: true, // 不自动下载
607
+ type: "blob", // 默认 blob 支持: blob、bloburl、pdfobjectnewwindow、dataurl --> jspdf.output(type)
608
+ onProgress: (cur, total) => {
609
+ const percent = Math.floor((cur / total) * 100);
610
+ console.log("toPdf 进度", percent);
611
+ },
612
+ })
613
+ // 原生方法
614
+ exportPdf2(tpl, printData, '打印预览pdf', {
615
+ paperWidth: 210,
616
+ paperHeight: 297,
617
+ scale: 2,
618
+ perPage: 6,
619
+ leftOffset:-1,
620
+ topOffset:-1,
621
+ onProgress: (cur: number, total: number) => {
622
+ console.log('toPdf 进度', Math.floor((cur/total)*100))
623
+ }
571
624
  })
572
625
 
573
- // 5) 导出图片(示例参数)
626
+ // 5) 导出图片
627
+ // 插件方法 (推荐)
574
628
  await exportImage(tpl, printData, {
575
- isDownload: true,
576
- name: 'A4排版',
577
- limit: 1, // 导出多张时设置每张合成的页数;不传或设为 0 合成整图
578
- type: 'image/jpeg',
579
- pixelRatio: 2,
580
- quality: 0.8,
581
- toType: 'url',
582
- onProgress: (cur, total) => console.log('toImage 进度', Math.floor((cur/total)*100))
629
+ isDownload: true, // 不自动下载
630
+ name: "图片名称",
631
+ limit: 1, // 默认 10 多少页为一个 图片文件, 太多 图片文件就很长
632
+ type: "image/jpeg", // 默认 image/jpeg --> canvas.toDataURL(type, quality)
633
+ pixelRatio: 2, // 图片像素比 2: 提高清晰度 默认 window.devicePixelRatio.
634
+ quality: 0.8, // 默认 0.92 图片质量 0-1
635
+ toType: "url", // 默认 url 支持: url、blob
636
+ onProgress: (cur, total) => {
637
+ const percent = Math.floor((cur / total) * 100);
638
+ console.log("toImage 进度", percent);
639
+ },
640
+ })
641
+ // 原生方法
642
+ await exportImage2(tpl, printData, {
643
+ isDownload: true,
644
+ name: '图片名称',
645
+ limit: 1,
646
+ type: 'image/jpeg',
647
+ pixelRatio: 2,
648
+ quality: 0.8,
649
+ toType: 'url',
650
+ useCORS: true, // 开启跨域支持
651
+ onProgress: (cur: number, total: number) => {
652
+ console.log('toImage 进度', Math.floor((cur/total)*100))
653
+ }
583
654
  })
584
655
 
585
656
  // 6) 客户端直连打印(需要本地客户端)
@@ -0,0 +1,87 @@
1
+ import { l as b } from "./index-CwLU3yfY.js";
2
+ function M(e) {
3
+ return typeof e == "string" && /^data:image\/svg\+xml/i.test(e);
4
+ }
5
+ function C(e) {
6
+ let r = e.indexOf(",");
7
+ return r >= 0 ? decodeURIComponent(e.slice(r + 1)) : "";
8
+ }
9
+ function v(e) {
10
+ return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(e)}`;
11
+ }
12
+ function F(e) {
13
+ let r = [], t = "", i = 0;
14
+ for (let o = 0; o < e.length; o++) {
15
+ let n = e[o];
16
+ n === "(" && i++, n === ")" && (i = Math.max(0, i - 1)), n === ";" && i === 0 ? (r.push(t), t = "") : t += n;
17
+ }
18
+ return t.trim() && r.push(t), r.map((o) => o.trim()).filter(Boolean);
19
+ }
20
+ function k(e) {
21
+ let r = [], t = "", i = 0;
22
+ for (let n = 0; n < e.length; n++) {
23
+ let s = e[n];
24
+ s === "(" && i++, s === ")" && (i = Math.max(0, i - 1)), s === "," && i === 0 ? (r.push(t.trim()), t = "") : t += s;
25
+ }
26
+ t.trim() && r.push(t.trim());
27
+ let o = [];
28
+ for (let n of r) {
29
+ if (/\binset\b/i.test(n)) continue;
30
+ let s = n.match(/-?\d+(?:\.\d+)?px/gi) || [], [l = "0px", a = "0px", m = "0px"] = s, u = n.replace(/-?\d+(?:\.\d+)?px/gi, "").replace(/\binset\b/ig, "").trim().replace(/\s{2,}/g, " "), d = !!u && u !== ",";
31
+ o.push(`drop-shadow(${l} ${a} ${m}${d ? ` ${u}` : ""})`);
32
+ }
33
+ return o.join(" ");
34
+ }
35
+ function y(e) {
36
+ let r = F(e), t = null, i = null, o = null, n = [];
37
+ for (let l of r) {
38
+ let a = l.indexOf(":");
39
+ if (a < 0) continue;
40
+ let m = l.slice(0, a).trim().toLowerCase(), u = l.slice(a + 1).trim();
41
+ m === "box-shadow" ? o = u : m === "filter" ? t = u : m === "-webkit-filter" ? i = u : n.push([m, u]);
42
+ }
43
+ if (o) {
44
+ let l = k(o);
45
+ l && (t = t ? `${t} ${l}` : l, i = i ? `${i} ${l}` : l);
46
+ }
47
+ let s = [...n];
48
+ return t && s.push(["filter", t]), i && s.push(["-webkit-filter", i]), s.map(([l, a]) => `${l}:${a}`).join(";");
49
+ }
50
+ function I(e) {
51
+ return e.replace(/([^{}]+)\{([^}]*)\}/g, (r, t, i) => `${t}{${y(i)}}`);
52
+ }
53
+ function N(e) {
54
+ return e = e.replace(/<style[^>]*>([\s\S]*?)<\/style>/gi, (r, t) => r.replace(t, I(t))), e = e.replace(/style=(['"])([\s\S]*?)\1/gi, (r, t, i) => `style=${t}${y(i)}${t}`), e;
55
+ }
56
+ function S(e) {
57
+ if (!b() || !M(e)) return e;
58
+ try {
59
+ let r = C(e), t = N(r);
60
+ return v(t);
61
+ } catch {
62
+ return e;
63
+ }
64
+ }
65
+ async function R(e, r) {
66
+ let { width: t, height: i, scale: o = 1, dpr: n = 1, meta: s = {}, backgroundColor: l } = r;
67
+ e = S(e);
68
+ let a = new Image();
69
+ a.loading = "eager", a.decoding = "sync", a.crossOrigin = "anonymous", a.src = e, await a.decode();
70
+ let m = a.naturalWidth, u = a.naturalHeight, d = Number.isFinite(s.w0) ? s.w0 : m, x = Number.isFinite(s.h0) ? s.h0 : u, c, f, $ = Number.isFinite(t), w = Number.isFinite(i);
71
+ if ($ && w) c = Math.max(1, t), f = Math.max(1, i);
72
+ else if ($) {
73
+ let g = t / Math.max(1, d);
74
+ c = t, f = x * g;
75
+ } else if (w) {
76
+ let g = i / Math.max(1, x);
77
+ f = i, c = d * g;
78
+ } else c = m, f = u;
79
+ c = c * o, f = f * o;
80
+ let h = document.createElement("canvas");
81
+ h.width = c * n, h.height = f * n, h.style.width = `${c}px`, h.style.height = `${f}px`;
82
+ let p = h.getContext("2d");
83
+ return n !== 1 && p.scale(n, n), l && (p.save(), p.fillStyle = l, p.fillRect(0, 0, c, f), p.restore()), p.drawImage(a, 0, 0, c, f), h;
84
+ }
85
+ export {
86
+ R as W
87
+ };
@@ -0,0 +1,8 @@
1
+ import { W as a } from "./chunk-3VYIPD3A-CWjvYN8r.js";
2
+ async function o(r, t) {
3
+ let i = await a(r, t), e = new Image();
4
+ return e.src = i.toDataURL(`image/${t.format}`, t.quality), await e.decode(), e.style.width = `${i.width / t.dpr}px`, e.style.height = `${i.height / t.dpr}px`, e;
5
+ }
6
+ export {
7
+ o as d
8
+ };
@@ -0,0 +1,13 @@
1
+ import { W as m } from "./chunk-3VYIPD3A-CWjvYN8r.js";
2
+ async function a(n, e) {
3
+ let o = e.type;
4
+ if (o === "svg") {
5
+ let t = decodeURIComponent(n.split(",")[1]);
6
+ return new Blob([t], { type: "image/svg+xml" });
7
+ }
8
+ let i = await m(n, e);
9
+ return new Promise((t) => i.toBlob((l) => t(l), `image/${o}`, e.quality));
10
+ }
11
+ export {
12
+ a as m
13
+ };
@@ -0,0 +1,15 @@
1
+ import { m as f } from "./chunk-T4JIPPZO-CMzWM7_N.js";
2
+ import { W as t } from "./chunk-3VYIPD3A-CWjvYN8r.js";
3
+ async function w(n, e) {
4
+ let c = ((e == null ? void 0 : e.format) || (e == null ? void 0 : e.type) || "").toLowerCase(), a = c === "jpg" ? "jpeg" : c || "png", i = (e == null ? void 0 : e.filename) || `snapdom.${a}`, o = { ...e || {}, format: a, type: a };
5
+ if (o.dpr = 1, a === "svg") {
6
+ let d = await f(n, { ...o, type: "svg" }), l = URL.createObjectURL(d), m = document.createElement("a");
7
+ m.href = l, m.download = i, m.click(), URL.revokeObjectURL(l);
8
+ return;
9
+ }
10
+ let p = await t(n, o), r = document.createElement("a");
11
+ r.href = p.toDataURL(`image/${a}`, e == null ? void 0 : e.quality), r.download = i, r.click();
12
+ }
13
+ export {
14
+ w as download
15
+ };