Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export async function exportImage(
applyStyles(svg, previousStyles);
exportDoc.body.replaceChildren(elementClone);
exportDoc.body.style.margin = '0px';
/* Set display block through styles to ensure that CSS rules that target `body` don't accidentally target this
* iframe's body, which might cause the body to have no intrinsic width or height, leading to the canvas having a
* size of 0px, which causes the `toBlob` call to return null. */
exportDoc.body.style.display = 'block';
/* The body's parent has a width of 0, so we use fit-content to ensure that the body adjusts its width to the width
* of its children. */
exportDoc.body.style.width = 'fit-content';
Expand Down Expand Up @@ -83,6 +87,13 @@ export async function exportImage(
canvas.style.width = `${exportDocBodySize.width}px`;
canvas.style.height = `${exportDocBodySize.height}px`;

if (canvas.width === 0 || canvas.height === 0) {
doc.body.removeChild(iframe);
throw new Error(
`MUI X Charts: Cannot export an image with zero width or height. Width: ${canvas.width}px. Height: ${canvas.height}px.`,
);
}

try {
await drawDocument(iframe.contentDocument!, canvas, {
// Handle retina displays: https://github.com/cburgmer/rasterizeHTML.js/blob/262b3404d1c469ce4a7750a2976dec09b8ae2d6c/examples/retina.html#L71
Expand Down
Loading