@oiij/js-pdf 0.0.1 → 0.0.3
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 +5 -18
- package/dist/index.cjs +100 -12
- package/dist/index.d.cts +63 -10
- package/dist/index.d.ts +63 -10
- package/dist/index.js +96 -9
- package/package.json +13 -51
package/README.md
CHANGED
|
@@ -1,25 +1,12 @@
|
|
|
1
|
-
# JS-PDF
|
|
1
|
+
# Use JS-PDF
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@oiij/js-pdf)
|
|
4
|
+
[](https://github.com/Eiog/@oiij/js-pdf/blob/main/LICENSE)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
- Test with [vitest](https://vitest.dev)
|
|
7
|
-
|
|
8
|
-
# Usage
|
|
9
|
-
|
|
10
|
-
### 安装
|
|
6
|
+
## Usage
|
|
11
7
|
|
|
12
8
|
```bash
|
|
13
9
|
pnpm add @oiij/js-pdf
|
|
14
10
|
```
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
import { openPdf } from '@oiij/js-pdf'
|
|
20
|
-
const { pdf } = await openPdf()
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## License
|
|
24
|
-
|
|
25
|
-
MIT
|
|
12
|
+
[在线文档](https://oiij-use.vercel.app/examples/js-pdf/started)
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -22,13 +21,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
21
|
}) : target, mod));
|
|
23
22
|
|
|
24
23
|
//#endregion
|
|
25
|
-
const file_saver = __toESM(require("file-saver"));
|
|
26
24
|
const jspdf = __toESM(require("jspdf"));
|
|
25
|
+
const file_saver = __toESM(require("file-saver"));
|
|
27
26
|
const jszip = __toESM(require("jszip"));
|
|
28
27
|
const nanoid = __toESM(require("nanoid"));
|
|
29
28
|
const pdfjs_dist = __toESM(require("pdfjs-dist"));
|
|
30
29
|
|
|
31
|
-
//#region src/
|
|
30
|
+
//#region src/utils.ts
|
|
32
31
|
pdfjs_dist.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs_dist.version}/build/pdf.worker.min.mjs`;
|
|
33
32
|
function file2Buffer(file) {
|
|
34
33
|
return new Promise((resolve, reject) => {
|
|
@@ -63,6 +62,7 @@ function page2Canvas(page, id) {
|
|
|
63
62
|
canvas.style.height = `${height / pixelRatio}px`;
|
|
64
63
|
page.render({
|
|
65
64
|
canvasContext: ctx,
|
|
65
|
+
canvas,
|
|
66
66
|
viewport
|
|
67
67
|
}).promise.then(() => resolve(canvas)).catch((e) => reject(e));
|
|
68
68
|
});
|
|
@@ -84,7 +84,7 @@ async function pdf2Canvases(pdf) {
|
|
|
84
84
|
}
|
|
85
85
|
async function openPdf(url) {
|
|
86
86
|
try {
|
|
87
|
-
if (url) {
|
|
87
|
+
if (url && (typeof url === "string" || url instanceof URL)) {
|
|
88
88
|
const pdf = await (0, pdfjs_dist.getDocument)(url).promise;
|
|
89
89
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
90
90
|
return {
|
|
@@ -93,10 +93,9 @@ async function openPdf(url) {
|
|
|
93
93
|
id,
|
|
94
94
|
canvases
|
|
95
95
|
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
const buffer = await file2Buffer(file);
|
|
96
|
+
}
|
|
97
|
+
if (url instanceof File) {
|
|
98
|
+
const buffer = await file2Buffer(url);
|
|
100
99
|
const pdf = await (0, pdfjs_dist.getDocument)(buffer).promise;
|
|
101
100
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
102
101
|
return {
|
|
@@ -116,7 +115,7 @@ function canvas2Blob(canvas) {
|
|
|
116
115
|
try {
|
|
117
116
|
canvas.toBlob((blob) => {
|
|
118
117
|
if (blob) return resolve(blob);
|
|
119
|
-
return reject(new Error("canvas to blob error"));
|
|
118
|
+
return reject(/* @__PURE__ */ new Error("canvas to blob error"));
|
|
120
119
|
});
|
|
121
120
|
} catch (error) {
|
|
122
121
|
return reject(error);
|
|
@@ -153,8 +152,97 @@ function canvas2Zip(canvases, fileName) {
|
|
|
153
152
|
}).catch((error) => reject(error));
|
|
154
153
|
});
|
|
155
154
|
}
|
|
155
|
+
async function readPdfFile(buffer) {
|
|
156
|
+
const pdf = await (0, pdfjs_dist.getDocument)(buffer).promise;
|
|
157
|
+
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
158
|
+
return {
|
|
159
|
+
pdf,
|
|
160
|
+
pages,
|
|
161
|
+
id,
|
|
162
|
+
canvases
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/index.ts
|
|
168
|
+
function setDrawStyle(pdf, data) {
|
|
169
|
+
const { drawColor, fillColor, lineWidth } = data;
|
|
170
|
+
if (typeof fillColor === "string") pdf.setFillColor(fillColor);
|
|
171
|
+
else pdf.setFillColor(...fillColor);
|
|
172
|
+
if (typeof drawColor === "string") pdf.setDrawColor(drawColor);
|
|
173
|
+
else pdf.setDrawColor(...drawColor);
|
|
174
|
+
pdf.setLineWidth(lineWidth);
|
|
175
|
+
}
|
|
176
|
+
async function generatePDF(data, options, globalStyle) {
|
|
177
|
+
const pdf = new jspdf.jsPDF(options);
|
|
178
|
+
const font = pdf.getFont();
|
|
179
|
+
const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
|
|
180
|
+
const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
|
|
181
|
+
function setText(data$1) {
|
|
182
|
+
const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
|
|
183
|
+
pdf.setFontSize(fontSize);
|
|
184
|
+
pdf.setFont(fontName, fontStyle, fontWeight);
|
|
185
|
+
pdf.text(text, x, y, textOpt);
|
|
186
|
+
}
|
|
187
|
+
async function setImage(data$1) {
|
|
188
|
+
const { imageData, x, y, width, height, alias, compression, rotation } = data$1;
|
|
189
|
+
const _imageData = typeof imageData === "function" ? await Promise.try(imageData) : imageData;
|
|
190
|
+
pdf.addImage(_imageData, x, y, width, height, alias, compression, rotation);
|
|
191
|
+
}
|
|
192
|
+
function setCircle(data$1) {
|
|
193
|
+
const { x, y, r } = data$1;
|
|
194
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
195
|
+
setDrawStyle(pdf, {
|
|
196
|
+
fillColor,
|
|
197
|
+
drawColor,
|
|
198
|
+
lineWidth
|
|
199
|
+
});
|
|
200
|
+
pdf.circle(x, y, r, style);
|
|
201
|
+
}
|
|
202
|
+
function setLine(data$1) {
|
|
203
|
+
const { x1, y1, x2, y2 } = data$1;
|
|
204
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
205
|
+
setDrawStyle(pdf, {
|
|
206
|
+
fillColor,
|
|
207
|
+
drawColor,
|
|
208
|
+
lineWidth
|
|
209
|
+
});
|
|
210
|
+
pdf.line(x1, y1, x2, y2, style);
|
|
211
|
+
}
|
|
212
|
+
function setLines(data$1) {
|
|
213
|
+
const { lines, x, y, scale = [1, 1], closed = false } = data$1;
|
|
214
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
215
|
+
setDrawStyle(pdf, {
|
|
216
|
+
fillColor,
|
|
217
|
+
drawColor,
|
|
218
|
+
lineWidth
|
|
219
|
+
});
|
|
220
|
+
pdf.lines(lines, x, y, scale, style, closed);
|
|
221
|
+
}
|
|
222
|
+
for (const { type,...opt } of data) switch (type) {
|
|
223
|
+
case "text":
|
|
224
|
+
setText(opt);
|
|
225
|
+
break;
|
|
226
|
+
case "image":
|
|
227
|
+
await setImage(opt);
|
|
228
|
+
break;
|
|
229
|
+
case "circle":
|
|
230
|
+
setCircle(opt);
|
|
231
|
+
break;
|
|
232
|
+
case "line":
|
|
233
|
+
setLine(opt);
|
|
234
|
+
break;
|
|
235
|
+
case "lines":
|
|
236
|
+
setLines(opt);
|
|
237
|
+
break;
|
|
238
|
+
default: break;
|
|
239
|
+
}
|
|
240
|
+
return { pdf };
|
|
241
|
+
}
|
|
156
242
|
|
|
157
243
|
//#endregion
|
|
158
|
-
exports.canvas2Pdf = canvas2Pdf
|
|
159
|
-
exports.canvas2Zip = canvas2Zip
|
|
160
|
-
exports.
|
|
244
|
+
exports.canvas2Pdf = canvas2Pdf;
|
|
245
|
+
exports.canvas2Zip = canvas2Zip;
|
|
246
|
+
exports.generatePDF = generatePDF;
|
|
247
|
+
exports.openPdf = openPdf;
|
|
248
|
+
exports.readPdfFile = readPdfFile;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,14 +1,67 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTMLFontFace, ImageOptions, TextOptions, jsPDF, jsPDFOptions } from "jspdf";
|
|
2
|
+
import * as pdfjs_dist_types_src_display_api0 from "pdfjs-dist/types/src/display/api";
|
|
2
3
|
|
|
3
|
-
//#region src/
|
|
4
|
-
declare function openPdf(url?: string | URL): Promise<{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}>;
|
|
4
|
+
//#region src/utils.d.ts
|
|
5
|
+
declare function openPdf(url?: string | URL | File): Promise<{
|
|
6
|
+
pdf: pdfjs_dist_types_src_display_api0.PDFDocumentProxy;
|
|
7
|
+
pages: pdfjs_dist_types_src_display_api0.PDFPageProxy[];
|
|
8
|
+
id: string;
|
|
9
|
+
canvases: HTMLCanvasElement[];
|
|
10
|
+
} | undefined>;
|
|
10
11
|
declare function canvas2Pdf(canvases: HTMLCanvasElement[], fileName: string): void;
|
|
11
12
|
declare function canvas2Zip(canvases: HTMLCanvasElement[], fileName: string): Promise<unknown>;
|
|
12
|
-
|
|
13
|
+
declare function readPdfFile(buffer: ArrayBuffer): Promise<{
|
|
14
|
+
pdf: pdfjs_dist_types_src_display_api0.PDFDocumentProxy;
|
|
15
|
+
pages: pdfjs_dist_types_src_display_api0.PDFPageProxy[];
|
|
16
|
+
id: string;
|
|
17
|
+
canvases: HTMLCanvasElement[];
|
|
18
|
+
}>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/index.d.ts
|
|
21
|
+
type Color = string | [number, number, number, number];
|
|
22
|
+
interface TextStyle {
|
|
23
|
+
fontSize?: number;
|
|
24
|
+
fontName?: string;
|
|
25
|
+
fontStyle?: HTMLFontFace['style'];
|
|
26
|
+
fontWeight?: HTMLFontFace['weight'];
|
|
27
|
+
textColor?: Color;
|
|
28
|
+
}
|
|
29
|
+
interface DrawStyle {
|
|
30
|
+
drawColor?: Color;
|
|
31
|
+
fillColor?: Color;
|
|
32
|
+
charSpace?: number;
|
|
33
|
+
lineWidth?: number;
|
|
34
|
+
style?: 'S' | 'F' | 'FD' | 'DF' | null;
|
|
35
|
+
}
|
|
36
|
+
type PDFStyle = TextStyle & DrawStyle & {};
|
|
37
|
+
type PDFDataType = 'text' | 'image' | 'circle' | 'line' | 'lines';
|
|
38
|
+
type OTextOptions = TextOptions & TextStyle;
|
|
39
|
+
type OImageOptions = Omit<ImageOptions, 'imageData'> & {
|
|
40
|
+
imageData: ImageOptions['imageData'] | (() => ImageOptions['imageData']) | (() => Promise<ImageOptions['imageData']>);
|
|
41
|
+
};
|
|
42
|
+
type OCircleOptions = DrawStyle & {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
r: number;
|
|
46
|
+
};
|
|
47
|
+
type OLineOptions = DrawStyle & {
|
|
48
|
+
x1: number;
|
|
49
|
+
y1: number;
|
|
50
|
+
x2: number;
|
|
51
|
+
y2: number;
|
|
52
|
+
};
|
|
53
|
+
type OLinesOptions = DrawStyle & {
|
|
54
|
+
lines: number[][];
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
scale?: [number, number];
|
|
58
|
+
closed?: boolean;
|
|
59
|
+
};
|
|
60
|
+
type PDFDataRow<T extends PDFDataType = PDFDataType> = {
|
|
61
|
+
type: T;
|
|
62
|
+
} & (T extends 'text' ? OTextOptions : T extends 'image' ? OImageOptions : T extends 'circle' ? OCircleOptions : T extends 'line' ? OLineOptions : T extends 'lines' ? OLinesOptions : never);
|
|
63
|
+
declare function generatePDF(data: PDFDataRow[], options?: jsPDFOptions, globalStyle?: PDFStyle): Promise<{
|
|
64
|
+
pdf: jsPDF;
|
|
65
|
+
}>;
|
|
13
66
|
//#endregion
|
|
14
|
-
export { canvas2Pdf, canvas2Zip, openPdf };
|
|
67
|
+
export { PDFDataRow, canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,67 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTMLFontFace, ImageOptions, TextOptions, jsPDF, jsPDFOptions } from "jspdf";
|
|
2
|
+
import * as pdfjs_dist_types_src_display_api0 from "pdfjs-dist/types/src/display/api";
|
|
2
3
|
|
|
3
|
-
//#region src/
|
|
4
|
-
declare function openPdf(url?: string | URL): Promise<{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}>;
|
|
4
|
+
//#region src/utils.d.ts
|
|
5
|
+
declare function openPdf(url?: string | URL | File): Promise<{
|
|
6
|
+
pdf: pdfjs_dist_types_src_display_api0.PDFDocumentProxy;
|
|
7
|
+
pages: pdfjs_dist_types_src_display_api0.PDFPageProxy[];
|
|
8
|
+
id: string;
|
|
9
|
+
canvases: HTMLCanvasElement[];
|
|
10
|
+
} | undefined>;
|
|
10
11
|
declare function canvas2Pdf(canvases: HTMLCanvasElement[], fileName: string): void;
|
|
11
12
|
declare function canvas2Zip(canvases: HTMLCanvasElement[], fileName: string): Promise<unknown>;
|
|
12
|
-
|
|
13
|
+
declare function readPdfFile(buffer: ArrayBuffer): Promise<{
|
|
14
|
+
pdf: pdfjs_dist_types_src_display_api0.PDFDocumentProxy;
|
|
15
|
+
pages: pdfjs_dist_types_src_display_api0.PDFPageProxy[];
|
|
16
|
+
id: string;
|
|
17
|
+
canvases: HTMLCanvasElement[];
|
|
18
|
+
}>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/index.d.ts
|
|
21
|
+
type Color = string | [number, number, number, number];
|
|
22
|
+
interface TextStyle {
|
|
23
|
+
fontSize?: number;
|
|
24
|
+
fontName?: string;
|
|
25
|
+
fontStyle?: HTMLFontFace['style'];
|
|
26
|
+
fontWeight?: HTMLFontFace['weight'];
|
|
27
|
+
textColor?: Color;
|
|
28
|
+
}
|
|
29
|
+
interface DrawStyle {
|
|
30
|
+
drawColor?: Color;
|
|
31
|
+
fillColor?: Color;
|
|
32
|
+
charSpace?: number;
|
|
33
|
+
lineWidth?: number;
|
|
34
|
+
style?: 'S' | 'F' | 'FD' | 'DF' | null;
|
|
35
|
+
}
|
|
36
|
+
type PDFStyle = TextStyle & DrawStyle & {};
|
|
37
|
+
type PDFDataType = 'text' | 'image' | 'circle' | 'line' | 'lines';
|
|
38
|
+
type OTextOptions = TextOptions & TextStyle;
|
|
39
|
+
type OImageOptions = Omit<ImageOptions, 'imageData'> & {
|
|
40
|
+
imageData: ImageOptions['imageData'] | (() => ImageOptions['imageData']) | (() => Promise<ImageOptions['imageData']>);
|
|
41
|
+
};
|
|
42
|
+
type OCircleOptions = DrawStyle & {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
r: number;
|
|
46
|
+
};
|
|
47
|
+
type OLineOptions = DrawStyle & {
|
|
48
|
+
x1: number;
|
|
49
|
+
y1: number;
|
|
50
|
+
x2: number;
|
|
51
|
+
y2: number;
|
|
52
|
+
};
|
|
53
|
+
type OLinesOptions = DrawStyle & {
|
|
54
|
+
lines: number[][];
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
scale?: [number, number];
|
|
58
|
+
closed?: boolean;
|
|
59
|
+
};
|
|
60
|
+
type PDFDataRow<T extends PDFDataType = PDFDataType> = {
|
|
61
|
+
type: T;
|
|
62
|
+
} & (T extends 'text' ? OTextOptions : T extends 'image' ? OImageOptions : T extends 'circle' ? OCircleOptions : T extends 'line' ? OLineOptions : T extends 'lines' ? OLinesOptions : never);
|
|
63
|
+
declare function generatePDF(data: PDFDataRow[], options?: jsPDFOptions, globalStyle?: PDFStyle): Promise<{
|
|
64
|
+
pdf: jsPDF;
|
|
65
|
+
}>;
|
|
13
66
|
//#endregion
|
|
14
|
-
export { canvas2Pdf, canvas2Zip, openPdf };
|
|
67
|
+
export { PDFDataRow, canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { saveAs } from "file-saver";
|
|
2
1
|
import { jsPDF } from "jspdf";
|
|
2
|
+
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
6
|
|
|
7
|
-
//#region src/
|
|
7
|
+
//#region src/utils.ts
|
|
8
8
|
GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${version}/build/pdf.worker.min.mjs`;
|
|
9
9
|
function file2Buffer(file) {
|
|
10
10
|
return new Promise((resolve, reject) => {
|
|
@@ -39,6 +39,7 @@ function page2Canvas(page, id) {
|
|
|
39
39
|
canvas.style.height = `${height / pixelRatio}px`;
|
|
40
40
|
page.render({
|
|
41
41
|
canvasContext: ctx,
|
|
42
|
+
canvas,
|
|
42
43
|
viewport
|
|
43
44
|
}).promise.then(() => resolve(canvas)).catch((e) => reject(e));
|
|
44
45
|
});
|
|
@@ -60,7 +61,7 @@ async function pdf2Canvases(pdf) {
|
|
|
60
61
|
}
|
|
61
62
|
async function openPdf(url) {
|
|
62
63
|
try {
|
|
63
|
-
if (url) {
|
|
64
|
+
if (url && (typeof url === "string" || url instanceof URL)) {
|
|
64
65
|
const pdf = await getDocument(url).promise;
|
|
65
66
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
66
67
|
return {
|
|
@@ -69,10 +70,9 @@ async function openPdf(url) {
|
|
|
69
70
|
id,
|
|
70
71
|
canvases
|
|
71
72
|
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
const buffer = await file2Buffer(file);
|
|
73
|
+
}
|
|
74
|
+
if (url instanceof File) {
|
|
75
|
+
const buffer = await file2Buffer(url);
|
|
76
76
|
const pdf = await getDocument(buffer).promise;
|
|
77
77
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
78
78
|
return {
|
|
@@ -92,7 +92,7 @@ function canvas2Blob(canvas) {
|
|
|
92
92
|
try {
|
|
93
93
|
canvas.toBlob((blob) => {
|
|
94
94
|
if (blob) return resolve(blob);
|
|
95
|
-
return reject(new Error("canvas to blob error"));
|
|
95
|
+
return reject(/* @__PURE__ */ new Error("canvas to blob error"));
|
|
96
96
|
});
|
|
97
97
|
} catch (error) {
|
|
98
98
|
return reject(error);
|
|
@@ -129,6 +129,93 @@ function canvas2Zip(canvases, fileName) {
|
|
|
129
129
|
}).catch((error) => reject(error));
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
|
+
async function readPdfFile(buffer) {
|
|
133
|
+
const pdf = await getDocument(buffer).promise;
|
|
134
|
+
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
135
|
+
return {
|
|
136
|
+
pdf,
|
|
137
|
+
pages,
|
|
138
|
+
id,
|
|
139
|
+
canvases
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/index.ts
|
|
145
|
+
function setDrawStyle(pdf, data) {
|
|
146
|
+
const { drawColor, fillColor, lineWidth } = data;
|
|
147
|
+
if (typeof fillColor === "string") pdf.setFillColor(fillColor);
|
|
148
|
+
else pdf.setFillColor(...fillColor);
|
|
149
|
+
if (typeof drawColor === "string") pdf.setDrawColor(drawColor);
|
|
150
|
+
else pdf.setDrawColor(...drawColor);
|
|
151
|
+
pdf.setLineWidth(lineWidth);
|
|
152
|
+
}
|
|
153
|
+
async function generatePDF(data, options, globalStyle) {
|
|
154
|
+
const pdf = new jsPDF(options);
|
|
155
|
+
const font = pdf.getFont();
|
|
156
|
+
const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
|
|
157
|
+
const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
|
|
158
|
+
function setText(data$1) {
|
|
159
|
+
const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
|
|
160
|
+
pdf.setFontSize(fontSize);
|
|
161
|
+
pdf.setFont(fontName, fontStyle, fontWeight);
|
|
162
|
+
pdf.text(text, x, y, textOpt);
|
|
163
|
+
}
|
|
164
|
+
async function setImage(data$1) {
|
|
165
|
+
const { imageData, x, y, width, height, alias, compression, rotation } = data$1;
|
|
166
|
+
const _imageData = typeof imageData === "function" ? await Promise.try(imageData) : imageData;
|
|
167
|
+
pdf.addImage(_imageData, x, y, width, height, alias, compression, rotation);
|
|
168
|
+
}
|
|
169
|
+
function setCircle(data$1) {
|
|
170
|
+
const { x, y, r } = data$1;
|
|
171
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
172
|
+
setDrawStyle(pdf, {
|
|
173
|
+
fillColor,
|
|
174
|
+
drawColor,
|
|
175
|
+
lineWidth
|
|
176
|
+
});
|
|
177
|
+
pdf.circle(x, y, r, style);
|
|
178
|
+
}
|
|
179
|
+
function setLine(data$1) {
|
|
180
|
+
const { x1, y1, x2, y2 } = data$1;
|
|
181
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
182
|
+
setDrawStyle(pdf, {
|
|
183
|
+
fillColor,
|
|
184
|
+
drawColor,
|
|
185
|
+
lineWidth
|
|
186
|
+
});
|
|
187
|
+
pdf.line(x1, y1, x2, y2, style);
|
|
188
|
+
}
|
|
189
|
+
function setLines(data$1) {
|
|
190
|
+
const { lines, x, y, scale = [1, 1], closed = false } = data$1;
|
|
191
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
192
|
+
setDrawStyle(pdf, {
|
|
193
|
+
fillColor,
|
|
194
|
+
drawColor,
|
|
195
|
+
lineWidth
|
|
196
|
+
});
|
|
197
|
+
pdf.lines(lines, x, y, scale, style, closed);
|
|
198
|
+
}
|
|
199
|
+
for (const { type,...opt } of data) switch (type) {
|
|
200
|
+
case "text":
|
|
201
|
+
setText(opt);
|
|
202
|
+
break;
|
|
203
|
+
case "image":
|
|
204
|
+
await setImage(opt);
|
|
205
|
+
break;
|
|
206
|
+
case "circle":
|
|
207
|
+
setCircle(opt);
|
|
208
|
+
break;
|
|
209
|
+
case "line":
|
|
210
|
+
setLine(opt);
|
|
211
|
+
break;
|
|
212
|
+
case "lines":
|
|
213
|
+
setLines(opt);
|
|
214
|
+
break;
|
|
215
|
+
default: break;
|
|
216
|
+
}
|
|
217
|
+
return { pdf };
|
|
218
|
+
}
|
|
132
219
|
|
|
133
220
|
//#endregion
|
|
134
|
-
export { canvas2Pdf, canvas2Zip, openPdf };
|
|
221
|
+
export { canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oiij/js-pdf",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "A simple PDF library for JavaScript",
|
|
6
6
|
"author": "oiij",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"homepage": "https://github.com/oiij/
|
|
8
|
+
"homepage": "https://github.com/oiij/use",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git@github.com:oiij/
|
|
11
|
+
"url": "git@github.com:oiij/use.git"
|
|
12
12
|
},
|
|
13
|
-
"bugs": "https://github.com/oiij/
|
|
13
|
+
"bugs": "https://github.com/oiij/use/issues",
|
|
14
14
|
"keywords": [
|
|
15
|
+
"@oiij/use",
|
|
15
16
|
"js-pdf"
|
|
16
17
|
],
|
|
17
18
|
"sideEffects": false,
|
|
@@ -31,66 +32,27 @@
|
|
|
31
32
|
"dist",
|
|
32
33
|
"package.json"
|
|
33
34
|
],
|
|
34
|
-
"scripts": {
|
|
35
|
-
"dev": "tsdown --watch",
|
|
36
|
-
"build": "tsc --noEmit && tsdown",
|
|
37
|
-
"lint": "eslint .",
|
|
38
|
-
"lint:fix": "eslint . --fix",
|
|
39
|
-
"prepublishOnly": "pnpm build",
|
|
40
|
-
"release": "bumpp && npm publish",
|
|
41
|
-
"awe": "pnpx are-we-esm",
|
|
42
|
-
"nmi": "pnpx node-modules-inspector",
|
|
43
|
-
"start": "esno src/index.ts",
|
|
44
|
-
"test": "vitest",
|
|
45
|
-
"update:deps": "taze -w && pnpm i",
|
|
46
|
-
"type:check": "tsc --noEmit",
|
|
47
|
-
"cz": "czg",
|
|
48
|
-
"commit": "git pull && git add -A && pnpm cz && git push",
|
|
49
|
-
"link": "pnpm link --global",
|
|
50
|
-
"preinstall": "npx only-allow pnpm"
|
|
51
|
-
},
|
|
52
35
|
"peerDependencies": {
|
|
53
36
|
"file-saver": "^2.0.5",
|
|
54
37
|
"jspdf": "^3.0.1",
|
|
55
38
|
"jszip": "^3.10.1",
|
|
56
39
|
"nanoid": "^5.1.5",
|
|
57
|
-
"pdfjs-dist": "^5.
|
|
40
|
+
"pdfjs-dist": "^5.4.54"
|
|
58
41
|
},
|
|
59
42
|
"devDependencies": {
|
|
60
|
-
"@antfu/eslint-config": "^4.12.0",
|
|
61
|
-
"@oiij/tsconfig": "^0.0.1",
|
|
62
43
|
"@types/file-saver": "^2.0.7",
|
|
63
|
-
"@types/node": "^22.14.1",
|
|
64
|
-
"@types/wicg-file-system-access": "^2023.10.6",
|
|
65
|
-
"@vitest/ui": "^3.1.2",
|
|
66
|
-
"bumpp": "^10.1.0",
|
|
67
|
-
"commitlint": "^19.8.0",
|
|
68
|
-
"cz-git": "^1.11.1",
|
|
69
|
-
"czg": "^1.11.1",
|
|
70
|
-
"eslint": "^9.25.1",
|
|
71
|
-
"eslint-plugin-format": "^1.0.1",
|
|
72
|
-
"esno": "^4.8.0",
|
|
73
44
|
"file-saver": "^2.0.5",
|
|
74
45
|
"jspdf": "^3.0.1",
|
|
75
46
|
"jszip": "^3.10.1",
|
|
76
|
-
"lint-staged": "^15.5.1",
|
|
77
47
|
"nanoid": "^5.1.5",
|
|
78
|
-
"pdfjs-dist": "^5.
|
|
79
|
-
"simple-git-hooks": "^2.12.1",
|
|
80
|
-
"taze": "^19.0.4",
|
|
81
|
-
"tsdown": "^0.9.3",
|
|
82
|
-
"typescript": "^5.8.3",
|
|
83
|
-
"vitest": "^3.1.2"
|
|
84
|
-
},
|
|
85
|
-
"simple-git-hooks": {
|
|
86
|
-
"pre-commit": "pnpm lint-staged && pnpm type:check"
|
|
87
|
-
},
|
|
88
|
-
"lint-staged": {
|
|
89
|
-
"*.{js,jsx,ts,tsx}": [
|
|
90
|
-
"pnpm lint:fix"
|
|
91
|
-
]
|
|
48
|
+
"pdfjs-dist": "^5.4.54"
|
|
92
49
|
},
|
|
93
50
|
"publishConfig": {
|
|
94
51
|
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "tsdown --watch",
|
|
55
|
+
"build": "tsc --noEmit && tsdown",
|
|
56
|
+
"bumpp": "bumpp --no-push"
|
|
95
57
|
}
|
|
96
|
-
}
|
|
58
|
+
}
|