@yudaocode/univer-pdfs-ui 1.6.213 → 1.6.215
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/lib/cjs/index.js +56 -8
- package/lib/cjs/index.js.map +1 -1
- package/lib/es/index.js +56 -8
- package/lib/es/index.js.map +1 -1
- package/package.json +3 -3
package/lib/cjs/index.js
CHANGED
|
@@ -1082,19 +1082,53 @@ function loadPdfDocument(dataUri, overrideAssets) {
|
|
|
1082
1082
|
const effectiveAssets = overrideAssets ?? configuredAssets ?? resolveGlobalPdfjsAssets();
|
|
1083
1083
|
ensurePdfjsWorker(effectiveAssets);
|
|
1084
1084
|
const bytes = dataUriToUint8Array(dataUri);
|
|
1085
|
-
const cacheKey = (0, import_univer_pdfs2.computePdfPayloadDigest)(bytes)
|
|
1085
|
+
const cacheKey = `${(0, import_univer_pdfs2.computePdfPayloadDigest)(bytes)}|${effectiveAssets?.cMapUrl ?? ""}`;
|
|
1086
1086
|
const existing = docCache.get(cacheKey);
|
|
1087
|
-
if (existing) {
|
|
1087
|
+
if (existing && existing.cMapFailures.length === 0) {
|
|
1088
1088
|
touchCacheEntry(cacheKey, existing);
|
|
1089
|
-
return existing.promise;
|
|
1089
|
+
return { promise: existing.promise, entry: existing, cacheKey };
|
|
1090
1090
|
}
|
|
1091
|
+
const cMapFailures = [];
|
|
1091
1092
|
const docInitParams = {
|
|
1092
1093
|
data: bytes.slice(),
|
|
1093
1094
|
isEvalSupported: false
|
|
1094
1095
|
};
|
|
1095
1096
|
if (effectiveAssets?.cMapUrl) {
|
|
1096
|
-
|
|
1097
|
-
|
|
1097
|
+
const cMapUrl = effectiveAssets.cMapUrl;
|
|
1098
|
+
const cMapPacked = effectiveAssets.cMapPacked ?? true;
|
|
1099
|
+
const normalizedBase = cMapUrl.endsWith("/") ? cMapUrl : `${cMapUrl}/`;
|
|
1100
|
+
docInitParams.cMapUrl = normalizedBase;
|
|
1101
|
+
docInitParams.cMapPacked = cMapPacked;
|
|
1102
|
+
docInitParams.CMapReaderFactory = class BgTrackedCMapReaderFactory {
|
|
1103
|
+
constructor(params) {
|
|
1104
|
+
this.baseUrl = params?.baseUrl ?? normalizedBase;
|
|
1105
|
+
this.isCompressed = params?.isCompressed ?? cMapPacked;
|
|
1106
|
+
}
|
|
1107
|
+
async fetch({ name }) {
|
|
1108
|
+
const base = this.baseUrl.endsWith("/") ? this.baseUrl : `${this.baseUrl}/`;
|
|
1109
|
+
const url = `${base}${name}${this.isCompressed ? ".bcmap" : ""}`;
|
|
1110
|
+
let resp;
|
|
1111
|
+
try {
|
|
1112
|
+
resp = await fetch(url);
|
|
1113
|
+
} catch (err) {
|
|
1114
|
+
const reason = err?.message || String(err);
|
|
1115
|
+
cMapFailures.push({ name, url, reason });
|
|
1116
|
+
throw new Error(`Unable to load CMap at ${url}: ${reason}`);
|
|
1117
|
+
}
|
|
1118
|
+
if (!resp.ok) {
|
|
1119
|
+
const reason = `HTTP ${resp.status}`;
|
|
1120
|
+
cMapFailures.push({ name, url, reason });
|
|
1121
|
+
throw new Error(`Unable to load CMap at ${url}: ${reason}`);
|
|
1122
|
+
}
|
|
1123
|
+
const buf = new Uint8Array(await resp.arrayBuffer());
|
|
1124
|
+
if (buf.byteLength === 0) {
|
|
1125
|
+
const reason = "Empty CMap payload";
|
|
1126
|
+
cMapFailures.push({ name, url, reason });
|
|
1127
|
+
throw new Error(`Unable to load CMap at ${url}: ${reason}`);
|
|
1128
|
+
}
|
|
1129
|
+
return { cMapData: buf, isCompressed: this.isCompressed };
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1098
1132
|
}
|
|
1099
1133
|
if (effectiveAssets?.standardFontDataUrl) {
|
|
1100
1134
|
docInitParams.standardFontDataUrl = effectiveAssets.standardFontDataUrl;
|
|
@@ -1104,6 +1138,7 @@ function loadPdfDocument(dataUri, overrideAssets) {
|
|
|
1104
1138
|
}
|
|
1105
1139
|
const loadingTask = pdfjsLib.getDocument(docInitParams);
|
|
1106
1140
|
const entry = {
|
|
1141
|
+
cMapFailures,
|
|
1107
1142
|
promise: loadingTask.promise.then((doc) => {
|
|
1108
1143
|
entry.resolvedDoc = doc;
|
|
1109
1144
|
return doc;
|
|
@@ -1116,10 +1151,11 @@ function loadPdfDocument(dataUri, overrideAssets) {
|
|
|
1116
1151
|
};
|
|
1117
1152
|
docCache.set(cacheKey, entry);
|
|
1118
1153
|
evictOverflowCacheEntries();
|
|
1119
|
-
return entry.promise;
|
|
1154
|
+
return { promise: entry.promise, entry, cacheKey };
|
|
1120
1155
|
}
|
|
1121
1156
|
async function inspectSourcePdfPages(dataUri, assets) {
|
|
1122
|
-
const
|
|
1157
|
+
const { promise: docPromise } = loadPdfDocument(dataUri, assets);
|
|
1158
|
+
const doc = await docPromise;
|
|
1123
1159
|
const sizes = [];
|
|
1124
1160
|
for (let i = 1; i <= doc.numPages; i++) {
|
|
1125
1161
|
const page = await doc.getPage(i);
|
|
@@ -1151,7 +1187,8 @@ async function renderSourcePdfPageToCanvas(canvas, dataUri, pageNumber, targetWi
|
|
|
1151
1187
|
} catch {
|
|
1152
1188
|
}
|
|
1153
1189
|
}
|
|
1154
|
-
const
|
|
1190
|
+
const { promise: docPromise, entry: docEntry, cacheKey } = loadPdfDocument(dataUri, assets);
|
|
1191
|
+
const doc = await docPromise;
|
|
1155
1192
|
if (activeCanvasRenderTasks.has(canvas)) {
|
|
1156
1193
|
return;
|
|
1157
1194
|
}
|
|
@@ -1172,6 +1209,17 @@ async function renderSourcePdfPageToCanvas(canvas, dataUri, pageNumber, targetWi
|
|
|
1172
1209
|
activeCanvasRenderTasks.set(canvas, renderTask);
|
|
1173
1210
|
try {
|
|
1174
1211
|
await renderTask.promise;
|
|
1212
|
+
if (docEntry.cMapFailures.length > 0) {
|
|
1213
|
+
const f = docEntry.cMapFailures[0];
|
|
1214
|
+
if (docCache.get(cacheKey) === docEntry) {
|
|
1215
|
+
docCache.delete(cacheKey);
|
|
1216
|
+
}
|
|
1217
|
+
const err = new Error(
|
|
1218
|
+
`PDF \u4E2D\u6587\u5B57\u4F53 CMap \u8D44\u6E90\u52A0\u8F7D\u5931\u8D25\uFF08${f.name}\uFF1A${f.url}\uFF0C${f.reason}\uFF09`
|
|
1219
|
+
);
|
|
1220
|
+
err.code = "CMAP_LOAD_FAILED";
|
|
1221
|
+
throw err;
|
|
1222
|
+
}
|
|
1175
1223
|
} catch (err) {
|
|
1176
1224
|
if (isRenderingCancelledError(err)) {
|
|
1177
1225
|
return;
|