Resolved! Uncaught (in promise) DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported

In my previous post, we created a Google Map Static API image using a Get URL. In one of my project, there was a requirement of HTML conversion into PDF document. So I wanted to add that Map Image into that PDF document. I used html2canvas and jsPDF libraries for that.

When I was trying to convert an HTML section with Google Static Map Image into the canvas, I faced a strange issue.

Uncaught (in promise) DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.

After some research I got know that it was not a bug in any of the libraries I used, It was a limitation to prevent unauthorized use of external usage of images in canvas in HTML5. You can see Map Image URL in this post.

Solution

As a workaround

Step 1) Sent Google Map Static Image URL to my server.
Step 2) Copied that Map URL on my server drive using server-side PHP.
Step 3) Got server path of Image saved.
Step 4) Used same server Image in HTML to use in Canvas.

The error is resolved!

Sent Google Image URL

HTML section

Server side PHP

For details on getPDF() function see this post in detail on How to convert HTML to PDF document.

 

Leave a Reply

Your email address will not be published. Required fields are marked *