@embedpdf/engines 1.4.1 → 2.0.0-next.0
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/dist/{engine-N_GwaBxA.js → engine-B-RaFU77.js} +98 -28
- package/dist/engine-B-RaFU77.js.map +1 -0
- package/dist/engine-CXnLqg_9.cjs +2 -0
- package/dist/engine-CXnLqg_9.cjs.map +1 -0
- package/dist/{index-DXUtjXMC.js → index-D92r9YXl.js} +3 -3
- package/dist/{index-DXUtjXMC.js.map → index-D92r9YXl.js.map} +1 -1
- package/dist/index-GEOxMZwR.cjs +2 -0
- package/dist/{index-CgsjfX9e.cjs.map → index-GEOxMZwR.cjs.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +3 -2
- package/dist/lib/converters/index.cjs.map +1 -1
- package/dist/lib/converters/index.js.map +1 -1
- package/dist/lib/pdfium/cache.d.ts +5 -2
- package/dist/lib/pdfium/engine.d.ts +1 -1
- package/dist/lib/pdfium/helper.d.ts +11 -0
- package/dist/lib/pdfium/index.cjs +1 -1
- package/dist/lib/pdfium/index.js +3 -2
- package/dist/lib/pdfium/web/direct-engine.cjs +1 -1
- package/dist/lib/pdfium/web/direct-engine.cjs.map +1 -1
- package/dist/lib/pdfium/web/direct-engine.js +1 -1
- package/dist/lib/pdfium/web/direct-engine.js.map +1 -1
- package/dist/lib/pdfium/web/worker-engine.cjs +1 -1
- package/dist/lib/pdfium/web/worker-engine.js +1 -1
- package/dist/lib/webworker/engine.cjs +1 -1
- package/dist/lib/webworker/engine.cjs.map +1 -1
- package/dist/lib/webworker/engine.js +1 -1
- package/dist/lib/webworker/engine.js.map +1 -1
- package/dist/preact/index.cjs +1 -1
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.js +1 -1
- package/dist/preact/index.js.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/svelte/index.cjs +1 -1
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.js.map +1 -1
- package/dist/vue/components/pdf-engine-provider.vue.d.ts +3 -2
- package/dist/vue/index.cjs +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.js +1 -1
- package/dist/vue/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/engine-N_GwaBxA.js.map +0 -1
- package/dist/engine-Z4iXwySK.cjs +0 -2
- package/dist/engine-Z4iXwySK.cjs.map +0 -1
- package/dist/index-CgsjfX9e.cjs +0 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStampFit, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE,
|
|
1
|
+
import { Rotation, NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStampFit, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfZoomMode, PdfActionType, MatchFlag } from "@embedpdf/models";
|
|
2
2
|
function readString(wasmModule, readChars, parseChars, defaultLength = 100) {
|
|
3
3
|
let buffer = wasmModule.wasmExports.malloc(defaultLength);
|
|
4
4
|
for (let i = 0; i < defaultLength; i++) {
|
|
@@ -53,14 +53,54 @@ function isValidCustomKey(key) {
|
|
|
53
53
|
}
|
|
54
54
|
return true;
|
|
55
55
|
}
|
|
56
|
+
function computeFormDrawParams(matrix, rect, pageSize, rotation) {
|
|
57
|
+
const rectLeft = rect.origin.x;
|
|
58
|
+
const rectBottom = rect.origin.y;
|
|
59
|
+
const rectRight = rectLeft + rect.size.width;
|
|
60
|
+
const rectTop = rectBottom + rect.size.height;
|
|
61
|
+
const pageWidth = pageSize.width;
|
|
62
|
+
const pageHeight = pageSize.height;
|
|
63
|
+
const scaleX = Math.hypot(matrix.a, matrix.b);
|
|
64
|
+
const scaleY = Math.hypot(matrix.c, matrix.d);
|
|
65
|
+
const swap = (rotation & 1) === 1;
|
|
66
|
+
const formsWidth = swap ? Math.max(1, Math.round(pageHeight * scaleX)) : Math.max(1, Math.round(pageWidth * scaleX));
|
|
67
|
+
const formsHeight = swap ? Math.max(1, Math.round(pageWidth * scaleY)) : Math.max(1, Math.round(pageHeight * scaleY));
|
|
68
|
+
let startX;
|
|
69
|
+
let startY;
|
|
70
|
+
switch (rotation) {
|
|
71
|
+
case Rotation.Degree0:
|
|
72
|
+
startX = -Math.round(rectLeft * scaleX);
|
|
73
|
+
startY = -Math.round(rectBottom * scaleY);
|
|
74
|
+
break;
|
|
75
|
+
case Rotation.Degree90:
|
|
76
|
+
startX = Math.round((rectTop - pageHeight) * scaleX);
|
|
77
|
+
startY = -Math.round(rectLeft * scaleY);
|
|
78
|
+
break;
|
|
79
|
+
case Rotation.Degree180:
|
|
80
|
+
startX = Math.round((rectRight - pageWidth) * scaleX);
|
|
81
|
+
startY = Math.round((rectTop - pageHeight) * scaleY);
|
|
82
|
+
break;
|
|
83
|
+
case Rotation.Degree270:
|
|
84
|
+
startX = -Math.round(rectBottom * scaleX);
|
|
85
|
+
startY = Math.round((rectRight - pageWidth) * scaleY);
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
startX = -Math.round(rectLeft * scaleX);
|
|
89
|
+
startY = -Math.round(rectBottom * scaleY);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
return { startX, startY, formsWidth, formsHeight, scaleX, scaleY };
|
|
93
|
+
}
|
|
94
|
+
const WasmPointer = (ptr) => ptr;
|
|
56
95
|
const DEFAULT_CONFIG = {
|
|
57
96
|
pageTtl: 5e3,
|
|
58
97
|
// 5 seconds
|
|
59
98
|
maxPagesPerDocument: 10
|
|
60
99
|
};
|
|
61
100
|
class PdfCache {
|
|
62
|
-
constructor(pdfium, config = {}) {
|
|
101
|
+
constructor(pdfium, memoryManager, config = {}) {
|
|
63
102
|
this.pdfium = pdfium;
|
|
103
|
+
this.memoryManager = memoryManager;
|
|
64
104
|
this.docs = /* @__PURE__ */ new Map();
|
|
65
105
|
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
66
106
|
}
|
|
@@ -68,7 +108,7 @@ class PdfCache {
|
|
|
68
108
|
setDocument(id, filePtr, docPtr) {
|
|
69
109
|
let ctx = this.docs.get(id);
|
|
70
110
|
if (!ctx) {
|
|
71
|
-
ctx = new DocumentContext(filePtr, docPtr, this.pdfium, this.config);
|
|
111
|
+
ctx = new DocumentContext(filePtr, docPtr, this.pdfium, this.memoryManager, this.config);
|
|
72
112
|
this.docs.set(id, ctx);
|
|
73
113
|
}
|
|
74
114
|
}
|
|
@@ -115,9 +155,10 @@ class PdfCache {
|
|
|
115
155
|
}
|
|
116
156
|
}
|
|
117
157
|
class DocumentContext {
|
|
118
|
-
constructor(filePtr, docPtr, pdfium, config) {
|
|
158
|
+
constructor(filePtr, docPtr, pdfium, memoryManager, config) {
|
|
119
159
|
this.filePtr = filePtr;
|
|
120
160
|
this.docPtr = docPtr;
|
|
161
|
+
this.memoryManager = memoryManager;
|
|
121
162
|
this.pageCache = new PageCache(pdfium, docPtr, config);
|
|
122
163
|
}
|
|
123
164
|
/** Main accessor for pages */
|
|
@@ -140,7 +181,7 @@ class DocumentContext {
|
|
|
140
181
|
dispose() {
|
|
141
182
|
this.pageCache.forceReleaseAll();
|
|
142
183
|
this.pageCache.pdf.FPDF_CloseDocument(this.docPtr);
|
|
143
|
-
this.
|
|
184
|
+
this.memoryManager.free(WasmPointer(this.filePtr));
|
|
144
185
|
}
|
|
145
186
|
}
|
|
146
187
|
class PageCache {
|
|
@@ -329,7 +370,6 @@ class PageContext {
|
|
|
329
370
|
if (this.disposed) throw new Error("PageContext already disposed");
|
|
330
371
|
}
|
|
331
372
|
}
|
|
332
|
-
const WasmPointer = (ptr) => ptr;
|
|
333
373
|
const MEMORY_LIMITS = {
|
|
334
374
|
/** Maximum total memory that can be allocated (2GB) */
|
|
335
375
|
MAX_TOTAL_MEMORY: 2 * 1024 * 1024 * 1024
|
|
@@ -476,10 +516,10 @@ class PdfiumEngine {
|
|
|
476
516
|
logger = new NoopLogger(),
|
|
477
517
|
imageDataConverter = browserImageDataToBlobConverter
|
|
478
518
|
} = options;
|
|
479
|
-
this.cache = new PdfCache(this.pdfiumModule);
|
|
480
519
|
this.logger = logger;
|
|
481
520
|
this.imageDataConverter = imageDataConverter;
|
|
482
521
|
this.memoryManager = new MemoryManager(this.pdfiumModule, this.logger);
|
|
522
|
+
this.cache = new PdfCache(this.pdfiumModule, this.memoryManager);
|
|
483
523
|
if (this.logger.isEnabled("debug")) {
|
|
484
524
|
this.memoryLeakCheckInterval = setInterval(() => {
|
|
485
525
|
this.memoryManager.checkLeaks();
|
|
@@ -549,11 +589,12 @@ class PdfiumEngine {
|
|
|
549
589
|
openDocumentUrl(file, options) {
|
|
550
590
|
const mode = (options == null ? void 0 : options.mode) ?? "auto";
|
|
551
591
|
const password = (options == null ? void 0 : options.password) ?? "";
|
|
592
|
+
const requestOptions = options == null ? void 0 : options.requestOptions;
|
|
552
593
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "openDocumentUrl called", file.url, mode);
|
|
553
594
|
const task = PdfTaskHelper.create();
|
|
554
595
|
(async () => {
|
|
555
596
|
try {
|
|
556
|
-
const fetchFullTask = await this.fetchFullAndOpen(file, password);
|
|
597
|
+
const fetchFullTask = await this.fetchFullAndOpen(file, password, requestOptions);
|
|
557
598
|
fetchFullTask.wait(
|
|
558
599
|
(doc) => task.resolve(doc),
|
|
559
600
|
(err) => task.reject(err.reason)
|
|
@@ -572,10 +613,14 @@ class PdfiumEngine {
|
|
|
572
613
|
* Check if the server supports range requests:
|
|
573
614
|
* Sends a HEAD request and sees if 'Accept-Ranges: bytes'.
|
|
574
615
|
*/
|
|
575
|
-
async checkRangeSupport(url) {
|
|
616
|
+
async checkRangeSupport(url, requestOptions) {
|
|
576
617
|
try {
|
|
577
618
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "checkRangeSupport", url);
|
|
578
|
-
const headResponse = await fetch(url, {
|
|
619
|
+
const headResponse = await fetch(url, {
|
|
620
|
+
method: "HEAD",
|
|
621
|
+
headers: requestOptions == null ? void 0 : requestOptions.headers,
|
|
622
|
+
credentials: requestOptions == null ? void 0 : requestOptions.credentials
|
|
623
|
+
});
|
|
579
624
|
const fileLength = headResponse.headers.get("Content-Length");
|
|
580
625
|
const acceptRanges = headResponse.headers.get("Accept-Ranges");
|
|
581
626
|
if (acceptRanges === "bytes") {
|
|
@@ -586,7 +631,8 @@ class PdfiumEngine {
|
|
|
586
631
|
};
|
|
587
632
|
}
|
|
588
633
|
const testResponse = await fetch(url, {
|
|
589
|
-
headers: { Range: "bytes=0-1" }
|
|
634
|
+
headers: { ...requestOptions == null ? void 0 : requestOptions.headers, Range: "bytes=0-1" },
|
|
635
|
+
credentials: requestOptions == null ? void 0 : requestOptions.credentials
|
|
590
636
|
});
|
|
591
637
|
if (testResponse.status === 200) {
|
|
592
638
|
const content = await testResponse.arrayBuffer();
|
|
@@ -610,16 +656,18 @@ class PdfiumEngine {
|
|
|
610
656
|
* Fully fetch the file (using fetch) into an ArrayBuffer,
|
|
611
657
|
* then call openDocumentFromBuffer.
|
|
612
658
|
*/
|
|
613
|
-
async fetchFullAndOpen(file, password) {
|
|
659
|
+
async fetchFullAndOpen(file, password, requestOptions) {
|
|
614
660
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "fetchFullAndOpen", file.url);
|
|
615
|
-
const response = await fetch(file.url
|
|
661
|
+
const response = await fetch(file.url, {
|
|
662
|
+
headers: requestOptions == null ? void 0 : requestOptions.headers,
|
|
663
|
+
credentials: requestOptions == null ? void 0 : requestOptions.credentials
|
|
664
|
+
});
|
|
616
665
|
if (!response.ok) {
|
|
617
666
|
throw new Error(`Could not fetch PDF: ${response.statusText}`);
|
|
618
667
|
}
|
|
619
668
|
const arrayBuf = await response.arrayBuffer();
|
|
620
669
|
const pdfFile = {
|
|
621
670
|
id: file.id,
|
|
622
|
-
name: file.name,
|
|
623
671
|
content: arrayBuf
|
|
624
672
|
};
|
|
625
673
|
return this.openDocumentBuffer(pdfFile, { password });
|
|
@@ -630,14 +678,22 @@ class PdfiumEngine {
|
|
|
630
678
|
* - We'll do a synchronous XHR read callback that pulls
|
|
631
679
|
* the desired byte ranges.
|
|
632
680
|
*/
|
|
633
|
-
async openDocumentWithRangeRequest(file, password, knownFileLength) {
|
|
681
|
+
async openDocumentWithRangeRequest(file, password, knownFileLength, requestOptions) {
|
|
634
682
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "openDocumentWithRangeRequest", file.url);
|
|
635
|
-
const fileLength = knownFileLength ?? (await this.retrieveFileLength(file.url)).fileLength;
|
|
683
|
+
const fileLength = knownFileLength ?? (await this.retrieveFileLength(file.url, requestOptions)).fileLength;
|
|
636
684
|
const callback = (offset, length) => {
|
|
637
685
|
const xhr = new XMLHttpRequest();
|
|
638
686
|
xhr.open("GET", file.url, false);
|
|
639
687
|
xhr.overrideMimeType("text/plain; charset=x-user-defined");
|
|
640
688
|
xhr.setRequestHeader("Range", `bytes=${offset}-${offset + length - 1}`);
|
|
689
|
+
if (requestOptions == null ? void 0 : requestOptions.headers) {
|
|
690
|
+
Object.entries(requestOptions.headers).forEach(([key, value]) => {
|
|
691
|
+
xhr.setRequestHeader(key, value);
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
if (requestOptions == null ? void 0 : requestOptions.credentials) {
|
|
695
|
+
xhr.withCredentials = requestOptions.credentials === "include";
|
|
696
|
+
}
|
|
641
697
|
xhr.send(null);
|
|
642
698
|
if (xhr.status === 206 || xhr.status === 200) {
|
|
643
699
|
return this.convertResponseToUint8Array(xhr.responseText);
|
|
@@ -656,9 +712,13 @@ class PdfiumEngine {
|
|
|
656
712
|
/**
|
|
657
713
|
* Helper to do a HEAD request or partial GET to find file length.
|
|
658
714
|
*/
|
|
659
|
-
async retrieveFileLength(url) {
|
|
715
|
+
async retrieveFileLength(url, requestOptions) {
|
|
660
716
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "retrieveFileLength", url);
|
|
661
|
-
const resp = await fetch(url, {
|
|
717
|
+
const resp = await fetch(url, {
|
|
718
|
+
method: "HEAD",
|
|
719
|
+
headers: requestOptions == null ? void 0 : requestOptions.headers,
|
|
720
|
+
credentials: requestOptions == null ? void 0 : requestOptions.credentials
|
|
721
|
+
});
|
|
662
722
|
if (!resp.ok) {
|
|
663
723
|
throw new Error(`Failed HEAD request for file length: ${resp.statusText}`);
|
|
664
724
|
}
|
|
@@ -738,7 +798,6 @@ class PdfiumEngine {
|
|
|
738
798
|
this.memoryManager.free(sizePtr);
|
|
739
799
|
const pdfDoc = {
|
|
740
800
|
id: file.id,
|
|
741
|
-
name: file.name,
|
|
742
801
|
pageCount,
|
|
743
802
|
pages
|
|
744
803
|
};
|
|
@@ -822,7 +881,6 @@ class PdfiumEngine {
|
|
|
822
881
|
this.memoryManager.free(sizePtr);
|
|
823
882
|
const pdfDoc = {
|
|
824
883
|
id: file.id,
|
|
825
|
-
name: file.name,
|
|
826
884
|
pageCount,
|
|
827
885
|
pages
|
|
828
886
|
};
|
|
@@ -2341,13 +2399,7 @@ class PdfiumEngine {
|
|
|
2341
2399
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "closeDocument", doc);
|
|
2342
2400
|
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "Begin", doc.id);
|
|
2343
2401
|
const ctx = this.cache.getContext(doc.id);
|
|
2344
|
-
if (!ctx)
|
|
2345
|
-
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "End", doc.id);
|
|
2346
|
-
return PdfTaskHelper.reject({
|
|
2347
|
-
code: PdfErrorCode.DocNotOpen,
|
|
2348
|
-
message: "document does not open"
|
|
2349
|
-
});
|
|
2350
|
-
}
|
|
2402
|
+
if (!ctx) return PdfTaskHelper.resolve(true);
|
|
2351
2403
|
ctx.dispose();
|
|
2352
2404
|
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "End", doc.id);
|
|
2353
2405
|
return PdfTaskHelper.resolve(true);
|
|
@@ -5951,6 +6003,8 @@ class PdfiumEngine {
|
|
|
5951
6003
|
const stride = wDev * 4;
|
|
5952
6004
|
const bytes = stride * hDev;
|
|
5953
6005
|
const pageCtx = ctx.acquirePage(page.index);
|
|
6006
|
+
const shouldRenderForms = (options == null ? void 0 : options.withForms) ?? false;
|
|
6007
|
+
const formHandle = shouldRenderForms ? pageCtx.getFormHandle() : void 0;
|
|
5954
6008
|
const heapPtr = this.memoryManager.malloc(bytes);
|
|
5955
6009
|
const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
|
|
5956
6010
|
wDev,
|
|
@@ -5977,6 +6031,21 @@ class PdfiumEngine {
|
|
|
5977
6031
|
clipPtr,
|
|
5978
6032
|
flags
|
|
5979
6033
|
);
|
|
6034
|
+
if (formHandle !== void 0) {
|
|
6035
|
+
const formParams = computeFormDrawParams(M, rect, page.size, rotation);
|
|
6036
|
+
const { startX, startY, formsWidth, formsHeight, scaleX, scaleY } = formParams;
|
|
6037
|
+
this.pdfiumModule.FPDF_FFLDraw(
|
|
6038
|
+
formHandle,
|
|
6039
|
+
bitmapPtr,
|
|
6040
|
+
pageCtx.pagePtr,
|
|
6041
|
+
startX,
|
|
6042
|
+
startY,
|
|
6043
|
+
formsWidth,
|
|
6044
|
+
formsHeight,
|
|
6045
|
+
rotation,
|
|
6046
|
+
flags
|
|
6047
|
+
);
|
|
6048
|
+
}
|
|
5980
6049
|
} finally {
|
|
5981
6050
|
pageCtx.release();
|
|
5982
6051
|
this.memoryManager.free(mPtr);
|
|
@@ -7017,7 +7086,8 @@ export {
|
|
|
7017
7086
|
PdfiumEngine as a,
|
|
7018
7087
|
browserImageDataToBlobConverter as b,
|
|
7019
7088
|
readArrayBuffer as c,
|
|
7089
|
+
computeFormDrawParams as d,
|
|
7020
7090
|
isValidCustomKey as i,
|
|
7021
7091
|
readString as r
|
|
7022
7092
|
};
|
|
7023
|
-
//# sourceMappingURL=engine-
|
|
7093
|
+
//# sourceMappingURL=engine-B-RaFU77.js.map
|