@borurio-print/print-designer 1.0.14 → 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 +168 -1
- package/dist-lib/PrintPlugin.vue.d.ts +46 -0
- package/dist-lib/core/dynamicText.d.ts +4 -0
- package/dist-lib/core/localPrintTypes.d.ts +61 -0
- package/dist-lib/core/types.d.ts +7 -1
- package/dist-lib/{index-B3WBSRxd.js → index-HlbmrwAc.js} +5069 -4675
- package/dist-lib/index.d.ts +3 -0
- package/dist-lib/{index.es-CKIMzdzw.js → index.es-BORqmi60.js} +2 -2
- package/dist-lib/index.js +7 -6
- package/dist-lib/{pdfGenerator-CkOErC34.js → pdfGenerator-DdAc3h0P.js} +1 -1
- package/dist-lib/style.css +1 -1
- package/dist-lib/views/print-designer/components/PdfSvgElement.vue.d.ts +2 -0
- package/dist-lib/views/print-designer/components/SvgElementsRenderer.vue.d.ts +3 -0
- package/package.json +1 -1
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,10 @@ export interface DynamicTextContext {
|
|
|
5
5
|
index: number;
|
|
6
6
|
/** 本次打印数据数组的总长度。 */
|
|
7
7
|
total: number;
|
|
8
|
+
/** 当前业务记录内的零基页号;未传时沿用整个任务页号。 */
|
|
9
|
+
recordPageIndex?: number;
|
|
10
|
+
/** 当前业务记录的总页数;未传时沿用整个任务总页数。 */
|
|
11
|
+
recordPageTotal?: number;
|
|
8
12
|
/** 当前打印页对应的业务记录。 */
|
|
9
13
|
data?: Record<string, unknown>;
|
|
10
14
|
/** 整个打印批次共用的时间,避免多页之间出现秒数差异。 */
|
|
@@ -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
|
+
}
|
package/dist-lib/core/types.d.ts
CHANGED
|
@@ -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 生成或预览的请求。 */
|
|
@@ -57,7 +61,7 @@ export interface PrintResult {
|
|
|
57
61
|
};
|
|
58
62
|
}
|
|
59
63
|
/** 业务字段允许在标签中使用的初始展示方式。 */
|
|
60
|
-
export type FieldElementType = 'text' | 'textBox' | 'qrcode' | 'barcode' | 'pageNumber';
|
|
64
|
+
export type FieldElementType = 'text' | 'textBox' | 'qrcode' | 'barcode' | 'pageNumber' | 'pdf';
|
|
61
65
|
/**
|
|
62
66
|
* 由业务系统传给设计器的字段定义。
|
|
63
67
|
*
|
|
@@ -202,6 +206,8 @@ export type PdfCropMode = 'none' | 'currentSize' | 'scale' | 'cropWidth' | 'crop
|
|
|
202
206
|
/** PDF 元素显示指定文件中的一页,打印前会将该页渲染进模板 SVG。 */
|
|
203
207
|
export interface PdfDesignerElement extends BaseDesignerElement {
|
|
204
208
|
type: 'pdf';
|
|
209
|
+
/** 存在 field 时,打印阶段从当前业务记录读取 PDF 地址。 */
|
|
210
|
+
field?: string;
|
|
205
211
|
/** 支持同源/CORS 地址、Blob URL 或 data URL。 */
|
|
206
212
|
content: string;
|
|
207
213
|
cropMode: PdfCropMode;
|