@embedpdf/engines 1.5.0 → 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-LzpGkzoU.js → engine-B-RaFU77.js} +41 -27
- 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-B9w0JSiH.js → index-D92r9YXl.js} +3 -3
- package/dist/{index-B9w0JSiH.js.map → index-D92r9YXl.js.map} +1 -1
- package/dist/index-GEOxMZwR.cjs +2 -0
- package/dist/{index-2S2aYR--.cjs.map → index-GEOxMZwR.cjs.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -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/index.cjs +1 -1
- package/dist/lib/pdfium/index.js +2 -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-Bisy84hW.cjs +0 -2
- package/dist/engine-Bisy84hW.cjs.map +0 -1
- package/dist/engine-LzpGkzoU.js.map +0 -1
- package/dist/index-2S2aYR--.cjs +0 -2
|
@@ -91,14 +91,16 @@ function computeFormDrawParams(matrix, rect, pageSize, rotation) {
|
|
|
91
91
|
}
|
|
92
92
|
return { startX, startY, formsWidth, formsHeight, scaleX, scaleY };
|
|
93
93
|
}
|
|
94
|
+
const WasmPointer = (ptr) => ptr;
|
|
94
95
|
const DEFAULT_CONFIG = {
|
|
95
96
|
pageTtl: 5e3,
|
|
96
97
|
// 5 seconds
|
|
97
98
|
maxPagesPerDocument: 10
|
|
98
99
|
};
|
|
99
100
|
class PdfCache {
|
|
100
|
-
constructor(pdfium, config = {}) {
|
|
101
|
+
constructor(pdfium, memoryManager, config = {}) {
|
|
101
102
|
this.pdfium = pdfium;
|
|
103
|
+
this.memoryManager = memoryManager;
|
|
102
104
|
this.docs = /* @__PURE__ */ new Map();
|
|
103
105
|
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
104
106
|
}
|
|
@@ -106,7 +108,7 @@ class PdfCache {
|
|
|
106
108
|
setDocument(id, filePtr, docPtr) {
|
|
107
109
|
let ctx = this.docs.get(id);
|
|
108
110
|
if (!ctx) {
|
|
109
|
-
ctx = new DocumentContext(filePtr, docPtr, this.pdfium, this.config);
|
|
111
|
+
ctx = new DocumentContext(filePtr, docPtr, this.pdfium, this.memoryManager, this.config);
|
|
110
112
|
this.docs.set(id, ctx);
|
|
111
113
|
}
|
|
112
114
|
}
|
|
@@ -153,9 +155,10 @@ class PdfCache {
|
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
class DocumentContext {
|
|
156
|
-
constructor(filePtr, docPtr, pdfium, config) {
|
|
158
|
+
constructor(filePtr, docPtr, pdfium, memoryManager, config) {
|
|
157
159
|
this.filePtr = filePtr;
|
|
158
160
|
this.docPtr = docPtr;
|
|
161
|
+
this.memoryManager = memoryManager;
|
|
159
162
|
this.pageCache = new PageCache(pdfium, docPtr, config);
|
|
160
163
|
}
|
|
161
164
|
/** Main accessor for pages */
|
|
@@ -178,7 +181,7 @@ class DocumentContext {
|
|
|
178
181
|
dispose() {
|
|
179
182
|
this.pageCache.forceReleaseAll();
|
|
180
183
|
this.pageCache.pdf.FPDF_CloseDocument(this.docPtr);
|
|
181
|
-
this.
|
|
184
|
+
this.memoryManager.free(WasmPointer(this.filePtr));
|
|
182
185
|
}
|
|
183
186
|
}
|
|
184
187
|
class PageCache {
|
|
@@ -367,7 +370,6 @@ class PageContext {
|
|
|
367
370
|
if (this.disposed) throw new Error("PageContext already disposed");
|
|
368
371
|
}
|
|
369
372
|
}
|
|
370
|
-
const WasmPointer = (ptr) => ptr;
|
|
371
373
|
const MEMORY_LIMITS = {
|
|
372
374
|
/** Maximum total memory that can be allocated (2GB) */
|
|
373
375
|
MAX_TOTAL_MEMORY: 2 * 1024 * 1024 * 1024
|
|
@@ -514,10 +516,10 @@ class PdfiumEngine {
|
|
|
514
516
|
logger = new NoopLogger(),
|
|
515
517
|
imageDataConverter = browserImageDataToBlobConverter
|
|
516
518
|
} = options;
|
|
517
|
-
this.cache = new PdfCache(this.pdfiumModule);
|
|
518
519
|
this.logger = logger;
|
|
519
520
|
this.imageDataConverter = imageDataConverter;
|
|
520
521
|
this.memoryManager = new MemoryManager(this.pdfiumModule, this.logger);
|
|
522
|
+
this.cache = new PdfCache(this.pdfiumModule, this.memoryManager);
|
|
521
523
|
if (this.logger.isEnabled("debug")) {
|
|
522
524
|
this.memoryLeakCheckInterval = setInterval(() => {
|
|
523
525
|
this.memoryManager.checkLeaks();
|
|
@@ -587,11 +589,12 @@ class PdfiumEngine {
|
|
|
587
589
|
openDocumentUrl(file, options) {
|
|
588
590
|
const mode = (options == null ? void 0 : options.mode) ?? "auto";
|
|
589
591
|
const password = (options == null ? void 0 : options.password) ?? "";
|
|
592
|
+
const requestOptions = options == null ? void 0 : options.requestOptions;
|
|
590
593
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "openDocumentUrl called", file.url, mode);
|
|
591
594
|
const task = PdfTaskHelper.create();
|
|
592
595
|
(async () => {
|
|
593
596
|
try {
|
|
594
|
-
const fetchFullTask = await this.fetchFullAndOpen(file, password);
|
|
597
|
+
const fetchFullTask = await this.fetchFullAndOpen(file, password, requestOptions);
|
|
595
598
|
fetchFullTask.wait(
|
|
596
599
|
(doc) => task.resolve(doc),
|
|
597
600
|
(err) => task.reject(err.reason)
|
|
@@ -610,10 +613,14 @@ class PdfiumEngine {
|
|
|
610
613
|
* Check if the server supports range requests:
|
|
611
614
|
* Sends a HEAD request and sees if 'Accept-Ranges: bytes'.
|
|
612
615
|
*/
|
|
613
|
-
async checkRangeSupport(url) {
|
|
616
|
+
async checkRangeSupport(url, requestOptions) {
|
|
614
617
|
try {
|
|
615
618
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "checkRangeSupport", url);
|
|
616
|
-
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
|
+
});
|
|
617
624
|
const fileLength = headResponse.headers.get("Content-Length");
|
|
618
625
|
const acceptRanges = headResponse.headers.get("Accept-Ranges");
|
|
619
626
|
if (acceptRanges === "bytes") {
|
|
@@ -624,7 +631,8 @@ class PdfiumEngine {
|
|
|
624
631
|
};
|
|
625
632
|
}
|
|
626
633
|
const testResponse = await fetch(url, {
|
|
627
|
-
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
|
|
628
636
|
});
|
|
629
637
|
if (testResponse.status === 200) {
|
|
630
638
|
const content = await testResponse.arrayBuffer();
|
|
@@ -648,16 +656,18 @@ class PdfiumEngine {
|
|
|
648
656
|
* Fully fetch the file (using fetch) into an ArrayBuffer,
|
|
649
657
|
* then call openDocumentFromBuffer.
|
|
650
658
|
*/
|
|
651
|
-
async fetchFullAndOpen(file, password) {
|
|
659
|
+
async fetchFullAndOpen(file, password, requestOptions) {
|
|
652
660
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "fetchFullAndOpen", file.url);
|
|
653
|
-
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
|
+
});
|
|
654
665
|
if (!response.ok) {
|
|
655
666
|
throw new Error(`Could not fetch PDF: ${response.statusText}`);
|
|
656
667
|
}
|
|
657
668
|
const arrayBuf = await response.arrayBuffer();
|
|
658
669
|
const pdfFile = {
|
|
659
670
|
id: file.id,
|
|
660
|
-
name: file.name,
|
|
661
671
|
content: arrayBuf
|
|
662
672
|
};
|
|
663
673
|
return this.openDocumentBuffer(pdfFile, { password });
|
|
@@ -668,14 +678,22 @@ class PdfiumEngine {
|
|
|
668
678
|
* - We'll do a synchronous XHR read callback that pulls
|
|
669
679
|
* the desired byte ranges.
|
|
670
680
|
*/
|
|
671
|
-
async openDocumentWithRangeRequest(file, password, knownFileLength) {
|
|
681
|
+
async openDocumentWithRangeRequest(file, password, knownFileLength, requestOptions) {
|
|
672
682
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "openDocumentWithRangeRequest", file.url);
|
|
673
|
-
const fileLength = knownFileLength ?? (await this.retrieveFileLength(file.url)).fileLength;
|
|
683
|
+
const fileLength = knownFileLength ?? (await this.retrieveFileLength(file.url, requestOptions)).fileLength;
|
|
674
684
|
const callback = (offset, length) => {
|
|
675
685
|
const xhr = new XMLHttpRequest();
|
|
676
686
|
xhr.open("GET", file.url, false);
|
|
677
687
|
xhr.overrideMimeType("text/plain; charset=x-user-defined");
|
|
678
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
|
+
}
|
|
679
697
|
xhr.send(null);
|
|
680
698
|
if (xhr.status === 206 || xhr.status === 200) {
|
|
681
699
|
return this.convertResponseToUint8Array(xhr.responseText);
|
|
@@ -694,9 +712,13 @@ class PdfiumEngine {
|
|
|
694
712
|
/**
|
|
695
713
|
* Helper to do a HEAD request or partial GET to find file length.
|
|
696
714
|
*/
|
|
697
|
-
async retrieveFileLength(url) {
|
|
715
|
+
async retrieveFileLength(url, requestOptions) {
|
|
698
716
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "retrieveFileLength", url);
|
|
699
|
-
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
|
+
});
|
|
700
722
|
if (!resp.ok) {
|
|
701
723
|
throw new Error(`Failed HEAD request for file length: ${resp.statusText}`);
|
|
702
724
|
}
|
|
@@ -776,7 +798,6 @@ class PdfiumEngine {
|
|
|
776
798
|
this.memoryManager.free(sizePtr);
|
|
777
799
|
const pdfDoc = {
|
|
778
800
|
id: file.id,
|
|
779
|
-
name: file.name,
|
|
780
801
|
pageCount,
|
|
781
802
|
pages
|
|
782
803
|
};
|
|
@@ -860,7 +881,6 @@ class PdfiumEngine {
|
|
|
860
881
|
this.memoryManager.free(sizePtr);
|
|
861
882
|
const pdfDoc = {
|
|
862
883
|
id: file.id,
|
|
863
|
-
name: file.name,
|
|
864
884
|
pageCount,
|
|
865
885
|
pages
|
|
866
886
|
};
|
|
@@ -2379,13 +2399,7 @@ class PdfiumEngine {
|
|
|
2379
2399
|
this.logger.debug(LOG_SOURCE, LOG_CATEGORY, "closeDocument", doc);
|
|
2380
2400
|
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "Begin", doc.id);
|
|
2381
2401
|
const ctx = this.cache.getContext(doc.id);
|
|
2382
|
-
if (!ctx)
|
|
2383
|
-
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "End", doc.id);
|
|
2384
|
-
return PdfTaskHelper.reject({
|
|
2385
|
-
code: PdfErrorCode.DocNotOpen,
|
|
2386
|
-
message: "document does not open"
|
|
2387
|
-
});
|
|
2388
|
-
}
|
|
2402
|
+
if (!ctx) return PdfTaskHelper.resolve(true);
|
|
2389
2403
|
ctx.dispose();
|
|
2390
2404
|
this.logger.perf(LOG_SOURCE, LOG_CATEGORY, `CloseDocument`, "End", doc.id);
|
|
2391
2405
|
return PdfTaskHelper.resolve(true);
|
|
@@ -7076,4 +7090,4 @@ export {
|
|
|
7076
7090
|
isValidCustomKey as i,
|
|
7077
7091
|
readString as r
|
|
7078
7092
|
};
|
|
7079
|
-
//# sourceMappingURL=engine-
|
|
7093
|
+
//# sourceMappingURL=engine-B-RaFU77.js.map
|