dom-to-vector-pdf 0.0.4 → 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.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/dom-converter.d.ts +5 -0
- package/dist/dom-converter.d.ts.map +1 -1
- package/dist/index.esm.js +22 -6
- package/dist/index.js +22 -6
- package/package.json +13 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
package/README.zh-CN.md
CHANGED
package/dist/dom-converter.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { ExportPdfOptions, LifecycleHooks } from './types';
|
|
|
4
4
|
*/
|
|
5
5
|
export declare class DomToPdfConverter {
|
|
6
6
|
private fontManager;
|
|
7
|
+
private resourceQueue;
|
|
7
8
|
constructor();
|
|
8
9
|
/**
|
|
9
10
|
* Export PDF
|
|
@@ -25,5 +26,9 @@ export declare class DomToPdfConverter {
|
|
|
25
26
|
* Render SVG to PDF
|
|
26
27
|
*/
|
|
27
28
|
private renderSvgToPdf;
|
|
29
|
+
/**
|
|
30
|
+
* Load resource
|
|
31
|
+
*/
|
|
32
|
+
private loadResource;
|
|
28
33
|
}
|
|
29
34
|
//# sourceMappingURL=dom-converter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom-converter.d.ts","sourceRoot":"","sources":["../src/dom-converter.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGhE;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,WAAW,CAAc;;
|
|
1
|
+
{"version":3,"file":"dom-converter.d.ts","sourceRoot":"","sources":["../src/dom-converter.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGhE;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,aAAa,CAAuB;;IAM5C;;OAEG;IACU,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAmDxF;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAwBzB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAezB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAUzB;;OAEG;YACW,cAAc;IAS5B;;OAEG;YACW,YAAY;CAY3B"}
|
package/dist/index.esm.js
CHANGED
|
@@ -158,6 +158,7 @@ class FontManager {
|
|
|
158
158
|
*/
|
|
159
159
|
class DomToPdfConverter {
|
|
160
160
|
constructor() {
|
|
161
|
+
this.resourceQueue = [];
|
|
161
162
|
this.fontManager = FontManager.getInstance();
|
|
162
163
|
}
|
|
163
164
|
/**
|
|
@@ -171,27 +172,29 @@ class DomToPdfConverter {
|
|
|
171
172
|
hooks?.afterDomClone?.(element);
|
|
172
173
|
// 2. Process SVG symbols
|
|
173
174
|
inlineSvgSymbols(element);
|
|
174
|
-
// 3.
|
|
175
|
+
// 3. Load resource
|
|
176
|
+
await this.loadResource(element);
|
|
177
|
+
// 4. Convert to SVG
|
|
175
178
|
const svgDocument = elementToSVG(element);
|
|
176
179
|
parentElement?.removeChild(element);
|
|
177
180
|
const svgElement = svgDocument.documentElement;
|
|
178
181
|
document.body.appendChild(svgElement);
|
|
179
182
|
this.prepareSvgElement(svgElement);
|
|
180
|
-
//
|
|
183
|
+
// 5. Process SVG fonts
|
|
181
184
|
processSvgFonts(svgElement, this.fontManager);
|
|
182
185
|
// Call lifecycle hook
|
|
183
186
|
hooks?.beforeSvgConvert?.(svgElement);
|
|
184
|
-
//
|
|
187
|
+
// 6. Create PDF document
|
|
185
188
|
const pdf = this.createPdfDocument(svgElement);
|
|
186
189
|
this.fontManager.setPdfInstance(pdf);
|
|
187
|
-
//
|
|
190
|
+
// 7. Draw SVG content to PDF
|
|
188
191
|
await this.renderSvgToPdf(svgElement, pdf);
|
|
189
192
|
// Call lifecycle hook
|
|
190
193
|
hooks?.beforePdfGenerate?.(pdf);
|
|
191
194
|
hooks?.beforePdfSave?.(pdf);
|
|
192
|
-
//
|
|
195
|
+
// 8. Save PDF
|
|
193
196
|
pdf.save(`${options.filename}.pdf`);
|
|
194
|
-
//
|
|
197
|
+
// 9. Clean up temporary elements
|
|
195
198
|
svgElement.remove();
|
|
196
199
|
this.fontManager.setPdfInstance(null);
|
|
197
200
|
}
|
|
@@ -259,6 +262,19 @@ class DomToPdfConverter {
|
|
|
259
262
|
height: pdf.internal.pageSize.getHeight(),
|
|
260
263
|
});
|
|
261
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Load resource
|
|
267
|
+
*/
|
|
268
|
+
async loadResource(element) {
|
|
269
|
+
this.resourceQueue = [];
|
|
270
|
+
const resources = element.querySelectorAll('img');
|
|
271
|
+
resources.forEach((resource) => {
|
|
272
|
+
this.resourceQueue.push(new Promise((resolve) => {
|
|
273
|
+
resource.onload = () => resolve(void 0);
|
|
274
|
+
}));
|
|
275
|
+
});
|
|
276
|
+
return Promise.allSettled(this.resourceQueue);
|
|
277
|
+
}
|
|
262
278
|
}
|
|
263
279
|
|
|
264
280
|
/**
|
package/dist/index.js
CHANGED
|
@@ -160,6 +160,7 @@
|
|
|
160
160
|
*/
|
|
161
161
|
class DomToPdfConverter {
|
|
162
162
|
constructor() {
|
|
163
|
+
this.resourceQueue = [];
|
|
163
164
|
this.fontManager = FontManager.getInstance();
|
|
164
165
|
}
|
|
165
166
|
/**
|
|
@@ -173,27 +174,29 @@
|
|
|
173
174
|
hooks?.afterDomClone?.(element);
|
|
174
175
|
// 2. Process SVG symbols
|
|
175
176
|
inlineSvgSymbols(element);
|
|
176
|
-
// 3.
|
|
177
|
+
// 3. Load resource
|
|
178
|
+
await this.loadResource(element);
|
|
179
|
+
// 4. Convert to SVG
|
|
177
180
|
const svgDocument = domToSvg.elementToSVG(element);
|
|
178
181
|
parentElement?.removeChild(element);
|
|
179
182
|
const svgElement = svgDocument.documentElement;
|
|
180
183
|
document.body.appendChild(svgElement);
|
|
181
184
|
this.prepareSvgElement(svgElement);
|
|
182
|
-
//
|
|
185
|
+
// 5. Process SVG fonts
|
|
183
186
|
processSvgFonts(svgElement, this.fontManager);
|
|
184
187
|
// Call lifecycle hook
|
|
185
188
|
hooks?.beforeSvgConvert?.(svgElement);
|
|
186
|
-
//
|
|
189
|
+
// 6. Create PDF document
|
|
187
190
|
const pdf = this.createPdfDocument(svgElement);
|
|
188
191
|
this.fontManager.setPdfInstance(pdf);
|
|
189
|
-
//
|
|
192
|
+
// 7. Draw SVG content to PDF
|
|
190
193
|
await this.renderSvgToPdf(svgElement, pdf);
|
|
191
194
|
// Call lifecycle hook
|
|
192
195
|
hooks?.beforePdfGenerate?.(pdf);
|
|
193
196
|
hooks?.beforePdfSave?.(pdf);
|
|
194
|
-
//
|
|
197
|
+
// 8. Save PDF
|
|
195
198
|
pdf.save(`${options.filename}.pdf`);
|
|
196
|
-
//
|
|
199
|
+
// 9. Clean up temporary elements
|
|
197
200
|
svgElement.remove();
|
|
198
201
|
this.fontManager.setPdfInstance(null);
|
|
199
202
|
}
|
|
@@ -261,6 +264,19 @@
|
|
|
261
264
|
height: pdf.internal.pageSize.getHeight(),
|
|
262
265
|
});
|
|
263
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Load resource
|
|
269
|
+
*/
|
|
270
|
+
async loadResource(element) {
|
|
271
|
+
this.resourceQueue = [];
|
|
272
|
+
const resources = element.querySelectorAll('img');
|
|
273
|
+
resources.forEach((resource) => {
|
|
274
|
+
this.resourceQueue.push(new Promise((resolve) => {
|
|
275
|
+
resource.onload = () => resolve(void 0);
|
|
276
|
+
}));
|
|
277
|
+
});
|
|
278
|
+
return Promise.allSettled(this.resourceQueue);
|
|
279
|
+
}
|
|
264
280
|
}
|
|
265
281
|
|
|
266
282
|
/**
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dom-to-vector-pdf",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "xzboss"
|
|
6
6
|
},
|
|
7
|
+
"license": "MIT",
|
|
7
8
|
"description": "Convert DOM elements to vector PDFs using jsPDF, dom-to-svg and svg2pdf.js",
|
|
8
9
|
"keywords": [
|
|
9
10
|
"dom",
|
|
@@ -14,7 +15,17 @@
|
|
|
14
15
|
"svg",
|
|
15
16
|
"dom-to-svg",
|
|
16
17
|
"svg2pdf.js",
|
|
17
|
-
"vector-pdf"
|
|
18
|
+
"vector-pdf",
|
|
19
|
+
"html-to-pdf",
|
|
20
|
+
"web-to-pdf",
|
|
21
|
+
"dom-conversion",
|
|
22
|
+
"vector-graphics",
|
|
23
|
+
"pdf-generation",
|
|
24
|
+
"browser-pdf",
|
|
25
|
+
"client-side-pdf",
|
|
26
|
+
"web-pdf",
|
|
27
|
+
"html2pdf",
|
|
28
|
+
"dom2pdf"
|
|
18
29
|
],
|
|
19
30
|
"main": "dist/index.js",
|
|
20
31
|
"module": "dist/index.esm.js",
|