@oiij/js-pdf 0.0.12 → 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.
- package/README.md +269 -51
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,90 +1,308 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @oiij/js-pdf
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@oiij/js-pdf)
|
|
4
4
|
[](https://github.com/oiij/use/blob/main/packages/js-pdf/LICENSE)
|
|
5
5
|
|
|
6
|
-
##
|
|
6
|
+
## 简介
|
|
7
7
|
|
|
8
|
-
Use JS-PDF 是基于 jsPDF
|
|
8
|
+
Use JS-PDF 是基于 jsPDF 和 pdfjs-dist 的工具库,提供 PDF 生成、读取、转换等功能,帮助开发者在浏览器端处理 PDF 文件。
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## 特点
|
|
11
11
|
|
|
12
|
-
### PDF 生成
|
|
12
|
+
### 📄 PDF 生成
|
|
13
13
|
|
|
14
|
-
- 📝
|
|
15
|
-
-
|
|
16
|
-
-
|
|
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
|
-
|
|
36
|
+
```bash
|
|
37
|
+
# 使用 pnpm
|
|
38
|
+
pnpm add @oiij/js-pdf
|
|
35
39
|
|
|
36
|
-
|
|
40
|
+
# 使用 npm
|
|
41
|
+
npm install @oiij/js-pdf
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
# 使用 yarn
|
|
44
|
+
yarn add @oiij/js-pdf
|
|
45
|
+
```
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
###
|
|
90
|
+
### 读取 PDF
|
|
45
91
|
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
```
|
|
92
|
+
```ts
|
|
93
|
+
import { openPdf } from '@oiij/js-pdf'
|
|
49
94
|
|
|
50
|
-
|
|
95
|
+
// 从 URL 读取
|
|
96
|
+
const result = await openPdf('https://example.com/file.pdf')
|
|
51
97
|
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
```
|
|
61
|
-
|
|
62
|
-
import { useJsPdf } from '@oiij/js-pdf'
|
|
63
|
-
</script>
|
|
118
|
+
```ts
|
|
119
|
+
import { canvas2Zip, openPdf } from '@oiij/js-pdf'
|
|
64
120
|
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
147
|
+
打开并读取 PDF 文件。
|
|
73
148
|
|
|
74
|
-
|
|
149
|
+
#### 参数
|
|
75
150
|
|
|
76
|
-
|
|
151
|
+
| 参数 | 类型 | 说明 |
|
|
152
|
+
| ----- | ----------------------- | --------------------------- |
|
|
153
|
+
| `url` | `string \| URL \| File` | PDF 文件的 URL 或 File 对象 |
|
|
77
154
|
|
|
78
|
-
|
|
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
|
-
|
|
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
|
-
|
|
308
|
+
[在线文档](https://oiij-use.vercel.app/js-pdf/js-pdf)
|