@oiij/js-pdf 0.0.6 → 0.0.7

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.
@@ -154,7 +154,7 @@ async function generatePDF(data, options, globalStyle) {
154
154
  const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
155
155
  const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
156
156
  function setText(data$1) {
157
- const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
157
+ const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight, ...textOpt } = data$1;
158
158
  pdf.setFontSize(fontSize);
159
159
  pdf.setFont(fontName, fontStyle, fontWeight);
160
160
  pdf.text(text, x, y, textOpt);
@@ -194,7 +194,7 @@ async function generatePDF(data, options, globalStyle) {
194
194
  });
195
195
  pdf.lines(lines, x, y, scale, style, closed);
196
196
  }
197
- for (const { type,...opt } of data) switch (type) {
197
+ for (const { type, ...opt } of data) switch (type) {
198
198
  case "text":
199
199
  setText(opt);
200
200
  break;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oiij/js-pdf",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "description": "A simple PDF library for JavaScript",
6
6
  "author": "oiij",
7
7
  "license": "MIT",
@@ -16,16 +16,9 @@
16
16
  "js-pdf"
17
17
  ],
18
18
  "sideEffects": false,
19
- "exports": {
20
- ".": {
21
- "types": "./dist/index.d.ts",
22
- "import": "./dist/index.js",
23
- "require": "./dist/index.cjs"
24
- }
25
- },
26
- "main": "./dist/index.js",
27
- "module": "./dist/index.js",
28
- "types": "./dist/index.d.ts",
19
+ "main": "./dist/index.mjs",
20
+ "module": "./dist/index.mjs",
21
+ "types": "./dist/index.d.mts",
29
22
  "files": [
30
23
  "LICENSE",
31
24
  "README.md",
@@ -33,19 +26,20 @@
33
26
  "package.json"
34
27
  ],
35
28
  "peerDependencies": {
29
+ "@types/file-saver": "^2.0.7",
36
30
  "file-saver": "^2.0.5",
37
- "jspdf": "^3.0.3",
31
+ "jspdf": "^3.0.4",
38
32
  "jszip": "^3.10.1",
39
33
  "nanoid": "^5.1.6",
40
- "pdfjs-dist": "^5.4.296"
34
+ "pdfjs-dist": "^5.4.449"
41
35
  },
42
36
  "devDependencies": {
43
37
  "@types/file-saver": "^2.0.7",
44
38
  "file-saver": "^2.0.5",
45
- "jspdf": "^3.0.3",
39
+ "jspdf": "^3.0.4",
46
40
  "jszip": "^3.10.1",
47
41
  "nanoid": "^5.1.6",
48
- "pdfjs-dist": "^5.4.296"
42
+ "pdfjs-dist": "^5.4.449"
49
43
  },
50
44
  "publishConfig": {
51
45
  "access": "public"
package/dist/index.cjs DELETED
@@ -1,251 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
-
23
- //#endregion
24
- let jspdf = require("jspdf");
25
- jspdf = __toESM(jspdf);
26
- let file_saver = require("file-saver");
27
- file_saver = __toESM(file_saver);
28
- let jszip = require("jszip");
29
- jszip = __toESM(jszip);
30
- let nanoid = require("nanoid");
31
- nanoid = __toESM(nanoid);
32
- let pdfjs_dist = require("pdfjs-dist");
33
- pdfjs_dist = __toESM(pdfjs_dist);
34
-
35
- //#region src/utils.ts
36
- pdfjs_dist.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs_dist.version}/build/pdf.worker.min.mjs`;
37
- function file2Buffer(file) {
38
- return new Promise((resolve, reject) => {
39
- const reader = new FileReader();
40
- reader.onload = () => {
41
- return resolve(reader.result);
42
- };
43
- reader.onerror = () => {
44
- return reject(reader.error?.message);
45
- };
46
- reader.readAsArrayBuffer(file);
47
- });
48
- }
49
- function createCanvas(width, height, id) {
50
- const canvas = document.createElement("canvas");
51
- const ctx = canvas.getContext("2d");
52
- canvas.id = id;
53
- canvas.width = width;
54
- canvas.height = height;
55
- return {
56
- canvas,
57
- ctx
58
- };
59
- }
60
- function page2Canvas(page, id) {
61
- return new Promise((resolve, reject) => {
62
- const pixelRatio = Math.max(window.devicePixelRatio, 2);
63
- const viewport = page.getViewport({ scale: pixelRatio });
64
- const { width, height } = viewport;
65
- const { canvas, ctx } = createCanvas(width, height, `page-${id}-${page._pageIndex}`);
66
- canvas.style.width = `${width / pixelRatio}px`;
67
- canvas.style.height = `${height / pixelRatio}px`;
68
- page.render({
69
- canvasContext: ctx,
70
- canvas,
71
- viewport
72
- }).promise.then(() => resolve(canvas)).catch((e) => reject(e));
73
- });
74
- }
75
- async function pdf2Canvases(pdf) {
76
- try {
77
- const pages = await Promise.all(Array.from({ length: pdf.numPages }).map((_, i) => pdf.getPage(i + 1)));
78
- const id = (0, nanoid.nanoid)();
79
- return {
80
- pages,
81
- id,
82
- canvases: await Promise.all(pages.map((page) => page2Canvas(page, id)))
83
- };
84
- } catch (error) {
85
- console.error(error);
86
- return Promise.reject(error);
87
- }
88
- }
89
- async function openPdf(url) {
90
- try {
91
- if (url && (typeof url === "string" || url instanceof URL)) {
92
- const pdf = await (0, pdfjs_dist.getDocument)(url).promise;
93
- const { pages, id, canvases } = await pdf2Canvases(pdf);
94
- return {
95
- pdf,
96
- pages,
97
- id,
98
- canvases
99
- };
100
- }
101
- if (url instanceof File) {
102
- const pdf = await (0, pdfjs_dist.getDocument)(await file2Buffer(url)).promise;
103
- const { pages, id, canvases } = await pdf2Canvases(pdf);
104
- return {
105
- pdf,
106
- pages,
107
- id,
108
- canvases
109
- };
110
- }
111
- } catch (error) {
112
- console.error(error);
113
- return Promise.reject(error);
114
- }
115
- }
116
- function canvas2Blob(canvas) {
117
- return new Promise((resolve, reject) => {
118
- try {
119
- canvas.toBlob((blob) => {
120
- if (blob) return resolve(blob);
121
- return reject(/* @__PURE__ */ new Error("canvas to blob error"));
122
- });
123
- } catch (error) {
124
- return reject(error);
125
- }
126
- });
127
- }
128
- function canvas2Pdf(canvases, fileName) {
129
- let doc = null;
130
- canvases.forEach((canvas, i) => {
131
- const width = Number((canvas.width / 2 / 72 * 25.4).toFixed());
132
- const height = Number((canvas.height / 2 / 72 * 25.4).toFixed());
133
- if (!doc) doc = new jspdf.jsPDF({
134
- unit: "mm",
135
- format: [width, height]
136
- });
137
- if (i > 0) doc.addPage([width, height]);
138
- doc.setPage(i + 1);
139
- doc.addImage(canvas, "PNG", 0, 0, width, height);
140
- if (i === canvases.length - 1) doc.save(fileName);
141
- return doc;
142
- });
143
- }
144
- function canvas2Zip(canvases, fileName) {
145
- return new Promise((resolve, reject) => {
146
- const zip = new jszip.default();
147
- Promise.all(canvases.map((canvas) => canvas2Blob(canvas))).then((blobs) => {
148
- blobs.forEach((blob, i) => {
149
- zip.file(`${canvases[i].id}.jpg`, blob);
150
- });
151
- zip.generateAsync({ type: "blob" }).then((blob) => {
152
- (0, file_saver.saveAs)(blob, `${fileName}.zip`);
153
- return resolve(blobs);
154
- }).catch((error) => reject(error));
155
- }).catch((error) => reject(error));
156
- });
157
- }
158
- async function readPdfFile(buffer) {
159
- const pdf = await (0, pdfjs_dist.getDocument)(buffer).promise;
160
- const { pages, id, canvases } = await pdf2Canvases(pdf);
161
- return {
162
- pdf,
163
- pages,
164
- id,
165
- canvases
166
- };
167
- }
168
-
169
- //#endregion
170
- //#region src/index.ts
171
- function setDrawStyle(pdf, data) {
172
- const { drawColor, fillColor, lineWidth } = data;
173
- if (typeof fillColor === "string") pdf.setFillColor(fillColor);
174
- else pdf.setFillColor(...fillColor);
175
- if (typeof drawColor === "string") pdf.setDrawColor(drawColor);
176
- else pdf.setDrawColor(...drawColor);
177
- pdf.setLineWidth(lineWidth);
178
- }
179
- async function generatePDF(data, options, globalStyle) {
180
- const pdf = new jspdf.jsPDF(options);
181
- const font = pdf.getFont();
182
- const { fontSize: _fontSize = pdf.getFontSize(), fontName: _fontName = font.fontName, fontStyle: _fontStyle = font.fontStyle, fontWeight: _fontWeight = "normal", textColor: _textColor = pdf.getTextColor() } = globalStyle ?? {};
183
- const { drawColor: _drawColor = pdf.getDrawColor(), fillColor: _fillColor = pdf.getDrawColor(), charSpace: _charSpace = pdf.getCharSpace(), lineWidth: _lineWidth = pdf.getLineWidth(), style: _style = "S" } = globalStyle ?? {};
184
- function setText(data$1) {
185
- const { text, x, y, fontSize = _fontSize, fontName = _fontName, fontStyle = _fontStyle, fontWeight = _fontWeight,...textOpt } = data$1;
186
- pdf.setFontSize(fontSize);
187
- pdf.setFont(fontName, fontStyle, fontWeight);
188
- pdf.text(text, x, y, textOpt);
189
- }
190
- async function setImage(data$1) {
191
- const { imageData, x, y, width, height, alias, compression, rotation } = data$1;
192
- const _imageData = typeof imageData === "function" ? await Promise.try(imageData) : imageData;
193
- pdf.addImage(_imageData, x, y, width, height, alias, compression, rotation);
194
- }
195
- function setCircle(data$1) {
196
- const { x, y, r } = data$1;
197
- const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
198
- setDrawStyle(pdf, {
199
- fillColor,
200
- drawColor,
201
- lineWidth
202
- });
203
- pdf.circle(x, y, r, style);
204
- }
205
- function setLine(data$1) {
206
- const { x1, y1, x2, y2 } = data$1;
207
- const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
208
- setDrawStyle(pdf, {
209
- fillColor,
210
- drawColor,
211
- lineWidth
212
- });
213
- pdf.line(x1, y1, x2, y2, style);
214
- }
215
- function setLines(data$1) {
216
- const { lines, x, y, scale = [1, 1], closed = false } = data$1;
217
- const { fillColor = _fillColor, drawColor = _drawColor, lineWidth = _lineWidth, style = _style } = data$1;
218
- setDrawStyle(pdf, {
219
- fillColor,
220
- drawColor,
221
- lineWidth
222
- });
223
- pdf.lines(lines, x, y, scale, style, closed);
224
- }
225
- for (const { type,...opt } of data) switch (type) {
226
- case "text":
227
- setText(opt);
228
- break;
229
- case "image":
230
- await setImage(opt);
231
- break;
232
- case "circle":
233
- setCircle(opt);
234
- break;
235
- case "line":
236
- setLine(opt);
237
- break;
238
- case "lines":
239
- setLines(opt);
240
- break;
241
- default: break;
242
- }
243
- return { pdf };
244
- }
245
-
246
- //#endregion
247
- exports.canvas2Pdf = canvas2Pdf;
248
- exports.canvas2Zip = canvas2Zip;
249
- exports.generatePDF = generatePDF;
250
- exports.openPdf = openPdf;
251
- exports.readPdfFile = readPdfFile;
package/dist/index.d.ts DELETED
@@ -1,67 +0,0 @@
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";
3
-
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>;
11
- declare function canvas2Pdf(canvases: HTMLCanvasElement[], fileName: string): void;
12
- declare function canvas2Zip(canvases: HTMLCanvasElement[], fileName: string): Promise<unknown>;
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
- }>;
66
- //#endregion
67
- export { PDFDataRow, canvas2Pdf, canvas2Zip, generatePDF, openPdf, readPdfFile };
File without changes