@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/es/index.js CHANGED
@@ -1053,19 +1053,53 @@ function loadPdfDocument(dataUri, overrideAssets) {
1053
1053
  const effectiveAssets = overrideAssets ?? configuredAssets ?? resolveGlobalPdfjsAssets();
1054
1054
  ensurePdfjsWorker(effectiveAssets);
1055
1055
  const bytes = dataUriToUint8Array(dataUri);
1056
- const cacheKey = computePdfPayloadDigest(bytes);
1056
+ const cacheKey = `${computePdfPayloadDigest(bytes)}|${effectiveAssets?.cMapUrl ?? ""}`;
1057
1057
  const existing = docCache.get(cacheKey);
1058
- if (existing) {
1058
+ if (existing && existing.cMapFailures.length === 0) {
1059
1059
  touchCacheEntry(cacheKey, existing);
1060
- return existing.promise;
1060
+ return { promise: existing.promise, entry: existing, cacheKey };
1061
1061
  }
1062
+ const cMapFailures = [];
1062
1063
  const docInitParams = {
1063
1064
  data: bytes.slice(),
1064
1065
  isEvalSupported: false
1065
1066
  };
1066
1067
  if (effectiveAssets?.cMapUrl) {
1067
- docInitParams.cMapUrl = effectiveAssets.cMapUrl;
1068
- docInitParams.cMapPacked = effectiveAssets.cMapPacked ?? true;
1068
+ const cMapUrl = effectiveAssets.cMapUrl;
1069
+ const cMapPacked = effectiveAssets.cMapPacked ?? true;
1070
+ const normalizedBase = cMapUrl.endsWith("/") ? cMapUrl : `${cMapUrl}/`;
1071
+ docInitParams.cMapUrl = normalizedBase;
1072
+ docInitParams.cMapPacked = cMapPacked;
1073
+ docInitParams.CMapReaderFactory = class BgTrackedCMapReaderFactory {
1074
+ constructor(params) {
1075
+ this.baseUrl = params?.baseUrl ?? normalizedBase;
1076
+ this.isCompressed = params?.isCompressed ?? cMapPacked;
1077
+ }
1078
+ async fetch({ name }) {
1079
+ const base = this.baseUrl.endsWith("/") ? this.baseUrl : `${this.baseUrl}/`;
1080
+ const url = `${base}${name}${this.isCompressed ? ".bcmap" : ""}`;
1081
+ let resp;
1082
+ try {
1083
+ resp = await fetch(url);
1084
+ } catch (err) {
1085
+ const reason = err?.message || String(err);
1086
+ cMapFailures.push({ name, url, reason });
1087
+ throw new Error(`Unable to load CMap at ${url}: ${reason}`);
1088
+ }
1089
+ if (!resp.ok) {
1090
+ const reason = `HTTP ${resp.status}`;
1091
+ cMapFailures.push({ name, url, reason });
1092
+ throw new Error(`Unable to load CMap at ${url}: ${reason}`);
1093
+ }
1094
+ const buf = new Uint8Array(await resp.arrayBuffer());
1095
+ if (buf.byteLength === 0) {
1096
+ const reason = "Empty CMap payload";
1097
+ cMapFailures.push({ name, url, reason });
1098
+ throw new Error(`Unable to load CMap at ${url}: ${reason}`);
1099
+ }
1100
+ return { cMapData: buf, isCompressed: this.isCompressed };
1101
+ }
1102
+ };
1069
1103
  }
1070
1104
  if (effectiveAssets?.standardFontDataUrl) {
1071
1105
  docInitParams.standardFontDataUrl = effectiveAssets.standardFontDataUrl;
@@ -1075,6 +1109,7 @@ function loadPdfDocument(dataUri, overrideAssets) {
1075
1109
  }
1076
1110
  const loadingTask = pdfjsLib.getDocument(docInitParams);
1077
1111
  const entry = {
1112
+ cMapFailures,
1078
1113
  promise: loadingTask.promise.then((doc) => {
1079
1114
  entry.resolvedDoc = doc;
1080
1115
  return doc;
@@ -1087,10 +1122,11 @@ function loadPdfDocument(dataUri, overrideAssets) {
1087
1122
  };
1088
1123
  docCache.set(cacheKey, entry);
1089
1124
  evictOverflowCacheEntries();
1090
- return entry.promise;
1125
+ return { promise: entry.promise, entry, cacheKey };
1091
1126
  }
1092
1127
  async function inspectSourcePdfPages(dataUri, assets) {
1093
- const doc = await loadPdfDocument(dataUri, assets);
1128
+ const { promise: docPromise } = loadPdfDocument(dataUri, assets);
1129
+ const doc = await docPromise;
1094
1130
  const sizes = [];
1095
1131
  for (let i = 1; i <= doc.numPages; i++) {
1096
1132
  const page = await doc.getPage(i);
@@ -1122,7 +1158,8 @@ async function renderSourcePdfPageToCanvas(canvas, dataUri, pageNumber, targetWi
1122
1158
  } catch {
1123
1159
  }
1124
1160
  }
1125
- const doc = await loadPdfDocument(dataUri, assets);
1161
+ const { promise: docPromise, entry: docEntry, cacheKey } = loadPdfDocument(dataUri, assets);
1162
+ const doc = await docPromise;
1126
1163
  if (activeCanvasRenderTasks.has(canvas)) {
1127
1164
  return;
1128
1165
  }
@@ -1143,6 +1180,17 @@ async function renderSourcePdfPageToCanvas(canvas, dataUri, pageNumber, targetWi
1143
1180
  activeCanvasRenderTasks.set(canvas, renderTask);
1144
1181
  try {
1145
1182
  await renderTask.promise;
1183
+ if (docEntry.cMapFailures.length > 0) {
1184
+ const f = docEntry.cMapFailures[0];
1185
+ if (docCache.get(cacheKey) === docEntry) {
1186
+ docCache.delete(cacheKey);
1187
+ }
1188
+ const err = new Error(
1189
+ `PDF \u4E2D\u6587\u5B57\u4F53 CMap \u8D44\u6E90\u52A0\u8F7D\u5931\u8D25\uFF08${f.name}\uFF1A${f.url}\uFF0C${f.reason}\uFF09`
1190
+ );
1191
+ err.code = "CMAP_LOAD_FAILED";
1192
+ throw err;
1193
+ }
1146
1194
  } catch (err) {
1147
1195
  if (isRenderingCancelledError(err)) {
1148
1196
  return;