@open-file-viewer/core 0.1.9 → 0.1.10
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 +17 -0
- package/dist/index.cjs +41 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +41 -19
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -106,6 +106,23 @@ viewer.resize();
|
|
|
106
106
|
viewer.destroy();
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
### PDF loading compatibility
|
|
110
|
+
|
|
111
|
+
If PDF preview falls back in Umi Max, utoo pack or similar build environments and the console shows
|
|
112
|
+
`Cannot set properties of undefined (setting 'onPull')` from pdf.js, enable `useFetchData`. It fetches
|
|
113
|
+
the PDF bytes on the main thread and then passes `data` to pdf.js, avoiding the worker network stream
|
|
114
|
+
path that can break in those bundlers:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
pdfPlugin({
|
|
118
|
+
workerSrc: pdfWorkerSrc,
|
|
119
|
+
useFetchData: true
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
This keeps compatibility at the cost of holding one extra copy of the PDF in memory, so use it only
|
|
124
|
+
for affected environments.
|
|
125
|
+
|
|
109
126
|
## CAD Customization
|
|
110
127
|
|
|
111
128
|
`cadPlugin()` has two CAD preview layers:
|
package/dist/index.cjs
CHANGED
|
@@ -4576,14 +4576,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4576
4576
|
scroller.className = "ofv-pdf ofv-pdf-pages";
|
|
4577
4577
|
viewer.append(summary, scroller);
|
|
4578
4578
|
options.viewport.append(viewer);
|
|
4579
|
-
const
|
|
4580
|
-
url: options.fileUrl,
|
|
4581
|
-
cMapUrl: options.cMapUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/cmaps/`,
|
|
4582
|
-
cMapPacked: options.cMapPacked ?? true,
|
|
4583
|
-
standardFontDataUrl: options.standardFontDataUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/standard_fonts/`,
|
|
4584
|
-
useSystemFonts: options.useSystemFonts ?? true
|
|
4585
|
-
});
|
|
4586
|
-
const doc = await documentTask.promise.catch((error) => {
|
|
4579
|
+
const showDocumentFallback = (error) => {
|
|
4587
4580
|
viewer.remove();
|
|
4588
4581
|
options.viewport.classList.add("ofv-center");
|
|
4589
4582
|
const fileLike = {
|
|
@@ -4600,8 +4593,29 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4600
4593
|
action: options.encryptedAction || "\u4E0B\u8F7D PDF"
|
|
4601
4594
|
}) : createPdfFallback(options.fileName, options.fileUrl, normalizePdfError(error), options.fallbackTitle);
|
|
4602
4595
|
options.viewport.append(fallback);
|
|
4603
|
-
|
|
4604
|
-
|
|
4596
|
+
};
|
|
4597
|
+
let documentTask;
|
|
4598
|
+
let doc;
|
|
4599
|
+
try {
|
|
4600
|
+
const pdfData = options.useFetchData ? await loadPdfData(options.fileUrl) : void 0;
|
|
4601
|
+
documentTask = pdf.getDocument({
|
|
4602
|
+
...pdfData ? { data: pdfData } : { url: options.fileUrl },
|
|
4603
|
+
cMapUrl: options.cMapUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/cmaps/`,
|
|
4604
|
+
cMapPacked: options.cMapPacked ?? true,
|
|
4605
|
+
standardFontDataUrl: options.standardFontDataUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/standard_fonts/`,
|
|
4606
|
+
useSystemFonts: options.useSystemFonts ?? true,
|
|
4607
|
+
disableStream: options.disableStream,
|
|
4608
|
+
disableAutoFetch: options.disableAutoFetch,
|
|
4609
|
+
disableRange: options.disableRange,
|
|
4610
|
+
rangeChunkSize: options.rangeChunkSize
|
|
4611
|
+
});
|
|
4612
|
+
doc = await documentTask.promise.catch((error) => {
|
|
4613
|
+
showDocumentFallback(error);
|
|
4614
|
+
return void 0;
|
|
4615
|
+
});
|
|
4616
|
+
} catch (error) {
|
|
4617
|
+
showDocumentFallback(error);
|
|
4618
|
+
}
|
|
4605
4619
|
if (!doc) {
|
|
4606
4620
|
return {
|
|
4607
4621
|
canCommand() {
|
|
@@ -4614,17 +4628,18 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4614
4628
|
},
|
|
4615
4629
|
destroy() {
|
|
4616
4630
|
options.viewport.classList.remove("ofv-center");
|
|
4617
|
-
documentTask
|
|
4631
|
+
documentTask?.destroy?.();
|
|
4618
4632
|
if (options.revokeUrlOnDestroy) {
|
|
4619
4633
|
revokeObjectUrl(options.fileUrl, Boolean(options.isExternal));
|
|
4620
4634
|
}
|
|
4621
4635
|
}
|
|
4622
4636
|
};
|
|
4623
4637
|
}
|
|
4638
|
+
const pdfDocument = doc;
|
|
4624
4639
|
const pagesMeta = [];
|
|
4625
|
-
for (let pageNumber = 1; pageNumber <=
|
|
4640
|
+
for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber += 1) {
|
|
4626
4641
|
try {
|
|
4627
|
-
const page = await
|
|
4642
|
+
const page = await pdfDocument.getPage(pageNumber);
|
|
4628
4643
|
const baseViewport = page.getViewport({ scale: 1 });
|
|
4629
4644
|
pagesMeta.push({
|
|
4630
4645
|
width: baseViewport.width,
|
|
@@ -4640,7 +4655,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4640
4655
|
let zoomFactor = 1;
|
|
4641
4656
|
let rotation = 0;
|
|
4642
4657
|
const updateSummary = () => {
|
|
4643
|
-
renderPdfSummary(summary,
|
|
4658
|
+
renderPdfSummary(summary, pdfDocument.numPages, pagesMeta, options.fit, zoomFactor);
|
|
4644
4659
|
options.toolbar?.setZoom(zoomFactor);
|
|
4645
4660
|
};
|
|
4646
4661
|
const clearPage = (pageIdx) => {
|
|
@@ -4663,7 +4678,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4663
4678
|
if (!state || state.rendered) return;
|
|
4664
4679
|
state.rendered = true;
|
|
4665
4680
|
try {
|
|
4666
|
-
const page = await
|
|
4681
|
+
const page = await pdfDocument.getPage(pageIdx + 1);
|
|
4667
4682
|
const meta = pagesMeta[pageIdx];
|
|
4668
4683
|
const scale = options.fit === "actual" ? zoomFactor : Math.max(0.05, Math.min(5, getPdfAvailableWidth(size.width) / rotatedPdfWidth(meta, rotation) * zoomFactor));
|
|
4669
4684
|
const viewport = page.getViewport({ scale, rotation });
|
|
@@ -4750,7 +4765,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4750
4765
|
if (!state.rendered) {
|
|
4751
4766
|
void renderPage(pageIdx, size);
|
|
4752
4767
|
}
|
|
4753
|
-
} else if (state.rendered &&
|
|
4768
|
+
} else if (state.rendered && pdfDocument.numPages > 8) {
|
|
4754
4769
|
clearPage(pageIdx);
|
|
4755
4770
|
}
|
|
4756
4771
|
});
|
|
@@ -4761,7 +4776,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4761
4776
|
}
|
|
4762
4777
|
);
|
|
4763
4778
|
}
|
|
4764
|
-
for (let i = 0; i <
|
|
4779
|
+
for (let i = 0; i < pdfDocument.numPages; i++) {
|
|
4765
4780
|
const meta = pagesMeta[i];
|
|
4766
4781
|
const rotatedWidth = rotatedPdfWidth(meta, rotation);
|
|
4767
4782
|
const rotatedHeight = rotatedPdfHeight(meta, rotation);
|
|
@@ -4789,7 +4804,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4789
4804
|
}
|
|
4790
4805
|
if (observer) {
|
|
4791
4806
|
window.setTimeout(() => {
|
|
4792
|
-
const eagerPages =
|
|
4807
|
+
const eagerPages = pdfDocument.numPages > 8 ? 2 : pdfDocument.numPages;
|
|
4793
4808
|
for (let i = 0; i < eagerPages; i++) {
|
|
4794
4809
|
void renderPage(i, size);
|
|
4795
4810
|
}
|
|
@@ -4846,10 +4861,10 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4846
4861
|
}
|
|
4847
4862
|
});
|
|
4848
4863
|
pageStates.length = 0;
|
|
4864
|
+
void pdfDocument.destroy?.();
|
|
4849
4865
|
if (options.revokeUrlOnDestroy) {
|
|
4850
4866
|
revokeObjectUrl(options.fileUrl, Boolean(options.isExternal));
|
|
4851
4867
|
}
|
|
4852
|
-
void doc.destroy();
|
|
4853
4868
|
}
|
|
4854
4869
|
};
|
|
4855
4870
|
}
|
|
@@ -4912,6 +4927,13 @@ function normalizePdfOptions(options) {
|
|
|
4912
4927
|
}
|
|
4913
4928
|
return options;
|
|
4914
4929
|
}
|
|
4930
|
+
async function loadPdfData(url) {
|
|
4931
|
+
const response = await fetch(url);
|
|
4932
|
+
if (!response.ok) {
|
|
4933
|
+
throw new Error(`Failed to load PDF data: ${response.status} ${response.statusText}`);
|
|
4934
|
+
}
|
|
4935
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
4936
|
+
}
|
|
4915
4937
|
function isCanvasVisuallyBlank(canvas, context) {
|
|
4916
4938
|
if (canvas.width === 0 || canvas.height === 0 || typeof context.getImageData !== "function") {
|
|
4917
4939
|
return false;
|