Skip to content

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

update html2canvas-pro (#70728)

Nick Fitzpatrickb773f68f23ee
diff --git a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Dark_Theme_Download.png b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Dark_Theme_Download.png
index 0beaf6c8dca..936055370d1 100644
Binary files a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Dark_Theme_Download.png and b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Dark_Theme_Download.png differ
diff --git a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Light_Theme_Download.png b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Light_Theme_Download.png
index 133c7e9a703..bacd99b6ce3 100644
Binary files a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Light_Theme_Download.png and b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedQuestionView_Light_Theme_Download.png differ
diff --git a/.loki/reference/chrome_laptop_viz_GridMapPdfExport_Grid_Map_Pdf_Export.png b/.loki/reference/chrome_laptop_viz_GridMapPdfExport_Grid_Map_Pdf_Export.png
index e5d2de33d0c..2f40ada69ba 100644
Binary files a/.loki/reference/chrome_laptop_viz_GridMapPdfExport_Grid_Map_Pdf_Export.png and b/.loki/reference/chrome_laptop_viz_GridMapPdfExport_Grid_Map_Pdf_Export.png differ
diff --git a/bun.lock b/bun.lock
index 74f45e2d14a..634eea239d8 100644
--- a/bun.lock
+++ b/bun.lock
@@ -102,7 +102,7 @@
"hast-util-from-html": "^2.0.1",
"hast-util-to-html": "^9.0.0",
"history": "3",
- "html2canvas-pro": "1.5.0",
+ "html2canvas-pro": "^2.0.2",
"humanize-plus": "^1.8.1",
"icepick": "2.4.0",
"iframe-resizer": "~4.3.2",
@@ -3270,7 +3270,7 @@
"html2canvas": ["[email protected]", "", { "dependencies": { "css-line-break": "^2.1.0", "text-segmentation": "^1.0.3" } }, "sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA=="],
- "html2canvas-pro": ["[email protected]", "", { "dependencies": { "css-line-break": "^2.1.0", "text-segmentation": "^1.0.3" } }, "sha512-izxSphcINRwfEVV6eamsPVdhsxSYqX8n/hxzK+niVWdB+onM+aYRoVO+xgS9iMmZoUleZTWg1tJwryikib2hXg=="],
+ "html2canvas-pro": ["[email protected]", "", { "dependencies": { "css-line-break": "^2.1.0", "text-segmentation": "^1.0.3" } }, "sha512-9G/t0XgCZWonLwL0JwI7su6NdbOPUY7Ur4Ihpp8+XMaW9ibA2nDXF181Jr6tm94k8lX6sthpaXB3XqEnsMd5Cw=="],
"htmlparser2": ["[email protected]", "", { "dependencies": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", "domutils": "^2.5.2", "entities": "^2.0.0" } }, "sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A=="],
diff --git a/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts b/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
index 53f4ddc9dbf..6d3600e858e 100644
--- a/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
+++ b/frontend/src/metabase/visualizations/lib/save-dashboard-pdf.ts
@@ -233,15 +233,10 @@ export const saveDashboardPdf = async ({
width: contentWidth,
useCORS: true,
backgroundColor,
- scale: window.devicePixelRatio || 1,
- /**
- * html2canvas-pro creates inline <style> elements that can be blocked by
- * CSP (observed from Firefox). We created a temporary patch to support
- * nonce until the library officially implements it.
- *
- * @see https://github.com/metabase/metabase/issues/66234
- */
- nonce: window.MetabaseNonce,
+ scale: Math.min(window.devicePixelRatio || 1, 2),
+ // We have patched html2canvas so that we can use our Nonce token. Without the patch, it complains
+ // that the token is not long enough
+ cspNonce: window.MetabaseNonce,
onclone: (_doc: Document, node: HTMLElement) => {
node.classList.add(SAVING_DOM_IMAGE_CLASS);
node.style.height = `${contentHeight}px`;
@@ -372,9 +367,15 @@ export const saveDashboardPdf = async ({
}
}
+ pageCanvas.width = 0;
+ pageCanvas.height = 0;
+
prevBreak = pageBreak;
});
+ image.width = 0;
+ image.height = 0;
+
pdf.save(fileName);
};
diff --git a/package.json b/package.json
index bbd2a422c9d..dbc41f7ea5e 100644
--- a/package.json
+++ b/package.json
@@ -107,7 +107,7 @@
"hast-util-from-html": "^2.0.1",
"hast-util-to-html": "^9.0.0",
"history": "3",
- "html2canvas-pro": "1.5.0",
+ "html2canvas-pro": "^2.0.2",
"humanize-plus": "^1.8.1",
"icepick": "2.4.0",
"iframe-resizer": "~4.3.2",
diff --git a/patches/html2canvas-pro+1.5.0.patch b/patches/html2canvas-pro+1.5.0.patch
deleted file mode 100644
index e4882af9cb6..00000000000
--- a/patches/html2canvas-pro+1.5.0.patch
+++ /dev/null
@@ -1,85 +0,0 @@
-diff --git a/node_modules/html2canvas-pro/dist/html2canvas-pro.js b/node_modules/html2canvas-pro/dist/html2canvas-pro.js
-index 86a610e..79c8cc6 100644
---- a/node_modules/html2canvas-pro/dist/html2canvas-pro.js
-+++ b/node_modules/html2canvas-pro/dist/html2canvas-pro.js
-@@ -802,6 +802,7 @@
- : undefined;
- return [indicies, classTypes, forbiddenBreakpoints];
- };
-+ var nonceAttributeValue
- var Break = /** @class */ (function () {
- function Break(codePoints, lineBreak, start, end) {
- this.codePoints = codePoints;
-@@ -5347,6 +5348,8 @@
- function SVGElementContainer(context, img) {
- var _this = _super.call(this, context, img) || this;
- var s = new XMLSerializer();
-+ // Remove transform to prevent double-application (transform is already accounted for in bounds/positioning)
-+ img.style.transform = "none";
- var bounds = parseBounds(context, img);
- img.setAttribute('width', bounds.width + "px");
- img.setAttribute('height', bounds.height + "px");
-@@ -6060,6 +6063,11 @@
- documentClone.write(serializeDoctype(document.doctype) + "<html></html>");
- // Chrome scrolls the parent document for some reason after the write to the cloned window???
- restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY);
-+
-+ if (isStyleElement(documentClone.documentElement) && nonceAttributeValue) {
-+ documentClone.documentElement.setAttribute('nonce', nonceAttributeValue);
-+ }
-+
- documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement);
- documentClone.close();
- return iframeLoad;
-@@ -6188,6 +6196,11 @@
- (!isScriptElement(child) &&
- !child.hasAttribute(IGNORE_ATTRIBUTE) &&
- (typeof this.options.ignoreElements !== 'function' || !this.options.ignoreElements(child)))) {
-+
-+ if (isStyleElement(child) && nonceAttributeValue) {
-+ child.setAttribute('nonce', nonceAttributeValue);
-+ }
-+
- if (!this.options.copyStyles || !isElementNode(child) || !isStyleElement(child)) {
- clone.appendChild(this.cloneNode(child, copyStyles));
- }
-@@ -6464,6 +6477,9 @@
- var document = body.ownerDocument;
- if (document) {
- var style = document.createElement('style');
-+ if (nonceAttributeValue) {
-+ style.setAttribute('nonce', nonceAttributeValue);
-+ }
- style.textContent = styles;
- body.appendChild(style);
- }
-@@ -6923,7 +6939,8 @@
- if (this.container.styles.opacity < 1) {
- this.effects.push(new OpacityEffect(this.container.styles.opacity));
- }
-- if (this.container.styles.transform !== null) {
-+ // Skip TransformEffect for SVG elements - their transform is already baked into bounds
-+ if (this.container.styles.transform !== null && !(this.container instanceof SVGElementContainer)) {
- var offsetX = this.container.bounds.left + this.container.styles.transformOrigin[0].number;
- var offsetY = this.container.bounds.top + this.container.styles.transformOrigin[1].number;
- var matrix = this.container.styles.transform;
-@@ -8570,6 +8587,7 @@
-
- var html2canvas = function (element, options) {
- if (options === void 0) { options = {}; }
-+ nonceAttributeValue = options.nonce
- return renderElement(element, options);
- };
- if (typeof window !== 'undefined') {
-diff --git a/node_modules/html2canvas-pro/dist/types/index.d.ts b/node_modules/html2canvas-pro/dist/types/index.d.ts
-index c481902..60b03fe 100644
---- a/node_modules/html2canvas-pro/dist/types/index.d.ts
-+++ b/node_modules/html2canvas-pro/dist/types/index.d.ts
-@@ -5,6 +5,7 @@ export declare type Options = CloneOptions & WindowOptions & RenderOptions & Con
- backgroundColor: string | null;
- foreignObjectRendering: boolean;
- removeContainer?: boolean;
-+ nonce?: string;
- };
- declare const html2canvas: (element: HTMLElement, options?: Partial<Options>) => Promise<HTMLCanvasElement>;
- export default html2canvas;
diff --git a/patches/html2canvas-pro+2.0.2.patch b/patches/html2canvas-pro+2.0.2.patch
new file mode 100644
index 00000000000..b0c0484456e
--- /dev/null
+++ b/patches/html2canvas-pro+2.0.2.patch
@@ -0,0 +1,42 @@
+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
+--- 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 {
+ constructor(context, img) {
+ super(context, img);
+ const s = new XMLSerializer();
++ // Remove transform to prevent double-application (transform is already accounted for in bounds/positioning)
++ img.style.transform = "none";
+ const bounds = parseBounds(context, img);
+ img.setAttribute('width', `${bounds.width}px`);
+ img.setAttribute('height', `${bounds.height}px`);
+@@ -7534,7 +7536,8 @@ class ElementPaint {
+ const rotateMatrix = [cos, sin, -sin, cos, 0, 0];
+ this.effects.push(new TransformEffect(offsetX, offsetY, rotateMatrix));
+ }
+- if (this.container.styles.transform !== null) {
++ // Skip TransformEffect for SVG elements - their transform is already baked into bounds
++ if (this.container.styles.transform !== null && !(this.container instanceof SVGElementContainer)) {
+ 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 {
+ }
+ // Basic format validation - nonce should be base64-like
+ // Typical format: base64 string, often 32+ characters
+- if (nonce.length < 16) {
+- return {
+- valid: false,
+- error: 'CSP nonce is too short (minimum 16 characters recommended)'
+- };
+- }
++ // if (nonce.length < 16) {
++ // return {
++ // valid: false,
++ // error: 'CSP nonce is too short (minimum 16 characters recommended)'
++ // };
++ // }
+ // Check for suspicious characters
+ if (!/^[A-Za-z0-9+/=_-]+$/.test(nonce)) {
+ return {