@yudaocode/univer-pdfs 1.6.215 → 1.6.217

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.
@@ -5582,6 +5582,32 @@ var DEFAULT_PDF_CJK_FONT_FILES = Object.freeze({
5582
5582
  })
5583
5583
  });
5584
5584
  var pdfFontUrlByteCache = /* @__PURE__ */ new Map();
5585
+ function computeFontUrlCacheKey(url, integrity) {
5586
+ return `${url}|${integrity ?? ""}`;
5587
+ }
5588
+ var PdfFontCandidateLoadError = class extends Error {
5589
+ constructor(kind, message, status) {
5590
+ super(message);
5591
+ this.name = "PdfFontCandidateLoadError";
5592
+ this.kind = kind;
5593
+ if (status !== void 0) {
5594
+ this.status = status;
5595
+ }
5596
+ }
5597
+ };
5598
+ function toFailedFontEntry(index, src, err) {
5599
+ const kind = err instanceof PdfFontCandidateLoadError ? err.kind : "format";
5600
+ const status = err instanceof PdfFontCandidateLoadError ? err.status : void 0;
5601
+ return {
5602
+ index,
5603
+ ...src.family !== void 0 ? { family: src.family } : {},
5604
+ ...src.weight !== void 0 ? { weight: src.weight } : {},
5605
+ ...src.url !== void 0 ? { url: src.url } : {},
5606
+ kind,
5607
+ ...status !== void 0 ? { status } : {},
5608
+ reason: err?.message || String(err)
5609
+ };
5610
+ }
5585
5611
  function bytesToBase64Standard(bytes) {
5586
5612
  if (typeof Buffer !== "undefined") {
5587
5613
  return Buffer.from(bytes).toString("base64");
@@ -5597,7 +5623,10 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
5597
5623
  if (!trimmed) return;
5598
5624
  const m = trimmed.match(/^(sha256|sha384|sha512)-([A-Za-z0-9+/=_-]+)$/i);
5599
5625
  if (!m) {
5600
- throw new Error(`Unsupported integrity format '${trimmed}' (expected sha256-/sha384-/sha512-).`);
5626
+ throw new PdfFontCandidateLoadError(
5627
+ "integrity",
5628
+ `Unsupported integrity format '${trimmed}' (expected sha256-/sha384-/sha512-).`
5629
+ );
5601
5630
  }
5602
5631
  const algoMap = {
5603
5632
  sha256: "SHA-256",
@@ -5608,7 +5637,10 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
5608
5637
  const expectedB64 = m[2].replace(/-/g, "+").replace(/_/g, "/");
5609
5638
  const subtle = globalThis.crypto?.subtle;
5610
5639
  if (!subtle) {
5611
- throw new Error("Web Crypto API is unavailable to verify font integrity.");
5640
+ throw new PdfFontCandidateLoadError(
5641
+ "integrity",
5642
+ "Web Crypto API is unavailable to verify font integrity."
5643
+ );
5612
5644
  }
5613
5645
  const digestBuf = await subtle.digest(
5614
5646
  algo,
@@ -5617,45 +5649,84 @@ async function verifyFontSubresourceIntegrity(bytes, integrity) {
5617
5649
  const actualB64 = bytesToBase64Standard(new Uint8Array(digestBuf)).replace(/=+$/, "");
5618
5650
  const normExpected = expectedB64.replace(/=+$/, "");
5619
5651
  if (actualB64 !== normExpected) {
5620
- throw new Error(`Font SRI integrity mismatch (${m[1]}).`);
5652
+ throw new PdfFontCandidateLoadError("integrity", `Font SRI integrity mismatch (${m[1]}).`);
5653
+ }
5654
+ }
5655
+ async function fetchFontBytesWithSri(url, options) {
5656
+ const cacheKey = computeFontUrlCacheKey(url, options?.integrity);
5657
+ const cached = pdfFontUrlByteCache.get(cacheKey);
5658
+ if (cached) {
5659
+ return cached;
5660
+ }
5661
+ const mergedInit = {
5662
+ ...options?.fetchInit ?? {},
5663
+ ...options?.credentials !== void 0 ? { credentials: options.credentials } : {}
5664
+ };
5665
+ const hasInitKeys = Object.keys(mergedInit).length > 0;
5666
+ let resp;
5667
+ try {
5668
+ resp = hasInitKeys ? await fetch(url, mergedInit) : await fetch(url);
5669
+ } catch (err) {
5670
+ throw new PdfFontCandidateLoadError(
5671
+ "network",
5672
+ `Network error fetching '${url}': ${err?.message || String(err)}`
5673
+ );
5674
+ }
5675
+ if (!resp.ok) {
5676
+ throw new PdfFontCandidateLoadError(
5677
+ "http",
5678
+ `HTTP ${resp.status} fetching '${url}'.`,
5679
+ resp.status
5680
+ );
5681
+ }
5682
+ let ab;
5683
+ try {
5684
+ ab = await resp.arrayBuffer();
5685
+ } catch (err) {
5686
+ throw new PdfFontCandidateLoadError(
5687
+ "network",
5688
+ `Network error reading font body for '${url}': ${err?.message || String(err)}`
5689
+ );
5690
+ }
5691
+ const rawBytes = new Uint8Array(ab);
5692
+ if (rawBytes.byteLength === 0) {
5693
+ throw new PdfFontCandidateLoadError("empty", `Font response for '${url}' is empty (0 bytes).`);
5694
+ }
5695
+ if (options?.integrity) {
5696
+ await verifyFontSubresourceIntegrity(rawBytes, options.integrity);
5621
5697
  }
5698
+ try {
5699
+ inspectTrueTypeFont(rawBytes);
5700
+ } catch (err) {
5701
+ throw new PdfFontCandidateLoadError("format", err?.message || String(err));
5702
+ }
5703
+ pdfFontUrlByteCache.set(cacheKey, rawBytes);
5704
+ return rawBytes;
5622
5705
  }
5623
5706
  async function loadSingleFontCandidate(src) {
5624
- let rawBytes;
5625
5707
  if (src.bytes != null) {
5626
- rawBytes = src.bytes instanceof Uint8Array ? src.bytes : new Uint8Array(src.bytes);
5708
+ const rawBytes = src.bytes instanceof Uint8Array ? src.bytes : new Uint8Array(src.bytes);
5709
+ if (rawBytes.byteLength === 0) {
5710
+ throw new PdfFontCandidateLoadError("empty", "Font binary is empty (0 bytes).");
5711
+ }
5627
5712
  if (src.integrity) {
5628
5713
  await verifyFontSubresourceIntegrity(rawBytes, src.integrity);
5629
5714
  }
5630
- } else if (src.url) {
5631
- const cacheKey = `${src.url}|${src.integrity ?? ""}`;
5632
- const cached = pdfFontUrlByteCache.get(cacheKey);
5633
- if (cached) {
5634
- rawBytes = cached;
5635
- } else {
5636
- let resp;
5637
- try {
5638
- resp = await fetch(src.url);
5639
- } catch (err) {
5640
- throw new Error(`Network error fetching '${src.url}': ${err?.message || String(err)}`);
5641
- }
5642
- if (!resp.ok) {
5643
- throw new Error(`HTTP ${resp.status} fetching '${src.url}'.`);
5644
- }
5645
- const ab = await resp.arrayBuffer();
5646
- rawBytes = new Uint8Array(ab);
5647
- if (src.integrity) {
5648
- await verifyFontSubresourceIntegrity(rawBytes, src.integrity);
5649
- }
5715
+ try {
5650
5716
  inspectTrueTypeFont(rawBytes);
5651
- pdfFontUrlByteCache.set(cacheKey, rawBytes);
5652
- return rawBytes;
5717
+ } catch (err) {
5718
+ throw new PdfFontCandidateLoadError("format", err?.message || String(err));
5653
5719
  }
5654
- } else {
5655
- throw new Error("Font source has neither bytes nor url.");
5720
+ return rawBytes;
5656
5721
  }
5657
- inspectTrueTypeFont(rawBytes);
5658
- return rawBytes;
5722
+ if (src.url && src.url.trim()) {
5723
+ return fetchFontBytesWithSri(src.url, {
5724
+ integrity: src.integrity,
5725
+ credentials: src.credentials,
5726
+ fetchInit: src.fetchInit
5727
+ });
5728
+ }
5729
+ throw new PdfFontCandidateLoadError("empty", "Font source has neither bytes nor url.");
5659
5730
  }
5660
5731
  function getByPath(data, path) {
5661
5732
  if (!data || !path) return void 0;
@@ -5962,13 +6033,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
5962
6033
  normalFontBytes = await loadSingleFontCandidate(f);
5963
6034
  break;
5964
6035
  } catch (err) {
5965
- failedFonts.push({
5966
- index,
5967
- family: f.family,
5968
- weight: f.weight,
5969
- url: f.url,
5970
- reason: err?.message || String(err)
5971
- });
6036
+ failedFonts.push(toFailedFontEntry(index, f, err));
5972
6037
  }
5973
6038
  }
5974
6039
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -5980,13 +6045,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
5980
6045
  boldFontBytes = loadedBold;
5981
6046
  break;
5982
6047
  } catch (err) {
5983
- failedFonts.push({
5984
- index,
5985
- family: f.family,
5986
- weight: f.weight,
5987
- url: f.url,
5988
- reason: err?.message || String(err)
5989
- });
6048
+ failedFonts.push(toFailedFontEntry(index, f, err));
5990
6049
  }
5991
6050
  }
5992
6051
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -5999,13 +6058,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
5999
6058
  normalFontBytes = await loadSingleFontCandidate(f);
6000
6059
  break;
6001
6060
  } catch (err) {
6002
- failedFonts.push({
6003
- index,
6004
- family: f.family,
6005
- weight: f.weight,
6006
- url: f.url,
6007
- reason: err?.message || String(err)
6008
- });
6061
+ failedFonts.push(toFailedFontEntry(index, f, err));
6009
6062
  }
6010
6063
  }
6011
6064
  if (!normalFontBytes) requiredWeightsFailed = true;
@@ -6016,13 +6069,7 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
6016
6069
  boldFontBytes = await loadSingleFontCandidate(f);
6017
6070
  break;
6018
6071
  } catch (err) {
6019
- failedFonts.push({
6020
- index,
6021
- family: f.family,
6022
- weight: f.weight,
6023
- url: f.url,
6024
- reason: err?.message || String(err)
6025
- });
6072
+ failedFonts.push(toFailedFontEntry(index, f, err));
6026
6073
  }
6027
6074
  }
6028
6075
  if (!boldFontBytes) requiredWeightsFailed = true;
@@ -6041,15 +6088,24 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
6041
6088
  }
6042
6089
  } else {
6043
6090
  boldFontBytes = options?.customBoldFontBytes;
6044
- if (options?.customFontBytes && options.customFontBytes.byteLength > 0) {
6045
- try {
6046
- inspectTrueTypeFont(options.customFontBytes);
6047
- normalFontBytes = options.customFontBytes;
6048
- } catch (err) {
6091
+ if (options?.customFontBytes !== void 0) {
6092
+ if (options.customFontBytes.byteLength === 0) {
6049
6093
  failedFonts.push({
6050
6094
  index: failedFonts.length,
6051
- reason: err?.message || String(err)
6095
+ kind: "empty",
6096
+ reason: "Font binary is empty (0 bytes)."
6052
6097
  });
6098
+ } else {
6099
+ try {
6100
+ inspectTrueTypeFont(options.customFontBytes);
6101
+ normalFontBytes = options.customFontBytes;
6102
+ } catch (err) {
6103
+ failedFonts.push({
6104
+ index: failedFonts.length,
6105
+ kind: "format",
6106
+ reason: err?.message || String(err)
6107
+ });
6108
+ }
6053
6109
  }
6054
6110
  }
6055
6111
  if (!normalFontBytes && snapshot?.resourceBindings) {
@@ -6059,12 +6115,22 @@ async function resolveRenderFontsForExport(snapshot, effectiveData, options) {
6059
6115
  if (fontBinding?.dataUri) {
6060
6116
  try {
6061
6117
  const decoded = dataUriToBytes(fontBinding.dataUri);
6062
- inspectTrueTypeFont(decoded);
6063
- normalFontBytes = decoded;
6118
+ if (decoded.byteLength === 0) {
6119
+ failedFonts.push({
6120
+ index: failedFonts.length,
6121
+ ...fontBinding.name ? { family: fontBinding.name } : {},
6122
+ kind: "empty",
6123
+ reason: "Font binary is empty (0 bytes)."
6124
+ });
6125
+ } else {
6126
+ inspectTrueTypeFont(decoded);
6127
+ normalFontBytes = decoded;
6128
+ }
6064
6129
  } catch (err) {
6065
6130
  failedFonts.push({
6066
6131
  index: failedFonts.length,
6067
- family: fontBinding.name,
6132
+ ...fontBinding.name ? { family: fontBinding.name } : {},
6133
+ kind: "format",
6068
6134
  reason: err?.message || String(err)
6069
6135
  });
6070
6136
  }
@@ -8414,6 +8480,12 @@ async function drawPageHeaderAndFooter(pdfDoc, pdfPage, pageWidth, pageHeight, c
8414
8480
  }
8415
8481
  }
8416
8482
  async function exportPdfBytes(snapshot, runtimeData, options) {
8483
+ const throwIfExportAborted = () => {
8484
+ if (options?.signal?.aborted) {
8485
+ throw typeof DOMException === "function" ? new DOMException("The operation was aborted.", "AbortError") : Object.assign(new Error("The operation was aborted."), { name: "AbortError" });
8486
+ }
8487
+ };
8488
+ throwIfExportAborted();
8417
8489
  const effectiveData = runtimeData ?? snapshot.metadata?.sampleData;
8418
8490
  const excludeBasePdf = Boolean(snapshot.metadata?.excludeBasePdfOnExport);
8419
8491
  const wmCfg = snapshot.metadata?.watermarkConfig;
@@ -8429,6 +8501,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
8429
8501
  );
8430
8502
  const optimizeSize = Boolean(options?.optimizeSize);
8431
8503
  const preResolvedFonts = await resolveRenderFontsForExport(snapshot, effectiveData, options);
8504
+ throwIfExportAborted();
8432
8505
  const pdfDoc = await import_pdf_lib3.PDFDocument.create();
8433
8506
  if (snapshot.metadata?.sanitized) {
8434
8507
  pdfDoc.setTitle("");
@@ -10690,7 +10763,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
10690
10763
  if (optimizeSize) {
10691
10764
  pruneUnreferencedObjectsAndCompressStreams(pdfDoc);
10692
10765
  }
10766
+ throwIfExportAborted();
10693
10767
  const savedPdfBytes = interactiveForm && hasAnyAcroWidget ? await pdfDoc.save({ updateFieldAppearances: false }) : await pdfDoc.save();
10768
+ throwIfExportAborted();
10694
10769
  if (options?.diagnosticGridOverlay) {
10695
10770
  const gridOpts = typeof options.diagnosticGridOverlay === "object" ? options.diagnosticGridOverlay : void 0;
10696
10771
  return await overlayDiagnosticGridOnPdfBytes(savedPdfBytes, snapshot, gridOpts);