html-to-image: "Error: Uncaught (in promise): Event: {"isTrusted":true}" while rendering container in FF
While trying to render div container with a lot of angular material chechboxes using htmlToImage.toPng i received an error:
Error: Uncaught (in promise): Event: {"isTrusted":true}
It seems that this error appeared after upgrading Firefox to version 97 or above (it works fine in current Chrome and Edge, and also worked fine in FF<=96.0.3). If i reduce number of components inside container: for example: from 400 to 100-200 it renders without error.
Steps To Reproduce
Here is code that reproduces bug: https://stackblitz.com/edit/angular-ivy-bq1svc Try running it from Firefox (version 97 or above) and click “Export”.
Additional Context
Same behavior is when instead of mat-checkbox i use mat-table for example. On the other hand when I replace mat-checkbox with mat-radio-button the error is gone.
Your Environment
- html-to-image: [e.g. 1.9.0]
- OS: [Windows 10 Pro]
- Browser: [Firefox 97.0.1]
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 12
- Comments: 36 (5 by maintainers)
bump. Why isn’t the bot removing the stale tag?
Seems like there is a history on this repo of issues in this queue getting ignored and then closed by the stale bot for no good reason. It’s fine if it takes a long time to get around to investigating issues, but automatically closing them because no one has got to them yet is extremely frustrating.
In case someone else has the same problem as I: For me, this error appeared, when I had an <img> with an empty src attribute (src=‘’). Removing the image (or adding a valid src) solved the problem.
I think I know the problem with the first. The svg (after prettier) is ~30MB, the flat dataUrl is ~40MB. After updating to have a much smaller list, the png generated without issue.
MDN Reference for quote
It seems like in general this error is browser limitations around dataUrls and therefore not really a bug for this project.
I do agree the error message could be more clear though. I’m sure we could add a message when
img.onerrorhere to say “There may be a browser limitation or data issue encountered when creating animgelement needed to render the result.”@andymcmullan Thanks Andy! I missed the decodeURIComponent step in my work, but once included it worked like a charm - thanks again.
FYI: In case it helps anyone else . . . We use a library which contains a large number of css variable references at the
:root {...}level. In Chrome|Edge, when runningtoSVG, the css styles never included those css variable references. But in Firefox, not only were those css variable references retained, they were duplicated for every node that was cloned, and when this was a table reference node, every cell in that table had a copy of these css variable references causing severe css bloat in the svg.Our fix was to use Andy’s suggestion (objectUrl instead of dataUrl). But in addition, we also removed all of the the
root: {}level css elements which we knew were not being used. A bit dangerous - yes, but for our use-case we knew that none of those css variables retained in firefox were needed for the svg to be properly rendered.I would propose an additional filter option (i.e. like the node element filter) that would operate on the svg-output - some sort of SVG-optimze filter function - that could be added as an option. That would be enough - at least in our case, to work around the firefox problem without having to extract/copy code as a workaround in html-to-image.
I encountered this issue when converting a complex page to PNG on Firefox.
In my case it was due to breaching the Firefox 32 MByte data URI limit on Firefox, as mentioned in other comments.
With the hints on this thread I was able to work around the issue by getting
html-to-imageto do the hard part (HTML to SVG), via thetoSvg()function, then taking over the SVG-to-PNG part by using a similar mechanism tohtml-to-image, but instead of attaching the SVG to an Image element using a data URI I use an object URL instead, which has much higher size limits.So my code is roughly:
Unfortunately this approach doesn’t seem to work for Chrome, which complains about the canvas being tainted. So I’ve had to special case usage of this code for Firefox only, and use the standard
toPng()for other browsers.The bug is related to firefox image limitations (some sort of max height is 32676px). When canvas/image have more height - Firefox throws an error. Workaround is to:
Use patch-package if needed.
The same can be done to convert to image.
toCanvasmethod).toCanvasmethod).