html2canvas: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
var canvasPromise = html2canvas(document.body, {
allowTaint: true,
useCORS: true
});
canvasPromise.then(function(canvas) {
document.body.appendChild(canvas);
console.log(canvas);
canvas.toDataURL('image/png');
});
Bug reports:
Uncaught (in promise) DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.
- html2canvas version tested with:
- Chrome 67.0.3396.99
- Windows 10
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 17
Have you saw this: CORS_enabled_image
If the source of the foreign content is an HTML
element, attempting to retrieve the contents of the canvas isn’t allowed.
As soon as you draw into a canvas any data that was loaded from another origin without CORS approval, the canvas becomes tainted.
I use the configuration options like this:
You will need to use just the property ‘useCORS: true’, if you use the property ‘allowTaint: true’ you give permssion to turn your canvas into a tainted canvas
USE THIS:
INSTEAD OF THIS:
I have also the same issues. Did you find a solution or workaround?
Found a solution and it is working
While calling html2canvas,pass useCORS true html2canvas(selectorElement,{useCORS:true}).then(canvas => { //do something });
Correct html2canvas.js file. Theres typo mistake Change “anonymous” to “Anonymous” in this if block if (isInlineBase64Image(src) || useCORS) { img.crossOrigin = ‘Anonymous’; }
If you do like below, what will happen?
No solution as far as I know, but I have a workaround: change every image to base64. That way, you could render it in canvas even though it’s originally from different domain.
Hi, we faced the same problem. We followed @dorklord23 suggestion because we already had a proxy url that did the conversion.
If someone found it helpful the solution was:
Where the helper function convertAllImagesToBase64 is:
By the way this are the tests for that helper function (we are using jest for wrting test and mockFetch packages):
Ruby backend enpdoint:
I hope someone find this helpful. I hope it helps someone not to invest as much time as we did to fix this properly.
If you can see any improvment please suggest. Regards.
Setting foreignObjectRendering to true worked for me. html2canvas(document.body, { allowTaint: true, foreignObjectRendering: true });
this worked for my next.js app.
I have used your approach and get image from my backend as base64 and it works thank you so much for this idea