@jcyao/print-sdk 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 joke_yao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # @jcyao/print-sdk
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@jcyao/print-sdk.svg)](https://www.npmjs.com/package/@jcyao/print-sdk)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ 通用打印 SDK - 客户端打印解决方案
7
+
8
+ ## ✨ 特性
9
+
10
+ - 🎨 **可视化模板设计** - 拖拽式设计打印模板
11
+ - 📄 **多组件支持** - 文本、表格、图片、二维码、条形码等
12
+ - 🔄 **数据绑定** - Schema 驱动的数据绑定系统
13
+ - 📊 **表格高级功能** - 跨页分页、表头重复、表格合计
14
+ - 🔌 **插件化架构** - 易于扩展的渲染器和管道系统
15
+ - 💯 **TypeScript** - 完整的类型定义
16
+ - 🎯 **高精度计算** - 使用 decimal.js 保证数值精度
17
+
18
+ ## 📦 安装
19
+
20
+ ```bash
21
+ npm install @jcyao/print-sdk
22
+ ```
23
+
24
+ ## 🚀 快速开始
25
+
26
+ ```typescript
27
+ import { init, print } from '@jcyao/print-sdk';
28
+
29
+ // 初始化 SDK(全局执行一次)
30
+ init();
31
+
32
+ // 打印
33
+ print({
34
+ template: {
35
+ pageConfig: {
36
+ size: 'A4',
37
+ orientation: 'portrait',
38
+ marginMm: {
39
+ top: 10,
40
+ right: 10,
41
+ bottom: 10,
42
+ left: 10
43
+ }
44
+ },
45
+ components: [
46
+ {
47
+ id: 'text-1',
48
+ type: 'text',
49
+ layout: {
50
+ mode: 'absolute',
51
+ xMm: 20,
52
+ yMm: 20,
53
+ widthMm: 170,
54
+ heightMm: 10
55
+ },
56
+ binding: {
57
+ path: 'orderNo'
58
+ },
59
+ props: {
60
+ label: '订单号:'
61
+ }
62
+ }
63
+ ]
64
+ },
65
+ data: {
66
+ orderNo: 'SR202401',
67
+ // ... 更多数据
68
+ }
69
+ });
70
+ ```
71
+
72
+ ## 📖 API 文档
73
+
74
+ ### `init()`
75
+
76
+ 初始化打印 SDK,全局执行一次。
77
+
78
+ ```typescript
79
+ init();
80
+ ```
81
+
82
+ ### `print(options: PrintOptions)`
83
+
84
+ 执行打印操作。
85
+
86
+ **参数:**
87
+
88
+ ```typescript
89
+ interface PrintOptions {
90
+ template: Template; // 打印模板
91
+ data: any; // 数据对象
92
+ }
93
+ ```
94
+
95
+ ### `preview(options: PrintOptions): string`
96
+
97
+ 生成预览 HTML。
98
+
99
+ ```typescript
100
+ const html = preview({
101
+ template: myTemplate,
102
+ data: myData
103
+ });
104
+ ```
105
+
106
+ ## 🎨 支持的组件
107
+
108
+ - **文本组件** - 显示文本内容,支持标签和数据绑定
109
+ - **表格组件** - 数组数据表格化展示,支持跨页、表头重复、合计
110
+ - **图片组件** - 本地/远程图片、base64 编码
111
+ - **二维码组件** - 自动生成二维码
112
+ - **条形码组件** - 多种条形码格式
113
+ - **线条组件** - 实线/虚线装饰
114
+ - **矩形组件** - 边框装饰
115
+ - **页码组件** - 自动分页页码
116
+
117
+ ## 🔄 数据管道
118
+
119
+ 支持 6 种内置管道:
120
+
121
+ - **date** - 日期格式化 (`YYYY-MM-DD HH:mm:ss`)
122
+ - **currency** - 货币格式化 (`¥9999.00`)
123
+ - **money** - 金额转换(分↔元、千分位)
124
+ - **uppercase/lowercase** - 大小写转换
125
+ - **slice** - 字符串截取
126
+ - **default** - 默认值处理
127
+
128
+ **使用示例:**
129
+
130
+ ```typescript
131
+ {
132
+ binding: {
133
+ path: 'amount',
134
+ pipes: [
135
+ {
136
+ type: 'money',
137
+ options: {
138
+ mode: 'fenToYuan', // 分转元
139
+ precision: 2,
140
+ symbol: '¥',
141
+ separator: true // 千分位分隔
142
+ }
143
+ }
144
+ ]
145
+ }
146
+ }
147
+ ```
148
+
149
+ ## 📊 表格高级功能
150
+
151
+ ### 跨页分页
152
+
153
+ ```typescript
154
+ {
155
+ type: 'table',
156
+ props: {
157
+ columns: [...],
158
+ repeatHeader: true // 跨页重复表头
159
+ }
160
+ }
161
+ ```
162
+
163
+ ### 表格合计
164
+
165
+ ```typescript
166
+ {
167
+ type: 'table',
168
+ props: {
169
+ columns: [
170
+ {
171
+ title: '金额',
172
+ dataIndex: 'amount',
173
+ summary: {
174
+ type: 'sum', // sum, avg, max, min, count
175
+ precision: 2,
176
+ prefix: '¥'
177
+ }
178
+ }
179
+ ],
180
+ showSummary: true,
181
+ summaryMode: 'total', // total: 仅最后一页, page: 每页合计
182
+ summaryLabel: '合计'
183
+ }
184
+ }
185
+ ```
186
+
187
+ ## 🔧 类型定义
188
+
189
+ 完整的 TypeScript 类型定义:
190
+
191
+ ```typescript
192
+ import type {
193
+ Template,
194
+ ComponentNode,
195
+ TableColumn,
196
+ PipeConfig
197
+ } from '@jcyao/print-sdk';
198
+ ```
199
+
200
+ ## 📝 License
201
+
202
+ MIT © joke_yao
203
+
204
+ ## 🔗 相关链接
205
+
206
+ - [GitHub 仓库](https://github.com/joker-yjc/printer)
207
+ - [问题反馈](https://github.com/joker-yjc/printer/issues)
208
+ - [更新日志](https://github.com/joker-yjc/printer/blob/main/CHANGELOG.md)
209
+
210
+ ## 🤝 贡献
211
+
212
+ 欢迎贡献代码、报告问题或提出建议!
213
+
214
+ 1. Fork 本仓库
215
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
216
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
217
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
218
+ 5. 提交 Pull Request
@@ -0,0 +1,72 @@
1
+ /**
2
+ * 打印 SDK 核心类
3
+ * 提供完整的打印功能封装
4
+ * 解耦设计:直接接收模板数据,不依赖模板服务
5
+ */
6
+ import type { PrintTemplate } from './types';
7
+ /**
8
+ * 打印选项
9
+ */
10
+ export interface PrintOptions {
11
+ template: PrintTemplate;
12
+ data: any;
13
+ preview?: boolean;
14
+ }
15
+ /**
16
+ * 批量打印选项(同模板多数据)
17
+ */
18
+ export interface BatchPrintOptions {
19
+ preview?: boolean;
20
+ onProgress?: (progress: BatchPrintProgress) => void;
21
+ }
22
+ /**
23
+ * 批量打印进度
24
+ */
25
+ export interface BatchPrintProgress {
26
+ total: number;
27
+ completed: number;
28
+ failed: number;
29
+ currentIndex: number;
30
+ }
31
+ export declare class PrintSDK {
32
+ /**
33
+ * 打印
34
+ * @param options 打印选项
35
+ * @param options.template 模板数据
36
+ * @param options.data 数据对象
37
+ * @param options.preview 是否预览(默认 false)
38
+ */
39
+ print(options: PrintOptions): Promise<void>;
40
+ /**
41
+ * 快捷打印(不预览)
42
+ * @param template 模板数据
43
+ * @param data 数据对象
44
+ */
45
+ printDirect(template: PrintTemplate, data: any): Promise<void>;
46
+ /**
47
+ * 预览后打印
48
+ * @param template 模板数据
49
+ * @param data 数据对象
50
+ */
51
+ printWithPreview(template: PrintTemplate, data: any): Promise<void>;
52
+ /**
53
+ * 仅生成 HTML(不打印)
54
+ * @param template 模板数据
55
+ * @param data 数据对象
56
+ * @returns HTML 字符串
57
+ */
58
+ generateHTML(template: PrintTemplate, data: any): Promise<string>;
59
+ /**
60
+ * 批量打印(同模板多数据)
61
+ * 生成包含所有数据的完整打印文档,只需要用户确认一次打印
62
+ * @param template 模板数据
63
+ * @param dataList 数据列表
64
+ * @param options 批量打印选项
65
+ */
66
+ printMultiple(template: PrintTemplate, dataList: any[], options?: BatchPrintOptions): Promise<void>;
67
+ }
68
+ /**
69
+ * 创建 SDK 实例(无需配置)
70
+ * @returns PrintSDK 实例
71
+ */
72
+ export declare function createPrintSDK(): PrintSDK;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 打印SDK入口文件
3
+ * 导出所有公共API
4
+ *
5
+ * SDK 设计原则:
6
+ * - 完全解耦:不依赖任何外部服务
7
+ * - 数据驱动:直接接收模板和数据
8
+ * - 无状态:无需初始化和配置
9
+ */
10
+ export * from './sdk';
11
+ export * from './pipes';