@oiij/js-pdf 0.0.1 → 0.0.2
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 +98 -11
- package/dist/index.d.cts +63 -10
- package/dist/index.d.ts +63 -10
- package/dist/index.js +94 -8
- package/package.json +12 -50
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) => {
|
|
@@ -84,7 +83,7 @@ async function pdf2Canvases(pdf) {
|
|
|
84
83
|
}
|
|
85
84
|
async function openPdf(url) {
|
|
86
85
|
try {
|
|
87
|
-
if (url) {
|
|
86
|
+
if (url && (typeof url === "string" || url instanceof URL)) {
|
|
88
87
|
const pdf = await (0, pdfjs_dist.getDocument)(url).promise;
|
|
89
88
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
90
89
|
return {
|
|
@@ -93,10 +92,9 @@ async function openPdf(url) {
|
|
|
93
92
|
id,
|
|
94
93
|
canvases
|
|
95
94
|
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
const buffer = await file2Buffer(file);
|
|
95
|
+
}
|
|
96
|
+
if (url instanceof File) {
|
|
97
|
+
const buffer = await file2Buffer(url);
|
|
100
98
|
const pdf = await (0, pdfjs_dist.getDocument)(buffer).promise;
|
|
101
99
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
102
100
|
return {
|
|
@@ -153,8 +151,97 @@ function canvas2Zip(canvases, fileName) {
|
|
|
153
151
|
}).catch((error) => reject(error));
|
|
154
152
|
});
|
|
155
153
|
}
|
|
154
|
+
async function readPdfFile(buffer) {
|
|
155
|
+
const pdf = await (0, pdfjs_dist.getDocument)(buffer).promise;
|
|
156
|
+
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
157
|
+
return {
|
|
158
|
+
pdf,
|
|
159
|
+
pages,
|
|
160
|
+
id,
|
|
161
|
+
canvases
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/index.ts
|
|
167
|
+
function setDrawStyle(pdf, data) {
|
|
168
|
+
const { drawColor, fillColor, lineWidth } = data;
|
|
169
|
+
if (typeof fillColor === "string") pdf.setFillColor(fillColor);
|
|
170
|
+
else pdf.setFillColor(...fillColor);
|
|
171
|
+
if (typeof drawColor === "string") pdf.setDrawColor(drawColor);
|
|
172
|
+
else pdf.setDrawColor(...drawColor);
|
|
173
|
+
pdf.setLineWidth(lineWidth);
|
|
174
|
+
}
|
|
175
|
+
async function generatePDF(data, options, globalStyle) {
|
|
176
|
+
const pdf = new jspdf.jsPDF(options);
|
|
177
|
+
const font = pdf.getFont();
|
|
178
|
+
const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
|
|
179
|
+
const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
|
|
180
|
+
function setText(data$1) {
|
|
181
|
+
const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
|
|
182
|
+
pdf.setFontSize(fontSize);
|
|
183
|
+
pdf.setFont(fontName, fontStyle, fontWeight);
|
|
184
|
+
pdf.text(text, x, y, textOpt);
|
|
185
|
+
}
|
|
186
|
+
async function setImage(data$1) {
|
|
187
|
+
const { imageData, x, y, width, height, alias, compression, rotation } = data$1;
|
|
188
|
+
const _imageData = typeof imageData === "function" ? await Promise.try(imageData) : imageData;
|
|
189
|
+
pdf.addImage(_imageData, x, y, width, height, alias, compression, rotation);
|
|
190
|
+
}
|
|
191
|
+
function setCircle(data$1) {
|
|
192
|
+
const { x, y, r } = data$1;
|
|
193
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
194
|
+
setDrawStyle(pdf, {
|
|
195
|
+
fillColor,
|
|
196
|
+
drawColor,
|
|
197
|
+
lineWidth
|
|
198
|
+
});
|
|
199
|
+
pdf.circle(x, y, r, style);
|
|
200
|
+
}
|
|
201
|
+
function setLine(data$1) {
|
|
202
|
+
const { x1, y1, x2, y2 } = data$1;
|
|
203
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
204
|
+
setDrawStyle(pdf, {
|
|
205
|
+
fillColor,
|
|
206
|
+
drawColor,
|
|
207
|
+
lineWidth
|
|
208
|
+
});
|
|
209
|
+
pdf.line(x1, y1, x2, y2, style);
|
|
210
|
+
}
|
|
211
|
+
function setLines(data$1) {
|
|
212
|
+
const { lines, x, y, scale = [1, 1], closed = false } = data$1;
|
|
213
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
214
|
+
setDrawStyle(pdf, {
|
|
215
|
+
fillColor,
|
|
216
|
+
drawColor,
|
|
217
|
+
lineWidth
|
|
218
|
+
});
|
|
219
|
+
pdf.lines(lines, x, y, scale, style, closed);
|
|
220
|
+
}
|
|
221
|
+
for (const { type,...opt } of data) switch (type) {
|
|
222
|
+
case "text":
|
|
223
|
+
setText(opt);
|
|
224
|
+
break;
|
|
225
|
+
case "image":
|
|
226
|
+
await setImage(opt);
|
|
227
|
+
break;
|
|
228
|
+
case "circle":
|
|
229
|
+
setCircle(opt);
|
|
230
|
+
break;
|
|
231
|
+
case "line":
|
|
232
|
+
setLine(opt);
|
|
233
|
+
break;
|
|
234
|
+
case "lines":
|
|
235
|
+
setLines(opt);
|
|
236
|
+
break;
|
|
237
|
+
default: break;
|
|
238
|
+
}
|
|
239
|
+
return { pdf };
|
|
240
|
+
}
|
|
156
241
|
|
|
157
242
|
//#endregion
|
|
158
|
-
exports.canvas2Pdf = canvas2Pdf
|
|
159
|
-
exports.canvas2Zip = canvas2Zip
|
|
160
|
-
exports.
|
|
243
|
+
exports.canvas2Pdf = canvas2Pdf;
|
|
244
|
+
exports.canvas2Zip = canvas2Zip;
|
|
245
|
+
exports.generatePDF = generatePDF;
|
|
246
|
+
exports.openPdf = openPdf;
|
|
247
|
+
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) => {
|
|
@@ -60,7 +60,7 @@ async function pdf2Canvases(pdf) {
|
|
|
60
60
|
}
|
|
61
61
|
async function openPdf(url) {
|
|
62
62
|
try {
|
|
63
|
-
if (url) {
|
|
63
|
+
if (url && (typeof url === "string" || url instanceof URL)) {
|
|
64
64
|
const pdf = await getDocument(url).promise;
|
|
65
65
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
66
66
|
return {
|
|
@@ -69,10 +69,9 @@ async function openPdf(url) {
|
|
|
69
69
|
id,
|
|
70
70
|
canvases
|
|
71
71
|
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
const buffer = await file2Buffer(file);
|
|
72
|
+
}
|
|
73
|
+
if (url instanceof File) {
|
|
74
|
+
const buffer = await file2Buffer(url);
|
|
76
75
|
const pdf = await getDocument(buffer).promise;
|
|
77
76
|
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
78
77
|
return {
|
|
@@ -129,6 +128,93 @@ function canvas2Zip(canvases, fileName) {
|
|
|
129
128
|
}).catch((error) => reject(error));
|
|
130
129
|
});
|
|
131
130
|
}
|
|
131
|
+
async function readPdfFile(buffer) {
|
|
132
|
+
const pdf = await getDocument(buffer).promise;
|
|
133
|
+
const { pages, id, canvases } = await pdf2Canvases(pdf);
|
|
134
|
+
return {
|
|
135
|
+
pdf,
|
|
136
|
+
pages,
|
|
137
|
+
id,
|
|
138
|
+
canvases
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/index.ts
|
|
144
|
+
function setDrawStyle(pdf, data) {
|
|
145
|
+
const { drawColor, fillColor, lineWidth } = data;
|
|
146
|
+
if (typeof fillColor === "string") pdf.setFillColor(fillColor);
|
|
147
|
+
else pdf.setFillColor(...fillColor);
|
|
148
|
+
if (typeof drawColor === "string") pdf.setDrawColor(drawColor);
|
|
149
|
+
else pdf.setDrawColor(...drawColor);
|
|
150
|
+
pdf.setLineWidth(lineWidth);
|
|
151
|
+
}
|
|
152
|
+
async function generatePDF(data, options, globalStyle) {
|
|
153
|
+
const pdf = new jsPDF(options);
|
|
154
|
+
const font = pdf.getFont();
|
|
155
|
+
const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
|
|
156
|
+
const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
|
|
157
|
+
function setText(data$1) {
|
|
158
|
+
const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
|
|
159
|
+
pdf.setFontSize(fontSize);
|
|
160
|
+
pdf.setFont(fontName, fontStyle, fontWeight);
|
|
161
|
+
pdf.text(text, x, y, textOpt);
|
|
162
|
+
}
|
|
163
|
+
async function setImage(data$1) {
|
|
164
|
+
const { imageData, x, y, width, height, alias, compression, rotation } = data$1;
|
|
165
|
+
const _imageData = typeof imageData === "function" ? await Promise.try(imageData) : imageData;
|
|
166
|
+
pdf.addImage(_imageData, x, y, width, height, alias, compression, rotation);
|
|
167
|
+
}
|
|
168
|
+
function setCircle(data$1) {
|
|
169
|
+
const { x, y, r } = data$1;
|
|
170
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
171
|
+
setDrawStyle(pdf, {
|
|
172
|
+
fillColor,
|
|
173
|
+
drawColor,
|
|
174
|
+
lineWidth
|
|
175
|
+
});
|
|
176
|
+
pdf.circle(x, y, r, style);
|
|
177
|
+
}
|
|
178
|
+
function setLine(data$1) {
|
|
179
|
+
const { x1, y1, x2, y2 } = data$1;
|
|
180
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
181
|
+
setDrawStyle(pdf, {
|
|
182
|
+
fillColor,
|
|
183
|
+
drawColor,
|
|
184
|
+
lineWidth
|
|
185
|
+
});
|
|
186
|
+
pdf.line(x1, y1, x2, y2, style);
|
|
187
|
+
}
|
|
188
|
+
function setLines(data$1) {
|
|
189
|
+
const { lines, x, y, scale = [1, 1], closed = false } = data$1;
|
|
190
|
+
const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
|
|
191
|
+
setDrawStyle(pdf, {
|
|
192
|
+
fillColor,
|
|
193
|
+
drawColor,
|
|
194
|
+
lineWidth
|
|
195
|
+
});
|
|
196
|
+
pdf.lines(lines, x, y, scale, style, closed);
|
|
197
|
+
}
|
|
198
|
+
for (const { type,...opt } of data) switch (type) {
|
|
199
|
+
case "text":
|
|
200
|
+
setText(opt);
|
|
201
|
+
break;
|
|
202
|
+
case "image":
|
|
203
|
+
await setImage(opt);
|
|
204
|
+
break;
|
|
205
|
+
case "circle":
|
|
206
|
+
setCircle(opt);
|
|
207
|
+
break;
|
|
208
|
+
case "line":
|
|
209
|
+
setLine(opt);
|
|
210
|
+
break;
|
|
211
|
+
case "lines":
|
|
212
|
+
setLines(opt);
|
|
213
|
+
break;
|
|
214
|
+
default: break;
|
|
215
|
+
}
|
|
216
|
+
return { pdf };
|
|
217
|
+
}
|
|
132
218
|
|
|
133
219
|
//#endregion
|
|
134
|
-
export { canvas2Pdf, canvas2Zip, openPdf };
|
|
220
|
+
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.2",
|
|
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,24 +32,6 @@
|
|
|
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",
|
|
@@ -57,40 +40,19 @@
|
|
|
57
40
|
"pdfjs-dist": "^5.1.91"
|
|
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.3.31"
|
|
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
|
+
}
|