@borurio-print/print-designer 1.0.13 → 1.0.15

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
@@ -69,6 +69,173 @@ import '@borurio-print/print-designer/style.css'
69
69
 
70
70
  设计器界面直接使用中文文案,业务应用无需注册 `vue-i18n`、合并语言包或传入语言代码。
71
71
 
72
+ ### 本地打印控件连接与设置
73
+
74
+ `PrintPlugin` 提供通用打印机、纸张设置与本地 WebSocket 连接。它沿用现有 Electron
75
+ JSON 协议,独立使用时不需要挂载模板编辑器,也不依赖业务系统的路由、Store 或 Element Plus。
76
+
77
+ ```vue
78
+ <script setup lang="ts">
79
+ import { ref } from 'vue'
80
+ import {
81
+ PrintPlugin,
82
+ type PrintPluginExpose,
83
+ type PrintResult,
84
+ } from '@borurio-print/print-designer'
85
+ import '@borurio-print/print-designer/style.css'
86
+
87
+ const pluginRef = ref<PrintPluginExpose | null>(null)
88
+
89
+ function handleResult(result: PrintResult): void {
90
+ console.log(result.requestId, result.result, result.message)
91
+ }
92
+ </script>
93
+
94
+ <template>
95
+ <button @click="pluginRef?.showEdit()">打印机设置</button>
96
+ <PrintPlugin
97
+ ref="pluginRef"
98
+ endpoint="ws://127.0.0.1:9274"
99
+ scene-key="inventorySku"
100
+ :auto-connect="true"
101
+ @print-result="handleResult"
102
+ />
103
+ </template>
104
+ ```
105
+
106
+ 父组件配置:
107
+
108
+ | 属性 | 默认值 | 说明 |
109
+ | --- | --- | --- |
110
+ | `endpoint` | `ws://127.0.0.1:9274` | 打印控件地址 |
111
+ | `sceneKey` | `default` | 打印机和纸张配置的保存场景;兼容旧属性 `printKey` |
112
+ | `client` | `PC` | 协议中的客户端标识 |
113
+ | `autoConnect` | `false` | 挂载后自动连接;否则打开设置或调用 `connectPrint()` 时连接 |
114
+ | `connectTimeoutMs` | `5000` | 连接及保存设置的超时时间 |
115
+ | `printTimeoutMs` | `60000` | 等待打印结果的超时时间 |
116
+ | `maxPdfBytes` | `67108864` | WebSocket 二进制 PDF 的最大字节数,默认 64 MB |
117
+ | `persistSettings` | `true` | 向打印控件保存和读取场景设置 |
118
+ | `defaultPrinter` | 空字符串 | 场景没有已保存打印机时使用,之后回退到系统默认打印机 |
119
+ | `defaultPaper` | 空字符串 | 场景没有已保存纸张时使用 |
120
+ | `theme` | 内置主题 | 主色配置,同时应用于设置弹窗和下拉浮层 |
121
+
122
+ 暴露方法为 `showEdit()`、`hideEdit()`、`connectPrint()`、`disconnect()`、
123
+ `refreshPrinters()`、`getPrintData()`、`setPrintUrl()` 和 `printPdf()`。
124
+ 连接成功表示 WebSocket 已打开;打印机和纸张列表随后异步返回。
125
+
126
+ 对于控件能够访问的 HTTP/HTTPS PDF 地址,可使用:
127
+
128
+ ```ts
129
+ const requestId = pluginRef.value!.setPrintUrl({
130
+ url: 'https://example.com/document.pdf',
131
+ requestId: crypto.randomUUID(),
132
+ key: 'printUrlList',
133
+ })
134
+ ```
135
+
136
+ 对于设计器生成的 PDF Blob,直接通过 WebSocket 二进制帧发送给本地打印控件,
137
+ 不需要 Base64、Data URI 或上传服务器:
138
+
139
+ ```ts
140
+ const requestId = crypto.randomUUID()
141
+
142
+ designerRef.value!.generatePdf({
143
+ requestId: `${requestId}:generate`,
144
+ data: printData,
145
+ returnBlob: true,
146
+ })
147
+
148
+ function handleGenerateResult(result: PrintResult): void {
149
+ if (result.requestId !== `${requestId}:generate`) return
150
+ if (result.result === 'failed' || !result.data?.blob) return
151
+
152
+ pluginRef.value!.printPdf({
153
+ requestId,
154
+ blob: result.data.blob,
155
+ copies: 1,
156
+ orientation: 'portrait',
157
+ })
158
+ }
159
+ ```
160
+
161
+ 二进制任务由打印控件写入临时 PDF 后交给系统打印机,结束后删除临时文件。
162
+ 业务系统应使用同一个 `requestId` 关联提交和最终结果。
163
+
164
+ 最终结果通过 `print-result` 返回 `{ requestId, result, message }`。组件还提供
165
+ `connection-change`、`settings-change`,以及兼容原封装的 `componentOp` 原始消息事件。
166
+ 新打印任务要求控件回传同一个 `requestId`,每个任务只通知一次终态;超时或断线意味着结果未知,
167
+ 组件不会自动重新提交打印。卸载时关闭连接、清理监听和计时器。
168
+
169
+ 浏览器的 `blob:` 地址不能由独立的 Electron 进程读取,因此 `printPdf()` 发送的是原始
170
+ PDF 二进制内容。URL 打印协议继续保留,用于打印控件能够直接访问的 HTTP/HTTPS 文件。
171
+
172
+ ### 纯浏览器压缩面单
173
+
174
+ 压缩面单不需要先上传服务器,也不依赖本地打印控件完成排版。业务系统把原始面单 PDF
175
+ 地址和商品明细作为同一条业务记录传给 `PrintTemplateRenderer`,设计器会在浏览器内把
176
+ 面单第一页渲染到首页模板的 PDF 区域,并将商品表格分页后生成一个新的 PDF。
177
+
178
+ 在业务字段中注册面单 PDF:
179
+
180
+ ```ts
181
+ import type { BusinessFieldGroup } from '@borurio-print/print-designer'
182
+
183
+ const fieldGroups: BusinessFieldGroup[] = [
184
+ {
185
+ label: '压缩面单',
186
+ fields: [
187
+ {
188
+ field: 'waybillPdfUrl',
189
+ label: '面单PDF',
190
+ type: 'pdf',
191
+ },
192
+ {
193
+ field: 'recordPage',
194
+ label: '单据页码',
195
+ type: 'pageNumber',
196
+ },
197
+ ],
198
+ },
199
+ ]
200
+ ```
201
+
202
+ 模板应使用“双模板”:
203
+
204
+ - 首页模板放置“面单PDF”字段和普通表格,PDF 元素的固定区域决定原面单压缩后的尺寸。
205
+ - 续页模板只放置表格等补充信息,可以给表格更大高度。
206
+ - 首页和续页的表格使用同一个数据字段。分页时会分别根据两个模板中表格的实际高度计算容量。
207
+ - 从业务字段添加的页码在没有显式页码字段值时,自动显示当前业务记录内部的 `1/N`;基础组件中的页码仍显示整个打印任务的全局页码。
208
+
209
+ 调用时一条原始面单对应一条根记录:
210
+
211
+ ```ts
212
+ const requestId = crypto.randomUUID()
213
+
214
+ rendererRef.value!.previewPdf({
215
+ requestId,
216
+ data: [
217
+ {
218
+ waybillPdfUrl: 'https://example.com/waybill-a.pdf',
219
+ skuList: [
220
+ { sku: 'SKU-A', quantity: 2, location: 'A-01' },
221
+ { sku: 'SKU-B', quantity: 1, location: 'A-02' },
222
+ ],
223
+ },
224
+ {
225
+ waybillPdfUrl: 'https://example.com/waybill-b.pdf',
226
+ skuList: [
227
+ { sku: 'SKU-C', quantity: 3, location: 'B-01' },
228
+ ],
229
+ },
230
+ ],
231
+ })
232
+ ```
233
+
234
+ `previewPdf()` 会先在当前页面完成所有面单 PDF、二维码、条形码和表格的渲染,最终 Blob PDF
235
+ 生成完成后才打开新的预览页面。每条根记录都会重新使用首页模板并独立计算页码,不会把多个
236
+ 订单合并成同一个页码序列。浏览器可能拦截异步打开的新页面,业务系统需要允许当前站点打开
237
+ 弹窗;被拦截时,组件会通过 `print-result` 返回失败结果。
238
+
72
239
  ## npm 发布
73
240
 
74
241
  包名和 npm registry 已在 `package.json` 中配置:
@@ -224,4 +391,4 @@ pnpm version patch --no-git-tag-version
224
391
 
225
392
  ## 自动生成文件
226
393
 
227
- `src/auto-imports.d.ts` 和 `src/components.d.ts` 由自动按需引入插件生成;`pnpm-lock.yaml` 由 pnpm 生成。这些文件不应手动添加说明或修改内容。
394
+ `src/auto-imports.d.ts` 和 `src/components.d.ts` 由自动按需引入插件生成;`pnpm-lock.yaml` 由 pnpm 生成。这些文件不应手动添加说明或修改内容。
@@ -0,0 +1,46 @@
1
+ import type { PrintResult } from './core/types';
2
+ import type { LocalPrintConnectionState, LocalPrintEvent, LocalPrintPdfRequest, LocalPrintUrlRequest, PrintPluginProps, PrinterSetting } from './core/localPrintTypes';
3
+ declare function disconnect(): void;
4
+ declare function connectPrint(): Promise<void>;
5
+ declare function refreshPrinters(): Promise<void>;
6
+ declare function showEdit(): void;
7
+ declare function hideEdit(): void;
8
+ declare function getPrintData(): PrinterSetting & {
9
+ printVersion: number;
10
+ };
11
+ declare function setPrintUrl(request: LocalPrintUrlRequest): string;
12
+ declare function printPdf(request: LocalPrintPdfRequest): string;
13
+ declare const __VLS_export: import("vue").DefineComponent<PrintPluginProps, {
14
+ connectPrint: typeof connectPrint;
15
+ disconnect: typeof disconnect;
16
+ showEdit: typeof showEdit;
17
+ hideEdit: typeof hideEdit;
18
+ refreshPrinters: typeof refreshPrinters;
19
+ getPrintData: typeof getPrintData;
20
+ setPrintUrl: typeof setPrintUrl;
21
+ printPdf: typeof printPdf;
22
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
23
+ "print-result": (result: PrintResult) => any;
24
+ componentOp: (event: LocalPrintEvent) => any;
25
+ "connection-change": (state: LocalPrintConnectionState) => any;
26
+ "settings-change": (setting: PrinterSetting) => any;
27
+ }, string, import("vue").PublicProps, Readonly<PrintPluginProps> & Readonly<{
28
+ "onPrint-result"?: (result: PrintResult) => any;
29
+ onComponentOp?: (event: LocalPrintEvent) => any;
30
+ "onConnection-change"?: (state: LocalPrintConnectionState) => any;
31
+ "onSettings-change"?: (setting: PrinterSetting) => any;
32
+ }>, {
33
+ endpoint: string;
34
+ sceneKey: string;
35
+ printKey: string;
36
+ client: string;
37
+ autoConnect: boolean;
38
+ connectTimeoutMs: number;
39
+ printTimeoutMs: number;
40
+ maxPdfBytes: number;
41
+ persistSettings: boolean;
42
+ defaultPrinter: string;
43
+ defaultPaper: string;
44
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
45
+ declare const _default: typeof __VLS_export;
46
+ export default _default;
@@ -5,6 +5,12 @@ export interface DynamicTextContext {
5
5
  index: number;
6
6
  /** 本次打印数据数组的总长度。 */
7
7
  total: number;
8
+ /** 当前业务记录内的零基页号;未传时沿用整个任务页号。 */
9
+ recordPageIndex?: number;
10
+ /** 当前业务记录的总页数;未传时沿用整个任务总页数。 */
11
+ recordPageTotal?: number;
12
+ /** 当前打印页对应的业务记录。 */
13
+ data?: Record<string, unknown>;
8
14
  /** 整个打印批次共用的时间,避免多页之间出现秒数差异。 */
9
15
  printTime: Date;
10
16
  }
@@ -0,0 +1,61 @@
1
+ import type { PrintDesignerTheme, PrintResult } from './types';
2
+ export type LocalPrintConnectionState = 'disconnected' | 'connecting' | 'connected' | 'failed';
3
+ export interface PrinterSetting {
4
+ printer: string;
5
+ paper: string;
6
+ }
7
+ export interface LocalPrinterInfo {
8
+ name: string;
9
+ isDefault?: boolean;
10
+ }
11
+ export interface LocalPaperInfo {
12
+ name: string;
13
+ width?: number;
14
+ height?: number;
15
+ }
16
+ export interface LocalPrintEvent {
17
+ key: string;
18
+ requestId?: string;
19
+ data?: unknown;
20
+ }
21
+ export interface LocalPrintUrlRequest {
22
+ url: string | string[];
23
+ requestId?: string;
24
+ key?: 'localUrl' | 'printUrlList';
25
+ }
26
+ export interface LocalPrintPdfRequest {
27
+ blob: Blob;
28
+ requestId?: string;
29
+ copies?: number;
30
+ orientation?: 'portrait' | 'landscape';
31
+ }
32
+ export interface PrintPluginProps {
33
+ endpoint?: string;
34
+ sceneKey?: string;
35
+ /** 兼容原业务系统的场景属性。优先使用 sceneKey。 */
36
+ printKey?: string;
37
+ client?: string;
38
+ autoConnect?: boolean;
39
+ connectTimeoutMs?: number;
40
+ printTimeoutMs?: number;
41
+ maxPdfBytes?: number;
42
+ persistSettings?: boolean;
43
+ defaultPrinter?: string;
44
+ defaultPaper?: string;
45
+ theme?: PrintDesignerTheme;
46
+ onComponentOp?: (event: LocalPrintEvent) => void;
47
+ onPrintResult?: (result: PrintResult) => void;
48
+ onConnectionChange?: (state: LocalPrintConnectionState) => void;
49
+ }
50
+ export interface PrintPluginExpose {
51
+ connectPrint: () => Promise<void>;
52
+ disconnect: () => void;
53
+ showEdit: () => void;
54
+ hideEdit: () => void;
55
+ refreshPrinters: () => Promise<void>;
56
+ getPrintData: () => PrinterSetting & {
57
+ printVersion: number;
58
+ };
59
+ setPrintUrl: (request: LocalPrintUrlRequest) => string;
60
+ printPdf: (request: LocalPrintPdfRequest) => string;
61
+ }
@@ -34,6 +34,10 @@ export interface PrintRenderPage {
34
34
  elements: DesignerElement[];
35
35
  /** 当前打印页对应的根业务记录;同一记录拆分出的续页共享该数据。 */
36
36
  data?: Record<string, unknown>;
37
+ /** 当前业务记录内部的零基页号,用于多单据分别计算页码。 */
38
+ recordPageIndex?: number;
39
+ /** 当前业务记录拆分后的总页数。 */
40
+ recordPageTotal?: number;
37
41
  tableData: Record<string, TableRuntimeData>;
38
42
  }
39
43
  /** 业务系统发起一次 PDF 生成或预览的请求。 */
@@ -56,8 +60,8 @@ export interface PrintResult {
56
60
  blob?: Blob;
57
61
  };
58
62
  }
59
- /** 业务字段目前允许在标签中使用的三种展示方式。 */
60
- export type FieldElementType = 'text' | 'textBox' | 'qrcode' | 'barcode';
63
+ /** 业务字段允许在标签中使用的初始展示方式。 */
64
+ export type FieldElementType = 'text' | 'textBox' | 'qrcode' | 'barcode' | 'pageNumber' | 'pdf';
61
65
  /**
62
66
  * 由业务系统传给设计器的字段定义。
63
67
  *
@@ -68,6 +72,10 @@ export interface BusinessField {
68
72
  field: string;
69
73
  label: string;
70
74
  type: FieldElementType;
75
+ /** 页码字段未单独指定当前页路径时,默认使用 field。 */
76
+ currentPageField?: string;
77
+ /** type 为 pageNumber 时,从当前业务记录读取总页数的字段路径。 */
78
+ totalPageField?: string;
71
79
  }
72
80
  /** 左侧字段列表的分组;不同业务系统可以传入完全不同的分组和字段。 */
73
81
  export interface BusinessFieldGroup {
@@ -93,7 +101,7 @@ export interface PrintDesignerTheme {
93
101
  */
94
102
  export type DesignerElementType = 'text' | 'textBox' | 'rectangle' | 'horizontalLine' | 'verticalLine' | 'qrcode' | 'barcode' | 'image' | 'serialNumber' | 'pageNumber' | 'time' | 'table' | 'pdf';
95
103
  /** 右侧属性面板中可独立组合的属性编辑器标识。 */
96
- export type ElementPropertyKey = 'displayType' | 'content' | 'textHorizontalAlign' | 'textVerticalAlign' | 'textBoxFitMode' | 'textBoxOverflowMode' | 'lineHeight' | 'minFontSize' | 'imageSource' | 'x' | 'y' | 'width' | 'height' | 'fontSize' | 'fontWeight' | 'strokeWidth' | 'rotation' | 'timeMode' | 'includeTime' | 'fixedTime' | 'tableConfig' | 'pdfSource' | 'pdfCropMode' | 'pdfCropRatio';
104
+ export type ElementPropertyKey = 'displayType' | 'content' | 'textHorizontalAlign' | 'textVerticalAlign' | 'textBoxFitMode' | 'textBoxOverflowMode' | 'lineHeight' | 'minFontSize' | 'imageSource' | 'x' | 'y' | 'width' | 'height' | 'fontSize' | 'fontWeight' | 'strokeWidth' | 'rotation' | 'timeMode' | 'includeTime' | 'fixedTime' | 'pageNumberConfig' | 'tableConfig' | 'pdfSource' | 'pdfCropMode' | 'pdfCropRatio';
97
105
  /**
98
106
  * 单个属性编辑器的能力与数值约束。
99
107
  * 数值范围跟随组件类型配置,输入框和 SVG 控制点共用,避免两处规则不一致。
@@ -149,9 +157,21 @@ export interface DynamicTextBaseDesignerElement extends BaseDesignerElement {
149
157
  export interface SerialNumberDesignerElement extends DynamicTextBaseDesignerElement {
150
158
  type: 'serialNumber';
151
159
  }
152
- /** 页码在打印阶段根据数组下标和总长度生成“当前页/总页数”。 */
160
+ /** 页码既可使用整个打印任务的全局页码,也可从当前业务记录读取。 */
161
+ export type PageNumberMode = 'global' | 'field';
162
+ export interface PageNumberConfigValue {
163
+ mode: PageNumberMode;
164
+ currentField: string;
165
+ totalField: string;
166
+ }
153
167
  export interface PageNumberDesignerElement extends DynamicTextBaseDesignerElement {
154
168
  type: 'pageNumber';
169
+ /** 旧模板缺少时按全局页码处理。 */
170
+ pageNumberMode?: PageNumberMode;
171
+ /** 字段模式下,从当前业务记录读取当前页。支持点路径。 */
172
+ currentPageField?: string;
173
+ /** 字段模式下,从当前业务记录读取总页数。支持点路径。 */
174
+ totalPageField?: string;
155
175
  }
156
176
  export type TimeMode = 'realtime' | 'fixed';
157
177
  /** 时间可在打印时实时生成,也可保存用户选择的固定日期时间。 */
@@ -186,6 +206,8 @@ export type PdfCropMode = 'none' | 'currentSize' | 'scale' | 'cropWidth' | 'crop
186
206
  /** PDF 元素显示指定文件中的一页,打印前会将该页渲染进模板 SVG。 */
187
207
  export interface PdfDesignerElement extends BaseDesignerElement {
188
208
  type: 'pdf';
209
+ /** 存在 field 时,打印阶段从当前业务记录读取 PDF 地址。 */
210
+ field?: string;
189
211
  /** 支持同源/CORS 地址、Blob URL 或 data URL。 */
190
212
  content: string;
191
213
  cropMode: PdfCropMode;
@@ -328,7 +350,7 @@ export interface ElementClipboardItem {
328
350
  element: DesignerElement;
329
351
  }
330
352
  /** 独立属性编辑器使用的统一值类型。 */
331
- export type ElementPropertyValue = string | number | boolean | TableConfigValue;
353
+ export type ElementPropertyValue = string | number | boolean | PageNumberConfigValue | TableConfigValue;
332
354
  /** SVG 选区八个缩放控制点的稳定标识。 */
333
355
  export type ResizeHandle = 'topLeft' | 'top' | 'topRight' | 'right' | 'bottomRight' | 'bottom' | 'bottomLeft' | 'left';
334
356
  /**