@pixldocs/canvas-renderer 0.5.191 → 0.5.193

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.
@@ -1,7 +1,7 @@
1
1
  import { jsPDF, ShadingPattern } from "jspdf";
2
2
  import { svg2pdf } from "svg2pdf.js";
3
3
  import * as fabric from "fabric";
4
- import { g as getCanvasForPage, c as captureFabricCanvasSvgForPdf, f as findNodeById, a as getAbsoluteBounds, p as parseTextMarkdown, r as renderSmartElementToSvg, n as normalizeShapeType, h as hasEdgeFade, b as getProxiedImageUrl, d as bakeEdgeFade, i as isElement, e as isGroup, j as buildRoundedTrianglePath, A as API_URL, k as getImageProxyFetchOptions, l as getRoundedRectRadii, T as TRIANGLE_STROKE_MITER_LIMIT, m as getTrianglePoints } from "./index-C3kuDISv.js";
4
+ import { g as getCanvasForPage, c as captureFabricCanvasSvgForPdf, f as findNodeById, a as getAbsoluteBounds, p as parseTextMarkdown, r as renderSmartElementToSvg, n as normalizeShapeType, h as hasEdgeFade, b as getProxiedImageUrl, d as bakeEdgeFade, i as isElement, e as isGroup, j as buildRoundedTrianglePath, A as API_URL, k as getImageProxyFetchOptions, l as getRoundedRectRadii, T as TRIANGLE_STROKE_MITER_LIMIT, m as getTrianglePoints } from "./index-CDoyeKa8.js";
5
5
  import { resetPdfFontRegistry, FONT_FALLBACK_SYMBOLS, FONT_FALLBACK_MATH, FONT_FALLBACK_DEVANAGARI, embedFontWithGoogleFallback, getEmbeddedVariantsList, isFontAvailable, isFamilyEmbedded, resolveBestRegisteredVariant, getEmbeddedJsPDFFontName, resolveFontWeight, doesVariantSupportChar } from "./pdfFonts-DhEaMTZl.js";
6
6
  async function embedFontsForSvg(pdf, svgStr) {
7
7
  var _a;
@@ -449,7 +449,8 @@ function buildMatrixTransformedSvgForPage(svg, matrix, localX, localY, width, he
449
449
  frame: { x: minX, y: minY, width: frameW, height: frameH }
450
450
  };
451
451
  }
452
- async function svg2pdfWithDomMount(svg, pdf, opts) {
452
+ async function svg2pdfWithDomMount(svg, pdf, opts, options) {
453
+ await preloadSvgImagesForPdf(svg, options);
453
454
  const wrap = document.createElement("div");
454
455
  wrap.style.cssText = "position:fixed;left:-9999px;top:0;width:0;height:0;overflow:hidden;pointer-events:none;opacity:0";
455
456
  wrap.appendChild(svg);
@@ -461,6 +462,48 @@ async function svg2pdfWithDomMount(svg, pdf, opts) {
461
462
  if (wrap.parentNode) document.body.removeChild(wrap);
462
463
  }
463
464
  }
465
+ function getSvgImageHref(image) {
466
+ return image.getAttribute("href") || image.getAttribute("xlink:href") || "";
467
+ }
468
+ async function preloadSvgImagesForPdf(svg, options) {
469
+ const images = Array.from(svg.querySelectorAll("image"));
470
+ if (!images.length) return;
471
+ const required = new Set((options == null ? void 0 : options.requiredImageIds) ?? []);
472
+ const failedRequired = [];
473
+ const loadOne = (href) => new Promise((resolve) => {
474
+ if (!href) return resolve(false);
475
+ const img = new Image();
476
+ if (!href.startsWith("data:") && !href.startsWith("blob:")) img.crossOrigin = "anonymous";
477
+ const done = (ok) => resolve(ok);
478
+ img.onload = async () => {
479
+ try {
480
+ if (typeof img.decode === "function") await img.decode();
481
+ } catch {
482
+ }
483
+ done(img.naturalWidth > 0 && img.naturalHeight > 0);
484
+ };
485
+ img.onerror = () => done(false);
486
+ try {
487
+ img.src = href;
488
+ } catch {
489
+ done(false);
490
+ }
491
+ });
492
+ for (const image of images) {
493
+ const href = getSvgImageHref(image);
494
+ const id = image.getAttribute("data-pixldocs-image-id") || "";
495
+ const ok = await loadOne(href);
496
+ if (id && required.has(id) && !ok) failedRequired.push(id);
497
+ }
498
+ if (required.size > 0) {
499
+ const present = new Set(images.map((image) => image.getAttribute("data-pixldocs-image-id") || "").filter(Boolean));
500
+ const missing = [...required].filter((id) => !present.has(id));
501
+ if (missing.length || failedRequired.length) {
502
+ throw new Error(`[client-pdf-export] expected image(s) not ready for SVG PDF${(options == null ? void 0 : options.context) ? ` (${options.context})` : ""}: missing=${missing.join(",") || "none"} failed=${failedRequired.join(",") || "none"}`);
503
+ }
504
+ console.log("[client-pdf-export] SVG image ids ready", { context: options == null ? void 0 : options.context, count: required.size, ids: [...required] });
505
+ }
506
+ }
464
507
  function resetPdfColorState(pdf, label) {
465
508
  pdf.setFillColor(0, 0, 0);
466
509
  pdf.setDrawColor(0, 0, 0);
@@ -2506,7 +2549,7 @@ async function fetchSvgAsElement(imageUrl, colorMap) {
2506
2549
  async function getRecoloredSvgDataUrl(imageUrl, colorMap) {
2507
2550
  if (!colorMap || Object.keys(colorMap).length === 0) return null;
2508
2551
  try {
2509
- const { getNormalizedSvgUrl } = await import("./index-C3kuDISv.js").then((n) => n.a6);
2552
+ const { getNormalizedSvgUrl } = await import("./index-CDoyeKa8.js").then((n) => n.a6);
2510
2553
  return await getNormalizedSvgUrl(imageUrl, colorMap);
2511
2554
  } catch {
2512
2555
  return null;
@@ -3297,7 +3340,7 @@ async function fetchImageAsBase64(imageUrl, opts = {}) {
3297
3340
  }
3298
3341
  let fetchUrl = imageUrl;
3299
3342
  if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
3300
- const { isPrivateUrl } = await import("./index-C3kuDISv.js").then((n) => n.a6);
3343
+ const { isPrivateUrl } = await import("./index-CDoyeKa8.js").then((n) => n.a6);
3301
3344
  if (isPrivateUrl(imageUrl)) return null;
3302
3345
  const proxyUrl = new URL(`${API_URL}/image-proxy`);
3303
3346
  proxyUrl.searchParams.set("url", imageUrl);
@@ -4902,6 +4945,17 @@ function preparePagesForExport(pages, canvasWidth, canvasHeight, options) {
4902
4945
  };
4903
4946
  });
4904
4947
  }
4948
+ function getExpectedRasterImageIdsForPdfPage(page) {
4949
+ const ids = [];
4950
+ const isSvgUrl = (url) => /\.svg(?:\?|#|$)/i.test(url) || /^data:image\/svg/i.test(url);
4951
+ for (const item of page.elements) {
4952
+ if (isGroupBackgroundDrawable(item) || item.type !== "image" || item.visible === false) continue;
4953
+ const src = String(item.src || item.imageUrl || "").trim();
4954
+ if (!src || item.sourceFormat === "svg" || isSvgUrl(src)) continue;
4955
+ ids.push(item.id);
4956
+ }
4957
+ return [...new Set(ids)];
4958
+ }
4905
4959
  async function exportMultiPagePdf(pages, options) {
4906
4960
  var _a, _b, _c, _d, _e;
4907
4961
  const { filename = "document.pdf", title, watermark = false, returnBlob = false, pdfTextMode = "selectable", skipLiveCanvasSvgFastPath = false } = options;
@@ -5049,6 +5103,7 @@ async function exportMultiPagePdf(pages, options) {
5049
5103
  }
5050
5104
  const liveCanvasForPage = page.pageId ? getCanvasForPage(page.pageId) : null;
5051
5105
  let allowLiveCanvasFallback = !!liveCanvasForPage && !skipLiveCanvasSvgFastPath;
5106
+ const expectedRasterImageIds = getExpectedRasterImageIdsForPdfPage(page);
5052
5107
  if (liveCanvasForPage && !skipLiveCanvasSvgFastPath) {
5053
5108
  try {
5054
5109
  const liveSvgString = captureFabricCanvasSvgForPdf(
@@ -5091,7 +5146,8 @@ async function exportMultiPagePdf(pages, options) {
5091
5146
  await svg2pdfWithDomMount(
5092
5147
  liveSvg,
5093
5148
  pdfWithColorLogging(pdf, seq, `page-${pageIndex + 1}`),
5094
- svg2pdfOpts(0, 0, page.width, page.height)
5149
+ svg2pdfOpts(0, 0, page.width, page.height),
5150
+ { requiredImageIds: expectedRasterImageIds, context: `page-${pageIndex + 1}` }
5095
5151
  );
5096
5152
  debugLog(`live canvas svg2pdf DONE #${seq} page=${pageIndex + 1}`);
5097
5153
  resetPdfColorState(pdf, `after live canvas svg2pdf page ${pageIndex + 1}`);
@@ -5105,6 +5161,9 @@ async function exportMultiPagePdf(pages, options) {
5105
5161
  svgLength: (liveSvgString == null ? void 0 : liveSvgString.length) ?? 0
5106
5162
  });
5107
5163
  } catch (error) {
5164
+ if (error instanceof Error && /expected image\(s\) not ready for SVG PDF/i.test(error.message)) {
5165
+ throw error;
5166
+ }
5108
5167
  allowLiveCanvasFallback = false;
5109
5168
  console.error(`[client-pdf-export] live SVG export failed for page ${pageIndex + 1}`, error);
5110
5169
  }
@@ -5293,4 +5352,4 @@ export {
5293
5352
  preparePagesForExport,
5294
5353
  rewriteSvgFontsForJsPDFWithSourceMeta
5295
5354
  };
5296
- //# sourceMappingURL=vectorPdfExport-CrTGqxx6.js.map
5355
+ //# sourceMappingURL=vectorPdfExport-Cd--tBky.js.map