@imggion/html2realpdf 0.3.1 → 0.3.2

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/README.md CHANGED
@@ -6,9 +6,6 @@
6
6
  <a href="https://www.npmjs.com/package/@imggion/html2realpdf">
7
7
  <img src="https://img.shields.io/npm/v/@imggion/html2realpdf.svg?style=flat-square" alt="npm version">
8
8
  </a>
9
- <a href="https://github.com/imggion/html2realpdf/releases/tag/v0.3.1">
10
- <img src="https://img.shields.io/badge/release-0.3.1-2ea44f?style=flat-square" alt="Latest release: 0.3.1">
11
- </a>
12
9
  <a href="#compliance">
13
10
  <img src="https://img.shields.io/badge/PDF%2FA--3u-d32f2f?style=flat-square&amp;logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IndoaXRlIiBzdHJva2Utd2lkdGg9IjEuNSIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTTE0IDJINXYyMGgxNFY3ek0xNCAydjVoNSIvPjx0ZXh0IHg9IjEyIiB5PSIxNyIgZmlsbD0id2hpdGUiIGZvbnQtZmFtaWx5PSJBcmlhbCxzYW5zLXNlcmlmIiBmb250LXNpemU9IjciIGZvbnQtd2VpZ2h0PSJib2xkIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIj5QREY8L3RleHQ%2BPC9zdmc%2B" alt="PDF/A-3u">
14
11
  </a>
Binary file
package/dist/snapshot.js CHANGED
@@ -1794,6 +1794,9 @@ function materializeComputedStyle(original, target, options, diagnostics, isSnap
1794
1794
  if (!isSvgElement(original)) {
1795
1795
  const unsupportedComputed = [
1796
1796
  ["filter", computed.getPropertyValue("filter"), "none", false],
1797
+ // PDF output paints source-over; a dropped blend mode must be reported
1798
+ // rather than silently painting a multiply/overlay layer opaquely.
1799
+ ["mix-blend-mode", computed.getPropertyValue("mix-blend-mode"), "normal", false],
1797
1800
  ["background-image", computed.getPropertyValue("background-image"), "none", true],
1798
1801
  ["box-shadow", computed.getPropertyValue("box-shadow"), "none", true],
1799
1802
  ["text-shadow", computed.getPropertyValue("text-shadow"), "none", true],
@@ -1813,9 +1816,17 @@ function materializeComputedStyle(original, target, options, diagnostics, isSnap
1813
1816
  if (isSvgElement(original)) {
1814
1817
  properties = [...SUPPORTED_COMPUTED_PROPERTIES, ...SVG_COMPUTED_PROPERTIES];
1815
1818
  }
1819
+ // Pseudo-elements share their host's geometry, so only real elements can
1820
+ // freeze the parent tracks a subgrid spans.
1821
+ const isPseudo = target.hasAttribute("data-html2realpdf-pseudo");
1822
+ const subgridOverrides = isPseudo ? new Map() : resolveSubgridOverrides(original, computed, view, options, diagnostics);
1816
1823
  for (const property of properties) {
1817
1824
  let value = computed.getPropertyValue(property);
1818
1825
  let authoredFlowDimension = "";
1826
+ if (subgridOverrides.has(property)) {
1827
+ declarations.push(`${property}:${subgridOverrides.get(property)}`);
1828
+ continue;
1829
+ }
1819
1830
  if (property === "display") {
1820
1831
  value = display;
1821
1832
  }
@@ -1866,6 +1877,92 @@ function materializeComputedStyle(original, target, options, diagnostics, isSnap
1866
1877
  }
1867
1878
  target.setAttribute("style", declarations.join(";"));
1868
1879
  }
1880
+ /**
1881
+ * The native Grid formatter has no subgrid. A subgrid uses its parent's
1882
+ * tracks, so the snapshot freezes the parent tracks it spans as explicit
1883
+ * lengths. Columns keep the browser's used sizes; rows keep them as minimums
1884
+ * so native text that measures slightly taller can still grow. A subgrid
1885
+ * whose tracks cannot be identified is reported and falls back to `none`.
1886
+ */
1887
+ function resolveSubgridOverrides(original, computed, view, options, diagnostics) {
1888
+ const overrides = new Map();
1889
+ for (const axis of ["columns", "rows"]) {
1890
+ const property = axis === "columns" ? "grid-template-columns" : "grid-template-rows";
1891
+ if (!/^subgrid\b/i.test(computed.getPropertyValue(property).trim()))
1892
+ continue;
1893
+ const resolved = subgridTracks(original, computed, view, axis);
1894
+ if (!resolved) {
1895
+ reportUnsupportedCss(`${property}:subgrid`, options, diagnostics);
1896
+ overrides.set(property, "none");
1897
+ continue;
1898
+ }
1899
+ overrides.set(property, resolved.tracks);
1900
+ if (resolved.gap !== null)
1901
+ overrides.set(axis === "columns" ? "column-gap" : "row-gap", resolved.gap);
1902
+ }
1903
+ return overrides;
1904
+ }
1905
+ function subgridTracks(original, computed, view, axis) {
1906
+ const parent = original.parentElement;
1907
+ if (!parent)
1908
+ return null;
1909
+ const parentStyle = view.getComputedStyle(parent);
1910
+ if (!/grid$/.test(parentStyle.getPropertyValue("display")))
1911
+ return null;
1912
+ if (axis === "columns" && parentStyle.getPropertyValue("direction") === "rtl")
1913
+ return null;
1914
+ // Grid containers report their used track sizes; line names are skipped.
1915
+ const rawTracks = parentStyle.getPropertyValue(axis === "columns" ? "grid-template-columns" : "grid-template-rows");
1916
+ const sizes = [];
1917
+ for (const token of rawTracks.replace(/\[[^\]]*\]/g, " ").trim().split(/\s+/).filter(Boolean)) {
1918
+ const match = token.match(/^(\d+(?:\.\d+)?(?:e[+-]?\d+)?)px$/i);
1919
+ if (!match)
1920
+ return null;
1921
+ sizes.push(Number.parseFloat(match[1]));
1922
+ }
1923
+ if (sizes.length === 0)
1924
+ return null;
1925
+ const gapProperty = axis === "columns" ? "column-gap" : "row-gap";
1926
+ const parentGap = Number.parseFloat(parentStyle.getPropertyValue(gapProperty)) || 0;
1927
+ const start = axis === "columns" ? "left" : "top";
1928
+ const end = axis === "columns" ? "right" : "bottom";
1929
+ const parentRect = parent.getBoundingClientRect();
1930
+ const rect = original.getBoundingClientRect();
1931
+ const pixels = (style, property) => Number.parseFloat(style.getPropertyValue(property)) || 0;
1932
+ const contentStart = parentRect[start] + pixels(parentStyle, `border-${start}-width`) + pixels(parentStyle, `padding-${start}`);
1933
+ const startEdges = pixels(computed, `margin-${start}`) + pixels(computed, `border-${start}-width`) + pixels(computed, `padding-${start}`);
1934
+ const endEdges = pixels(computed, `margin-${end}`) + pixels(computed, `border-${end}-width`) + pixels(computed, `padding-${end}`);
1935
+ const areaStart = rect[start] - pixels(computed, `margin-${start}`);
1936
+ const areaEnd = rect[end] + pixels(computed, `margin-${end}`);
1937
+ // Track lines from the parent's content edge; content-distribution offsets
1938
+ // other than `normal`/`start` make the match fail and fall back.
1939
+ const trackStarts = [];
1940
+ let cursor = contentStart;
1941
+ for (const size of sizes) {
1942
+ trackStarts.push(cursor);
1943
+ cursor += size + parentGap;
1944
+ }
1945
+ const tolerance = 1;
1946
+ const first = trackStarts.findIndex((line) => Math.abs(line - areaStart) <= tolerance);
1947
+ if (first < 0)
1948
+ return null;
1949
+ let last = -1;
1950
+ for (let index = first; index < sizes.length; index += 1) {
1951
+ if (Math.abs(trackStarts[index] + sizes[index] - areaEnd) <= tolerance)
1952
+ last = index;
1953
+ }
1954
+ if (last < 0)
1955
+ return null;
1956
+ // The subgrid's own margin, border, and padding shrink its edge tracks.
1957
+ const spanned = sizes.slice(first, last + 1);
1958
+ spanned[0] = Math.max(spanned[0] - startEdges, 0);
1959
+ spanned[spanned.length - 1] = Math.max(spanned[spanned.length - 1] - endEdges, 0);
1960
+ const tracks = spanned
1961
+ .map((size) => (axis === "columns" ? `${size}px` : `minmax(${size}px, auto)`))
1962
+ .join(" ");
1963
+ const ownGap = computed.getPropertyValue(gapProperty).trim();
1964
+ return { tracks, gap: ownGap === "normal" ? `${parentGap}px` : null };
1965
+ }
1869
1966
  function authoredInsetOverrides(element, computed, view) {
1870
1967
  const authored = new Set();
1871
1968
  if ("style" in element)
@@ -2294,6 +2391,8 @@ async function materializeInlineSvgs(root, options, diagnostics) {
2294
2391
  try {
2295
2392
  if (!svg.hasAttribute("xmlns"))
2296
2393
  svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
2394
+ if (options.fallback !== "rasterize-subtree")
2395
+ omitOuterSvgBoxEffects(svg, options, diagnostics);
2297
2396
  const unsupportedReason = vectorSvgUnsupportedReason(svg);
2298
2397
  const source = serializeVectorSvg(svg);
2299
2398
  const replacement = svg.ownerDocument.createElement("img");
@@ -2313,7 +2412,7 @@ async function materializeInlineSvgs(root, options, diagnostics) {
2313
2412
  if (options.fallback === "error") {
2314
2413
  throw new UnsupportedCssError(`Inline SVG requires subtree rasterization: ${unsupportedReason}`);
2315
2414
  }
2316
- replacement.src = await rasterizeBlobToPng(new Blob([source], { type: "image/svg+xml" }));
2415
+ replacement.src = await rasterizeBlobToPng(new Blob([source], { type: "image/svg+xml" }), SVG_RASTER_SCALE);
2317
2416
  diagnostics.push({
2318
2417
  code: "CSS_SUBTREE_RASTERIZED",
2319
2418
  severity: "warning",
@@ -2342,6 +2441,23 @@ async function materializeInlineSvgs(root, options, diagnostics) {
2342
2441
  }
2343
2442
  return replacements;
2344
2443
  }
2444
+ /**
2445
+ * CSS `filter` and `mix-blend-mode` on the outer `<svg>` box are box effects,
2446
+ * like on any HTML element. Without an explicit rasterization opt-in they are
2447
+ * reported through the unsupported-CSS policy and omitted, so a decorative
2448
+ * drop-shadow keeps the SVG vector instead of failing the whole document.
2449
+ */
2450
+ function omitOuterSvgBoxEffects(svg, options, diagnostics) {
2451
+ for (const property of ["filter", "mix-blend-mode"]) {
2452
+ const value = svg.style.getPropertyValue(property);
2453
+ if (!value || !svgUnsupportedPropertyIsActive(property, value))
2454
+ continue;
2455
+ reportUnsupportedCss(property, options, diagnostics);
2456
+ // Edit the attribute text: CSSOM removal re-serializes the declaration
2457
+ // block and folds `marker-*` longhands into an unsupported `marker`.
2458
+ svg.setAttribute("style", stripStyleProperties(svg.getAttribute("style") ?? "", [property]));
2459
+ }
2460
+ }
2345
2461
  function serializeVectorSvg(source) {
2346
2462
  const svg = source.cloneNode(true);
2347
2463
  for (const element of [svg, ...svg.querySelectorAll("*")]) {
@@ -2957,19 +3073,24 @@ function canvasToPng(source) {
2957
3073
  context.drawImage(source, 0, 0);
2958
3074
  return canvas.toDataURL("image/png");
2959
3075
  }
2960
- async function rasterizeBlobToPng(blob) {
3076
+ /**
3077
+ * Device pixels per CSS pixel for rasterized SVG fallbacks. Layout keeps the
3078
+ * SVG's CSS size, so the extra pixels only sharpen the printed image.
3079
+ */
3080
+ const SVG_RASTER_SCALE = 2;
3081
+ async function rasterizeBlobToPng(blob, scale = 1) {
2961
3082
  const objectUrl = URL.createObjectURL(blob);
2962
3083
  try {
2963
3084
  const image = new Image();
2964
3085
  image.src = objectUrl;
2965
3086
  await image.decode();
2966
3087
  const canvas = document.createElement("canvas");
2967
- canvas.width = Math.max(image.naturalWidth, 1);
2968
- canvas.height = Math.max(image.naturalHeight, 1);
3088
+ canvas.width = Math.max(Math.round(image.naturalWidth * scale), 1);
3089
+ canvas.height = Math.max(Math.round(image.naturalHeight * scale), 1);
2969
3090
  const context = canvas.getContext("2d");
2970
3091
  if (!context)
2971
3092
  throw new Error("Canvas 2D context is unavailable");
2972
- context.drawImage(image, 0, 0);
3093
+ context.drawImage(image, 0, 0, canvas.width, canvas.height);
2973
3094
  return canvas.toDataURL("image/png");
2974
3095
  }
2975
3096
  finally {