@yudaocode/univer-pdfs 1.6.215 → 1.6.216

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.
@@ -6680,14 +6680,42 @@ function createPdfCjkFontSources(options) {
6680
6680
  url: `${cleanBase}/${preset.file}`,
6681
6681
  family,
6682
6682
  weight,
6683
- ...integrity ? { integrity } : {}
6683
+ ...integrity ? { integrity } : {},
6684
+ ...options?.credentials !== void 0 ? { credentials: options.credentials } : {},
6685
+ ...options?.fetchInit !== void 0 ? { fetchInit: options.fetchInit } : {}
6684
6686
  };
6685
6687
  });
6686
6688
  }
6687
6689
  var pdfFontUrlByteCache = /* @__PURE__ */ new Map();
6690
+ function computeFontUrlCacheKey(url, integrity) {
6691
+ return `${url}|${integrity ?? ""}`;
6692
+ }
6688
6693
  function clearPdfFontUrlCache() {
6689
6694
  pdfFontUrlByteCache.clear();
6690
6695
  }
6696
+ var PdfFontCandidateLoadError = class extends Error {
6697
+ constructor(kind, message, status) {
6698
+ super(message);
6699
+ this.name = "PdfFontCandidateLoadError";
6700
+ this.kind = kind;
6701
+ if (status !== void 0) {
6702
+ this.status = status;
6703
+ }
6704
+ }
6705
+ };
6706
+ function toFailedFontEntry(index, src, err) {
6707
+ const kind = err instanceof PdfFontCandidateLoadError ? err.kind : "format";
6708
+ const status = err instanceof PdfFontCandidateLoadError ? err.status : void 0;
6709
+ return {
6710
+ index,
6711
+ ...src.family !== void 0 ? { family: src.family } : {},
6712
+ ...src.weight !== void 0 ? { weight: src.weight } : {},
6713
+ ...src.url !== void 0 ? { url: src.url } : {},
6714
+ kind,
6715
+ ...status !== void 0 ? { status } : {},
6716
+ reason: err?.message || String(err)
6717
+ };
6718
+ }
6691
6719
  function bytesToBase64Standard(bytes) {
6692
6720
  if (typeof Buffer !== "undefined") {
6693
6721
  return Buffer.from(bytes).toString("base64");
@@ -6703,7 +6731,10 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
6703
6731
  if (!trimmed) return;
6704
6732
  const m = trimmed.match(/^(sha256|sha384|sha512)-([A-Za-z0-9+/=_-]+)$/i);
6705
6733
  if (!m) {
6706
- throw new Error(`Unsupported integrity format '${trimmed}' (expected sha256-/sha384-/sha512-).`);
6734
+ throw new PdfFontCandidateLoadError(
6735
+ "integrity",
6736
+ `Unsupported integrity format '${trimmed}' (expected sha256-/sha384-/sha512-).`
6737
+ );
6707
6738
  }
6708
6739
  const algoMap = {
6709
6740
  sha256: "SHA-256",
@@ -6714,7 +6745,10 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
6714
6745
  const expectedB64 = m[2].replace(/-/g, "+").replace(/_/g, "/");
6715
6746
  const subtle = globalThis.crypto?.subtle;
6716
6747
  if (!subtle) {
6717
- throw new Error("Web Crypto API is unavailable to verify font integrity.");
6748
+ throw new PdfFontCandidateLoadError(
6749
+ "integrity",
6750
+ "Web Crypto API is unavailable to verify font integrity."
6751
+ );
6718
6752
  }
6719
6753
  const digestBuf = await subtle.digest(
6720
6754
  algo,
@@ -6723,45 +6757,84 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
6723
6757
  const actualB64 = bytesToBase64Standard(new Uint8Array(digestBuf)).replace(/=+$/, "");
6724
6758
  const normExpected = expectedB64.replace(/=+$/, "");
6725
6759
  if (actualB64 !== normExpected) {
6726
- throw new Error(`Font SRI integrity mismatch (${m[1]}).`);
6760
+ throw new PdfFontCandidateLoadError("integrity", `Font SRI integrity mismatch (${m[1]}).`);
6727
6761
  }
6728
6762
  }
6763
+ async function fetchFontBytesWithSri(url, options) {
6764
+ const cacheKey = computeFontUrlCacheKey(url, options?.integrity);
6765
+ const cached = pdfFontUrlByteCache.get(cacheKey);
6766
+ if (cached) {
6767
+ return cached;
6768
+ }
6769
+ const mergedInit = {
6770
+ ...options?.fetchInit ?? {},
6771
+ ...options?.credentials !== void 0 ? { credentials: options.credentials } : {}
6772
+ };
6773
+ const hasInitKeys = Object.keys(mergedInit).length > 0;
6774
+ let resp;
6775
+ try {
6776
+ resp = hasInitKeys ? await fetch(url, mergedInit) : await fetch(url);
6777
+ } catch (err) {
6778
+ throw new PdfFontCandidateLoadError(
6779
+ "network",
6780
+ `Network error fetching '${url}': ${err?.message || String(err)}`
6781
+ );
6782
+ }
6783
+ if (!resp.ok) {
6784
+ throw new PdfFontCandidateLoadError(
6785
+ "http",
6786
+ `HTTP ${resp.status} fetching '${url}'.`,
6787
+ resp.status
6788
+ );
6789
+ }
6790
+ let ab;
6791
+ try {
6792
+ ab = await resp.arrayBuffer();
6793
+ } catch (err) {
6794
+ throw new PdfFontCandidateLoadError(
6795
+ "network",
6796
+ `Network error reading font body for '${url}': ${err?.message || String(err)}`
6797
+ );
6798
+ }
6799
+ const rawBytes = new Uint8Array(ab);
6800
+ if (rawBytes.byteLength === 0) {
6801
+ throw new PdfFontCandidateLoadError("empty", `Font response for '${url}' is empty (0 bytes).`);
6802
+ }
6803
+ if (options?.integrity) {
6804
+ await verifyFontSubresourceIntegrity(rawBytes, options.integrity);
6805
+ }
6806
+ try {
6807
+ inspectTrueTypeFont(rawBytes);
6808
+ } catch (err) {
6809
+ throw new PdfFontCandidateLoadError("format", err?.message || String(err));
6810
+ }
6811
+ pdfFontUrlByteCache.set(cacheKey, rawBytes);
6812
+ return rawBytes;
6813
+ }
6729
6814
  async function loadSingleFontCandidate(src) {
6730
- let rawBytes;
6731
6815
  if (src.bytes != null) {
6732
- rawBytes = src.bytes instanceof Uint8Array ? src.bytes : new Uint8Array(src.bytes);
6816
+ const rawBytes = src.bytes instanceof Uint8Array ? src.bytes : new Uint8Array(src.bytes);
6817
+ if (rawBytes.byteLength === 0) {
6818
+ throw new PdfFontCandidateLoadError("empty", "Font binary is empty (0 bytes).");
6819
+ }
6733
6820
  if (src.integrity) {
6734
6821
  await verifyFontSubresourceIntegrity(rawBytes, src.integrity);
6735
6822
  }
6736
- } else if (src.url) {
6737
- const cacheKey = `${src.url}|${src.integrity ?? ""}`;
6738
- const cached = pdfFontUrlByteCache.get(cacheKey);
6739
- if (cached) {
6740
- rawBytes = cached;
6741
- } else {
6742
- let resp;
6743
- try {
6744
- resp = await fetch(src.url);
6745
- } catch (err) {
6746
- throw new Error(`Network error fetching '${src.url}': ${err?.message || String(err)}`);
6747
- }
6748
- if (!resp.ok) {
6749
- throw new Error(`HTTP ${resp.status} fetching '${src.url}'.`);
6750
- }
6751
- const ab = await resp.arrayBuffer();
6752
- rawBytes = new Uint8Array(ab);
6753
- if (src.integrity) {
6754
- await verifyFontSubresourceIntegrity(rawBytes, src.integrity);
6755
- }
6823
+ try {
6756
6824
  inspectTrueTypeFont(rawBytes);
6757
- pdfFontUrlByteCache.set(cacheKey, rawBytes);
6758
- return rawBytes;
6825
+ } catch (err) {
6826
+ throw new PdfFontCandidateLoadError("format", err?.message || String(err));
6759
6827
  }
6760
- } else {
6761
- throw new Error("Font source has neither bytes nor url.");
6828
+ return rawBytes;
6762
6829
  }
6763
- inspectTrueTypeFont(rawBytes);
6764
- return rawBytes;
6830
+ if (src.url && src.url.trim()) {
6831
+ return fetchFontBytesWithSri(src.url, {
6832
+ integrity: src.integrity,
6833
+ credentials: src.credentials,
6834
+ fetchInit: src.fetchInit
6835
+ });
6836
+ }
6837
+ throw new PdfFontCandidateLoadError("empty", "Font source has neither bytes nor url.");
6765
6838
  }
6766
6839
  function getByPath(data, path) {
6767
6840
  if (!data || !path) return void 0;
@@ -7144,13 +7217,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7144
7217
  normalFontBytes = await loadSingleFontCandidate(f);
7145
7218
  break;
7146
7219
  } catch (err) {
7147
- failedFonts.push({
7148
- index,
7149
- family: f.family,
7150
- weight: f.weight,
7151
- url: f.url,
7152
- reason: err?.message || String(err)
7153
- });
7220
+ failedFonts.push(toFailedFontEntry(index, f, err));
7154
7221
  }
7155
7222
  }
7156
7223
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -7162,13 +7229,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7162
7229
  boldFontBytes = loadedBold;
7163
7230
  break;
7164
7231
  } catch (err) {
7165
- failedFonts.push({
7166
- index,
7167
- family: f.family,
7168
- weight: f.weight,
7169
- url: f.url,
7170
- reason: err?.message || String(err)
7171
- });
7232
+ failedFonts.push(toFailedFontEntry(index, f, err));
7172
7233
  }
7173
7234
  }
7174
7235
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -7181,13 +7242,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7181
7242
  normalFontBytes = await loadSingleFontCandidate(f);
7182
7243
  break;
7183
7244
  } catch (err) {
7184
- failedFonts.push({
7185
- index,
7186
- family: f.family,
7187
- weight: f.weight,
7188
- url: f.url,
7189
- reason: err?.message || String(err)
7190
- });
7245
+ failedFonts.push(toFailedFontEntry(index, f, err));
7191
7246
  }
7192
7247
  }
7193
7248
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -7198,13 +7253,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7198
7253
  boldFontBytes = await loadSingleFontCandidate(f);
7199
7254
  break;
7200
7255
  } catch (err) {
7201
- failedFonts.push({
7202
- index,
7203
- family: f.family,
7204
- weight: f.weight,
7205
- url: f.url,
7206
- reason: err?.message || String(err)
7207
- });
7256
+ failedFonts.push(toFailedFontEntry(index, f, err));
7208
7257
  }
7209
7258
  }
7210
7259
  if (!boldFontBytes) requiredWeightsFailed = true;
@@ -7223,15 +7272,24 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7223
7272
  }
7224
7273
  } else {
7225
7274
  boldFontBytes = options?.customBoldFontBytes;
7226
- if (options?.customFontBytes && options.customFontBytes.byteLength > 0) {
7227
- try {
7228
- inspectTrueTypeFont(options.customFontBytes);
7229
- normalFontBytes = options.customFontBytes;
7230
- } catch (err) {
7275
+ if (options?.customFontBytes !== void 0) {
7276
+ if (options.customFontBytes.byteLength === 0) {
7231
7277
  failedFonts.push({
7232
7278
  index: failedFonts.length,
7233
- reason: err?.message || String(err)
7279
+ kind: "empty",
7280
+ reason: "Font binary is empty (0 bytes)."
7234
7281
  });
7282
+ } else {
7283
+ try {
7284
+ inspectTrueTypeFont(options.customFontBytes);
7285
+ normalFontBytes = options.customFontBytes;
7286
+ } catch (err) {
7287
+ failedFonts.push({
7288
+ index: failedFonts.length,
7289
+ kind: "format",
7290
+ reason: err?.message || String(err)
7291
+ });
7292
+ }
7235
7293
  }
7236
7294
  }
7237
7295
  if (!normalFontBytes && snapshot?.resourceBindings) {
@@ -7241,12 +7299,22 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
7241
7299
  if (fontBinding?.dataUri) {
7242
7300
  try {
7243
7301
  const decoded = dataUriToBytes(fontBinding.dataUri);
7244
- inspectTrueTypeFont(decoded);
7245
- normalFontBytes = decoded;
7302
+ if (decoded.byteLength === 0) {
7303
+ failedFonts.push({
7304
+ index: failedFonts.length,
7305
+ ...fontBinding.name ? { family: fontBinding.name } : {},
7306
+ kind: "empty",
7307
+ reason: "Font binary is empty (0 bytes)."
7308
+ });
7309
+ } else {
7310
+ inspectTrueTypeFont(decoded);
7311
+ normalFontBytes = decoded;
7312
+ }
7246
7313
  } catch (err) {
7247
7314
  failedFonts.push({
7248
7315
  index: failedFonts.length,
7249
- family: fontBinding.name,
7316
+ ...fontBinding.name ? { family: fontBinding.name } : {},
7317
+ kind: "format",
7250
7318
  reason: err?.message || String(err)
7251
7319
  });
7252
7320
  }
@@ -9667,6 +9735,12 @@ async function drawPageHeaderAndFooter(pdfDoc, pdfPage, pageWidth, pageHeight, c
9667
9735
  }
9668
9736
  }
9669
9737
  async function exportPdfBytes(snapshot, runtimeData, options) {
9738
+ const throwIfExportAborted = () => {
9739
+ if (options?.signal?.aborted) {
9740
+ throw typeof DOMException === "function" ? new DOMException("The operation was aborted.", "AbortError") : Object.assign(new Error("The operation was aborted."), { name: "AbortError" });
9741
+ }
9742
+ };
9743
+ throwIfExportAborted();
9670
9744
  const effectiveData = runtimeData ?? snapshot.metadata?.sampleData;
9671
9745
  const excludeBasePdf = Boolean(snapshot.metadata?.excludeBasePdfOnExport);
9672
9746
  const wmCfg = snapshot.metadata?.watermarkConfig;
@@ -9682,6 +9756,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
9682
9756
  );
9683
9757
  const optimizeSize = Boolean(options?.optimizeSize);
9684
9758
  const preResolvedFonts = await resolveRenderFontsForExport(snapshot, effectiveData, options);
9759
+ throwIfExportAborted();
9685
9760
  const pdfDoc = await PDFDocument2.create();
9686
9761
  if (snapshot.metadata?.sanitized) {
9687
9762
  pdfDoc.setTitle("");
@@ -11943,7 +12018,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
11943
12018
  if (optimizeSize) {
11944
12019
  pruneUnreferencedObjectsAndCompressStreams(pdfDoc);
11945
12020
  }
12021
+ throwIfExportAborted();
11946
12022
  const savedPdfBytes = interactiveForm && hasAnyAcroWidget ? await pdfDoc.save({ updateFieldAppearances: false }) : await pdfDoc.save();
12023
+ throwIfExportAborted();
11947
12024
  if (options?.diagnosticGridOverlay) {
11948
12025
  const gridOpts = typeof options.diagnosticGridOverlay === "object" ? options.diagnosticGridOverlay : void 0;
11949
12026
  return await overlayDiagnosticGridOnPdfBytes(savedPdfBytes, snapshot, gridOpts);
@@ -16193,6 +16270,7 @@ export {
16193
16270
  DEFAULT_PDF_CJK_FONT_FAMILY,
16194
16271
  DEFAULT_PDF_CJK_FONT_FILES,
16195
16272
  createPdfCjkFontSources,
16273
+ computeFontUrlCacheKey,
16196
16274
  clearPdfFontUrlCache,
16197
16275
  resolveTemplateString,
16198
16276
  validatePdfTemplateVariables,
@@ -16259,4 +16337,4 @@ export {
16259
16337
  renderJsxToPdfmeTemplate,
16260
16338
  exportJsxToPdfBytes
16261
16339
  };
16262
- //# sourceMappingURL=chunk-DYTQBEM7.js.map
16340
+ //# sourceMappingURL=chunk-EMSHTYYU.js.map