I know similar questions have been asked before but I still can't make it work. I have a div
with images inside of it loaded from a bucket in AWS s3, they load perfectly no problem.
Now I want to be able to save as a jpeg whatever is in that specific div
(like taking a screenshot), the plugin html2canvas
helps with that. The problem is that when I try to actually save it (or simply show immediately the result of such screenshot) I run into these issues:
Canvas is tainted => I set allowTaint: true
in the plugin but it would throw this error, so I disabled it and the error went away. I keep useCORS
set to true
though to allow images from another source.
Access to image has been blocked by CORS policy
In order to solve this I set up CORS on my AWS S3 bucket, but that didn't seem to work (or it worked partially). I noticed that the response header of those images don't have CORS metadata when the plugin uses them to generate the jpeg. I then tried to set crossOrigin="anonymous"
in those images inside the div
but it would throw a CORS error right away, which shouldn't happen since the AWS bucket has been set up for that as follows:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
I am running out of options on how to make this work. Any idea on how to proceed from here would be very appreciated.
EDIT: More details, I am using React and the images urls are retrieved from a server. This means that as soon as I get this array of urls I generate:
<div>
{ urls.map(url => <img src={url} alt="some alt" />) }
</div>
If I add the crossOrigin="anonymous"
I get the CORS error. If I leave that out, the images display but then the html2canvas
plugin throws a CORS error as well when trying to generate the "screenshot".
More details about the HTTP requests. So the first time I load an image inside the div
, this is the Response Header:
Accept-Ranges:bytes
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Cache-Control:max-age=2592000
Content-Length:508208
Content-Type:image/png
Date:Thu, 16 Feb 2017 18:25:05 GMT
Last-Modified:Wed, 15 Feb 2017 19:09:44 GMT
Server:AmazonS3
Vary:Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Now this works if crossOrigin='anonymous'
and the picture is not from the cache. If the crossOrigin
attribute is not set I get:
Accept-Ranges:bytes
Cache-Control:max-age=2592000
Content-Length:508208
Content-Type:image/png
Date:Thu, 16 Feb 2017 19:03:53 GMT
Last-Modified:Wed, 15 Feb 2017 19:09:44 GMT
Server:AmazonS3
or it throws a CORS error on the console without showing any meta data on the response header. I tried adding a random string at the end of the url (?somethingsomething
) so that they would never be grabbed from the cache, and that fixed the issue completely. But this is just a hack and it works for now but it is not definitely the solution I am looking for. I think Chrome is doing something with the cache and I have a hard time tracking the source of the issue, other than the fact that it's hard to reproduce this problem on my machine since it always retrieved the screenshot from cache even if I am using completely new images and disable/clear cache. It's very confusing.
HTML provides a crossorigin attribute for images that, in combination with an appropriate CORS header, allows images defined by the <img> element that are loaded from foreign origins to be used in a <canvas> as if they had been loaded from the current origin.
The crossorigin attribute on an <img> tag specifies that CORS is supported when loading an image from a third party server or domain.
See the edit, I did try setting the crossOrigin attribute with no luck, and I use useCORS set to true (forgot to mention that sorry). Still no luck.
I fixed some cors issues I was having with the combination of Google Chrome, AWS S3, and multiple origins.
I found this stackoverflow thread: Chrome + CORS + cache - requesting same file from two different origins
Which links to this bug report: https://bugs.chromium.org/p/chromium/issues/detail?id=260239
Anyhow as workaround solution you can try this modified version of html2canvas: https://gist.github.com/CrandellWS/6bc2078aced496004d7a045e6360f19b
use the options:
allowTaint : false,
useCORS: true
Hope that helps.
FYI, this will add the current time stamp to cors image urls to sidestep a cache issue I was having on Chrome... https://gist.github.com/CrandellWS/6bc2078aced496004d7a045e6360f19b#file-html2canvas-js-L6838
Which means it will effect performance by re-downloading those images...
original post: https://github.com/niklasvh/html2canvas/issues/1544#issuecomment-435640901
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With