Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Further pdf export improvements (#76016)

Nick Fitzpatrick6d294e5f4d20
diff --git a/frontend/src/metabase/visualizations/lib/image-exports.ts b/frontend/src/metabase/visualizations/lib/image-exports.ts
index 5356ab59c3e..7e3359430f2 100644
--- a/frontend/src/metabase/visualizations/lib/image-exports.ts
+++ b/frontend/src/metabase/visualizations/lib/image-exports.ts
@@ -49,6 +49,38 @@ export const saveDomImageStyles = css`
}
`;
+const SVG_VAR_PAINT_ATTRIBUTES = [
+ "fill",
+ "stroke",
+ "stop-color",
+ "flood-color",
+ "lighting-color",
+];
+
+// html2canvas serializes <svg> to a standalone image where :root custom props are out of
+// scope, so var() paint (e.g. white pie-slice borders) is lost. Bake in the resolved value.
+export const resolveSvgVarPaint = (root: HTMLElement) => {
+ root.querySelectorAll<SVGElement>("svg, svg *").forEach((el) => {
+ SVG_VAR_PAINT_ATTRIBUTES.forEach((attr) => {
+ const value = el.getAttribute(attr);
+ if (value?.includes("var(")) {
+ const resolved = getComputedStyle(el).getPropertyValue(attr);
+ if (resolved) {
+ el.setAttribute(attr, resolved);
+ }
+ }
+ });
+ });
+};
+
+// html2canvas's clone wipes inline styles off SVG nodes (empty computed cssText in Chrome),
+// dropping overflow:visible so nested label <svg>s clip their text. Restore it for export.
+export const restoreNestedSvgOverflow = (root: HTMLElement) => {
+ root.querySelectorAll<SVGElement>("svg svg").forEach((svg) => {
+ svg.style.overflow = "visible";
+ });
+};
+
export const getDomToCanvas = async (
element: HTMLElement,
options: {
@@ -66,7 +98,11 @@ export const getDomToCanvas = async (
width: options.width,
height: options.height,
scale: options.scale,
- onclone: options.onclone,
+ onclone: (doc, node) => {
+ options.onclone?.(doc, node);
+ resolveSvgVarPaint(node);
+ restoreNestedSvgOverflow(node);
+ },
});
};
diff --git a/frontend/src/metabase/visualizations/lib/save-chart-image.ts b/frontend/src/metabase/visualizations/lib/save-chart-image.ts
index 03ade07d763..dc3fcefc089 100644
--- a/frontend/src/metabase/visualizations/lib/save-chart-image.ts
+++ b/frontend/src/metabase/visualizations/lib/save-chart-image.ts
@@ -7,6 +7,7 @@ import {
getBrandingConfig,
getBrandingSize,
} from "./exports-branding-utils";
+import { resolveSvgVarPaint, restoreNestedSvgOverflow } from "./image-exports";
export const SAVING_DOM_IMAGE_CLASS = "saving-dom-image";
export const SAVING_DOM_IMAGE_HIDDEN_CLASS = "saving-dom-image-hidden";
@@ -71,6 +72,9 @@ export const saveChartImage = async ({
node.appendChild(branding);
}
+
+ resolveSvgVarPaint(node);
+ restoreNestedSvgOverflow(node);
},
});
diff --git a/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts b/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
index ffe12215c17..9eed800cbce 100644
--- a/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
+++ b/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
@@ -11,7 +11,11 @@ import {
getBrandingConfig,
getBrandingSize,
} from "./exports-branding-utils";
-import { fixParameterLegendOffsetForExport } from "./image-exports";
+import {
+ fixParameterLegendOffsetForExport,
+ resolveSvgVarPaint,
+ restoreNestedSvgOverflow,
+} from "./image-exports";
import { SAVING_DOM_IMAGE_CLASS } from "./save-chart-image";
// DOM ids on exportable nodes so the PDF exporter and downloads thunk can find them
@@ -276,6 +280,9 @@ export const saveDashboardPdf = async ({
const branding = createBrandingElement(size);
node.insertBefore(branding, node.firstChild);
}
+
+ resolveSvgVarPaint(node);
+ restoreNestedSvgOverflow(node);
},
});
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx
index 3894e143e23..e9bf5fa8d3b 100644
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx
@@ -229,11 +229,11 @@ export function Gauge({
ref={labelRef}
x={0}
y={0}
+ textAnchor="middle"
style={{
fill: getCenterLabelColor(),
fontSize: "1em",
fontWeight: "bold",
- textAnchor: "middle",
transform: "translate(0,0.2em)",
}}
>
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx
index f50feb57fc4..a5cc10eb0e6 100644
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx
@@ -14,10 +14,12 @@ export const GaugeSegmentLabel = ({
style,
}: Props) => (
<text
+ // fill + textAnchor are set as attributes (not inline style) so they survive
+ // the html2canvas cssText copy on export; var() fill is resolved by resolveSvgVarPaint.
+ fill="var(--mb-color-text-secondary)"
+ textAnchor={Math.abs(x) < 5 ? "middle" : x > 0 ? "start" : "end"}
style={{
- fill: "var(--mb-color-text-secondary)",
fontSize: `${FONT_SIZE_SEGMENT_LABEL}px`,
- textAnchor: Math.abs(x) < 5 ? "middle" : x > 0 ? "start" : "end",
// shift text in the lower half down a bit
transform:
y > 0 ? `translate(0,${FONT_SIZE_SEGMENT_LABEL / 2}px)` : undefined,
diff --git a/patches/html2canvas-pro+2.0.2.patch b/patches/html2canvas-pro+2.0.2.patch
index b0c0484456e..6a6436d84b0 100644
--- a/patches/html2canvas-pro+2.0.2.patch
+++ b/patches/html2canvas-pro+2.0.2.patch
@@ -1,5 +1,5 @@
diff --git a/node_modules/html2canvas-pro/dist/html2canvas-pro.esm.js b/node_modules/html2canvas-pro/dist/html2canvas-pro.esm.js
-index d49f1bd..1387285 100644
+index d49f1bd..2f21002 100644
--- a/node_modules/html2canvas-pro/dist/html2canvas-pro.esm.js
+++ b/node_modules/html2canvas-pro/dist/html2canvas-pro.esm.js
@@ -5875,6 +5875,8 @@ class SVGElementContainer extends ElementContainer {
@@ -11,7 +11,63 @@ index d49f1bd..1387285 100644
const bounds = parseBounds(context, img);
img.setAttribute('width', `${bounds.width}px`);
img.setAttribute('height', `${bounds.height}px`);
-@@ -7534,7 +7536,8 @@ class ElementPaint {
+@@ -7144,14 +7146,48 @@ const ignoredStyleProperties = [
+ 'd', // #2483
+ 'content' // Safari shows pseudoelements if content is set
+ ];
++const svgTextFontProperties = [
++ 'font-family',
++ 'font-size',
++ 'font-weight',
++ 'font-style',
++ 'font-variant',
++ 'font-stretch',
++ 'font-size-adjust',
++ 'letter-spacing',
++ 'word-spacing',
++ 'line-height',
++];
++const isSVGTextNode = (node) => {
++ const tagName = node.tagName;
++ return tagName === 'text' || tagName === 'tspan' || tagName === 'textPath';
++};
+ const copyCSSStyles = (style, target) => {
+- // Edge does not provide value for cssText
+- for (let i = style.length - 1; i >= 0; i--) {
+- const property = style.item(i);
+- // fix: Chrome_138 ignore custom properties
+- if (ignoredStyleProperties.indexOf(property) === -1 && !property.startsWith('--')) {
+- target.style.setProperty(property, style.getPropertyValue(property));
+- }
++ // Use cssText for a single native call instead of iterating 300+ properties
++ // individually via style.item(i), which is extremely slow in Firefox due to
++ // Stylo's per-call indexmap lookups in the custom properties map.
++ target.style.cssText = style.cssText;
++ // Remove properties that should be ignored
++ for (let i = ignoredStyleProperties.length - 1; i >= 0; i--) {
++ target.style.removeProperty(ignoredStyleProperties[i]);
++ }
++ // For SVG text elements, explicitly re-apply font properties from the
++ // computed style since cssText may not reliably serialize them.
++ if (isSVGTextNode(target)) {
++ for (let i = 0; i < svgTextFontProperties.length; i++) {
++ const value = style.getPropertyValue(svgTextFontProperties[i]);
++ if (value) {
++ target.style.setProperty(svgTextFontProperties[i], value);
++ }
++ }
++ }
++ // cssText is empty for computed styles in Chrome, which drops the SVG's transform and
++ // offsets its serialized bounds, re-breaking #68751. Re-apply transform props explicitly.
++ const transform = style.getPropertyValue('transform');
++ if (transform && transform !== 'none') {
++ target.style.setProperty('transform', transform);
++ target.style.setProperty('transform-origin', style.getPropertyValue('transform-origin'));
++ target.style.setProperty('transform-box', style.getPropertyValue('transform-box'));
+ }
+ return target;
+ };
+@@ -7534,7 +7570,8 @@ class ElementPaint {
const rotateMatrix = [cos, sin, -sin, cos, 0, 0];
this.effects.push(new TransformEffect(offsetX, offsetY, rotateMatrix));
}
@@ -21,7 +77,7 @@ index d49f1bd..1387285 100644
const origin = this.container.styles.transformOrigin;
const offsetX = this.container.bounds.left + getAbsoluteValue(origin[0], this.container.bounds.width);
const offsetY = this.container.bounds.top + getAbsoluteValue(origin[1], this.container.bounds.height);
-@@ -10373,12 +10376,12 @@ class Validator {
+@@ -10373,12 +10410,12 @@ class Validator {
}
// Basic format validation - nonce should be base64-like
// Typical format: base64 string, often 32+ characters