@oiij/js-pdf 0.0.11 → 0.0.13

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.
Files changed (3) hide show
  1. package/README.md +269 -51
  2. package/dist/index.mjs +1 -4
  3. package/package.json +7 -7
package/README.md CHANGED
@@ -1,90 +1,308 @@
1
- # Use JS-PDF 🚀
1
+ # @oiij/js-pdf
2
2
 
3
3
  [![NPM version](https://img.shields.io/npm/v/@oiij/js-pdf)](https://www.npmjs.com/package/@oiij/js-pdf)
4
4
  [![MIT-license](https://img.shields.io/npm/l/@oiij/js-pdf)](https://github.com/oiij/use/blob/main/packages/js-pdf/LICENSE)
5
5
 
6
- ## 项目简介 📦
6
+ ## 简介
7
7
 
8
- Use JS-PDF 是基于 jsPDF Vue 3 封装,提供便捷的 PDF 生成功能,帮助开发者在浏览器端快速生成和下载 PDF 文档。
8
+ Use JS-PDF 是基于 jsPDF pdfjs-dist 的工具库,提供 PDF 生成、读取、转换等功能,帮助开发者在浏览器端处理 PDF 文件。
9
9
 
10
- ## 功能特点 ✨
10
+ ## 特点
11
11
 
12
- ### PDF 生成 📄
12
+ ### 📄 PDF 生成
13
13
 
14
- - 📝 支持文本、图片等多种内容
15
- - 🎨 提供丰富的样式配置
16
- - 💾 直接在浏览器端生成 PDF
14
+ - 📝 支持文本、图片、圆形、线条等元素
15
+ - 🖼️ 支持异步图片加载
16
+ - 🎨 支持自定义样式配置
17
17
 
18
- ### 模块化设计 🧩
18
+ ### 📖 PDF 读取
19
19
 
20
- - 📁 采用模块化架构,组件独立封装
21
- - 📦 支持按需导入,减小打包体积
22
- - 🔧 清晰的文件结构,易于维护和扩展
20
+ - 🔗 支持从 URL 或 File 对象读取 PDF
21
+ - 🖥️ 支持渲染 PDF 为 Canvas
22
+ - 📑 支持多页 PDF 处理
23
23
 
24
- ### 类型安全 🔒
24
+ ### 📦 导出功能
25
+
26
+ - 📥 支持将 Canvas 导出为 PDF
27
+ - 📦 支持将 Canvas 打包为 ZIP
28
+
29
+ ### 🔒 类型安全
25
30
 
26
31
  - 📝 完整的 TypeScript 类型定义
27
32
  - 💡 提供准确的类型推断和代码提示
28
- - 🎯 支持 Vue 3 的 Composition API 类型系统
29
33
 
30
- ### 轻量高效 ⚡
34
+ ## 安装
31
35
 
32
- - 🚀 核心代码精简,无额外依赖
33
- - 🏃 优化的性能表现,最小化运行时开销
34
- - 📦 支持 Tree Shaking,进一步减小打包体积
36
+ ```bash
37
+ # 使用 pnpm
38
+ pnpm add @oiij/js-pdf
35
39
 
36
- ## 安装 📥
40
+ # 使用 npm
41
+ npm install @oiij/js-pdf
37
42
 
38
- ### 使用 pnpm 🐱
43
+ # 使用 yarn
44
+ yarn add @oiij/js-pdf
45
+ ```
39
46
 
40
- ```bash
41
- pnpm add @oiij/js-pdf
47
+ ## 依赖
48
+
49
+ - `jspdf`: ^2.0.0
50
+ - `pdfjs-dist`: ^4.0.0
51
+ - `file-saver`: ^2.0.0
52
+ - `jszip`: ^3.0.0
53
+ - `nanoid`: ^5.0.0
54
+
55
+ ## 示例
56
+
57
+ ### 生成 PDF
58
+
59
+ ```ts
60
+ import { generatePDF } from '@oiij/js-pdf'
61
+
62
+ const { pdf } = await generatePDF([
63
+ {
64
+ type: 'text',
65
+ text: 'Hello World',
66
+ x: 10,
67
+ y: 10,
68
+ fontSize: 20
69
+ },
70
+ {
71
+ type: 'image',
72
+ imageData: 'data:image/png;base64,...',
73
+ x: 10,
74
+ y: 30,
75
+ width: 100,
76
+ height: 100
77
+ },
78
+ {
79
+ type: 'circle',
80
+ x: 50,
81
+ y: 80,
82
+ r: 20,
83
+ fillColor: 'red'
84
+ }
85
+ ])
86
+
87
+ pdf.save('example.pdf')
42
88
  ```
43
89
 
44
- ### 使用 npm 📦
90
+ ### 读取 PDF
45
91
 
46
- ```bash
47
- npm install @oiij/js-pdf
48
- ```
92
+ ```ts
93
+ import { openPdf } from '@oiij/js-pdf'
49
94
 
50
- ### 使用 yarn 🧶
95
+ // URL 读取
96
+ const result = await openPdf('https://example.com/file.pdf')
51
97
 
52
- ```bash
53
- yarn add @oiij/js-pdf
98
+ // 从 File 读取
99
+ const file = document.querySelector('input[type="file"]').files[0]
100
+ const result = await openPdf(file)
101
+
102
+ // result.pdf - PDF 文档
103
+ // result.pages - 页面数组
104
+ // result.canvases - Canvas 数组
54
105
  ```
55
106
 
56
- ## 快速开始 🌟
107
+ ### Canvas 转 PDF
108
+
109
+ ```ts
110
+ import { canvas2Pdf, openPdf } from '@oiij/js-pdf'
57
111
 
58
- ### 基础使用
112
+ const { canvases } = await openPdf('file.pdf')
113
+ canvas2Pdf(canvases, 'output.pdf')
114
+ ```
115
+
116
+ ### Canvas 转 ZIP
59
117
 
60
- ```vue
61
- <script setup>
62
- import { useJsPdf } from '@oiij/js-pdf'
63
- </script>
118
+ ```ts
119
+ import { canvas2Zip, openPdf } from '@oiij/js-pdf'
64
120
 
65
- <template>
66
- <div />
67
- </template>
121
+ const { canvases } = await openPdf('file.pdf')
122
+ await canvas2Zip(canvases, 'output')
68
123
  ```
69
124
 
70
- ## 在线文档 📚
125
+ ## API
126
+
127
+ ### `generatePDF(data, options?, globalStyle?)`
128
+
129
+ 生成 PDF 文档。
130
+
131
+ #### 参数
132
+
133
+ | 参数 | 类型 | 说明 |
134
+ | ------------- | -------------- | ---------------- |
135
+ | `data` | `PDFDataRow[]` | PDF 数据行数组 |
136
+ | `options` | `jsPDFOptions` | jsPDF 初始化选项 |
137
+ | `globalStyle` | `PDFStyle` | 全局样式 |
138
+
139
+ #### 返回值
140
+
141
+ | 属性 | 类型 | 说明 |
142
+ | ----- | ------- | ---------- |
143
+ | `pdf` | `jsPDF` | jsPDF 实例 |
144
+
145
+ ### `openPdf(url?)`
71
146
 
72
- [在线文档](https://oiij-use.vercel.app/js-pdf/js-pdf) 📖
147
+ 打开并读取 PDF 文件。
73
148
 
74
- ## 贡献指南 🤝
149
+ #### 参数
75
150
 
76
- 欢迎贡献代码、报告问题或提出新功能建议!
151
+ | 参数 | 类型 | 说明 |
152
+ | ----- | ----------------------- | --------------------------- |
153
+ | `url` | `string \| URL \| File` | PDF 文件的 URL 或 File 对象 |
77
154
 
78
- 1. Fork 本仓库 🍴
79
- 2. 创建您的特性分支 (`git checkout -b feature/amazing-feature`) 🌿
80
- 3. 提交您的更改 (`git commit -m 'Add some amazing feature'`) 💾
81
- 4. 推送到分支 (`git push origin feature/amazing-feature`) 🚀
82
- 5. 打开一个 Pull Request 📥
155
+ #### 返回值
83
156
 
84
- ## 许可证 📄
157
+ | 属性 | 类型 | 说明 |
158
+ | ---------- | --------------------- | ------------ |
159
+ | `pdf` | `PDFDocumentProxy` | PDF 文档对象 |
160
+ | `pages` | `PDFPageProxy[]` | 页面数组 |
161
+ | `id` | `string` | 唯一 ID |
162
+ | `canvases` | `HTMLCanvasElement[]` | Canvas 数组 |
85
163
 
86
- 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情 📑
164
+ ### `canvas2Pdf(canvases, fileName)`
165
+
166
+ 将 Canvas 数组转换为 PDF 并下载。
167
+
168
+ #### 参数
169
+
170
+ | 参数 | 类型 | 说明 |
171
+ | ---------- | --------------------- | ----------- |
172
+ | `canvases` | `HTMLCanvasElement[]` | Canvas 数组 |
173
+ | `fileName` | `string` | 文件名 |
174
+
175
+ ### `canvas2Zip(canvases, fileName)`
176
+
177
+ 将 Canvas 数组转换为 ZIP 并下载。
178
+
179
+ #### 参数
180
+
181
+ | 参数 | 类型 | 说明 |
182
+ | ---------- | --------------------- | ----------- |
183
+ | `canvases` | `HTMLCanvasElement[]` | Canvas 数组 |
184
+ | `fileName` | `string` | 文件名 |
185
+
186
+ ### `readPdfFile(buffer)`
187
+
188
+ 从 ArrayBuffer 读取 PDF 文件。
189
+
190
+ #### 参数
191
+
192
+ | 参数 | 类型 | 说明 |
193
+ | -------- | ------------- | ---------------------- |
194
+ | `buffer` | `ArrayBuffer` | PDF 文件的 ArrayBuffer |
195
+
196
+ ## PDFDataRow 类型
197
+
198
+ ### text 类型
199
+
200
+ ```ts
201
+ type TextDataRow = {
202
+ type: 'text'
203
+ text: string
204
+ x: number
205
+ y: number
206
+ fontSize?: number
207
+ fontName?: string
208
+ fontStyle?: string
209
+ fontWeight?: string
210
+ textColor?: Color
211
+ align?: string
212
+ baseline?: string
213
+ }
214
+ ```
215
+
216
+ ### image 类型
217
+
218
+ ```ts
219
+ type ImageDataRow = {
220
+ type: 'image'
221
+ imageData: string | HTMLImageElement | (() => Promise<HTMLImageElement>)
222
+ x: number
223
+ y: number
224
+ width: number
225
+ height: number
226
+ alias?: string
227
+ compression?: string
228
+ rotation?: number
229
+ }
230
+ ```
231
+
232
+ ### circle 类型
233
+
234
+ ```ts
235
+ type CircleDataRow = {
236
+ type: 'circle'
237
+ x: number
238
+ y: number
239
+ r: number
240
+ fillColor?: Color
241
+ drawColor?: Color
242
+ lineWidth?: number
243
+ style?: 'S' | 'F' | 'FD' | 'DF'
244
+ }
245
+ ```
246
+
247
+ ### line 类型
248
+
249
+ ```ts
250
+ type LineDataRow = {
251
+ type: 'line'
252
+ x1: number
253
+ y1: number
254
+ x2: number
255
+ y2: number
256
+ drawColor?: Color
257
+ lineWidth?: number
258
+ style?: 'S' | 'F' | 'FD' | 'DF'
259
+ }
260
+ ```
261
+
262
+ ### lines 类型
263
+
264
+ ```ts
265
+ type LinesDataRow = {
266
+ type: 'lines'
267
+ lines: number[][]
268
+ x: number
269
+ y: number
270
+ scale?: [number, number]
271
+ closed?: boolean
272
+ drawColor?: Color
273
+ lineWidth?: number
274
+ style?: 'S' | 'F' | 'FD' | 'DF'
275
+ }
276
+ ```
277
+
278
+ ## 类型定义
279
+
280
+ ```ts
281
+ type Color = string | [number, number, number, number]
282
+
283
+ type PDFStyle = {
284
+ fontSize?: number
285
+ fontName?: string
286
+ fontStyle?: string
287
+ fontWeight?: string
288
+ textColor?: Color
289
+ drawColor?: Color
290
+ fillColor?: Color
291
+ lineWidth?: number
292
+ }
293
+
294
+ type PDFDataRow = {
295
+ type: 'text' | 'image' | 'circle' | 'line' | 'lines'
296
+ // ... 具体选项根据 type 不同
297
+ }
298
+
299
+ export declare function generatePDF(data: PDFDataRow[], options?: jsPDFOptions, globalStyle?: PDFStyle): Promise<{ pdf: jsPDF }>
300
+ export declare function openPdf(url?: string | URL | File): Promise<{ pdf: PDFDocumentProxy, pages: PDFPageProxy[], id: string, canvases: HTMLCanvasElement[] }>
301
+ export declare function canvas2Pdf(canvases: HTMLCanvasElement[], fileName: string): void
302
+ export declare function canvas2Zip(canvases: HTMLCanvasElement[], fileName: string): Promise<unknown>
303
+ export declare function readPdfFile(buffer: ArrayBuffer): Promise<{ pdf: PDFDocumentProxy, pages: PDFPageProxy[], id: string, canvases: HTMLCanvasElement[] }>
304
+ ```
87
305
 
88
- ## 联系方式 📞
306
+ ## 在线文档
89
307
 
90
- - GitHub: [https://github.com/Eiog/@oiij/js-pdf](https://github.com/Eiog/@oiij/js-pdf) 🌟
308
+ [在线文档](https://oiij-use.vercel.app/js-pdf/js-pdf)
package/dist/index.mjs CHANGED
@@ -3,7 +3,6 @@ import { saveAs } from "file-saver";
3
3
  import JsZip from "jszip";
4
4
  import { nanoid } from "nanoid";
5
5
  import { GlobalWorkerOptions, getDocument, version } from "pdfjs-dist";
6
-
7
6
  //#region src/utils.ts
8
7
  GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${version}/build/pdf.worker.min.mjs`;
9
8
  function file2Buffer(file) {
@@ -199,7 +198,6 @@ async function readPdfFile(buffer) {
199
198
  canvases
200
199
  };
201
200
  }
202
-
203
201
  //#endregion
204
202
  //#region src/index.ts
205
203
  function setDrawStyle(pdf, data) {
@@ -301,6 +299,5 @@ async function generatePDF(data, options, globalStyle) {
301
299
  }
302
300
  return { pdf };
303
301
  }
304
-
305
302
  //#endregion
306
- export { canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
303
+ export { canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oiij/js-pdf",
3
3
  "type": "module",
4
- "version": "0.0.11",
4
+ "version": "0.0.13",
5
5
  "description": "A simple PDF library for JavaScript",
6
6
  "author": "oiij",
7
7
  "license": "MIT",
@@ -28,18 +28,18 @@
28
28
  "peerDependencies": {
29
29
  "@types/file-saver": "^2.0.7",
30
30
  "file-saver": "^2.0.5",
31
- "jspdf": "^4.0.0",
31
+ "jspdf": "^4.2.1",
32
32
  "jszip": "^3.10.1",
33
- "nanoid": "^5.1.6",
34
- "pdfjs-dist": "^5.4.530"
33
+ "nanoid": "^5.1.7",
34
+ "pdfjs-dist": "^5.5.207"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/file-saver": "^2.0.7",
38
38
  "file-saver": "^2.0.5",
39
- "jspdf": "^4.0.0",
39
+ "jspdf": "^4.2.1",
40
40
  "jszip": "^3.10.1",
41
- "nanoid": "^5.1.6",
42
- "pdfjs-dist": "^5.4.530"
41
+ "nanoid": "^5.1.7",
42
+ "pdfjs-dist": "^5.5.207"
43
43
  },
44
44
  "publishConfig": {
45
45
  "access": "public"