Reputation: 860
I'm trying to get screenshot using html2canvas. It's working fine for text but it is not rendering CDN images, if i host the images in server it was working fine but if trying to load images from CDN links those image were not rendering.
My code is:
index.php
<div id="target">
<img id="editor_Img_1" src="http://d2j259rizy5u5h.cloudfront.net/outputvideos/thumbs/email_44_2015_03_02_54f462457526c-00001.png" alt="event-video" style="height: 200px; width: 320px;">
</div>
function capture() {
$('#target').html2canvas({
onrendered: function (canvas) {
var img = canvas.toDataURL();
console.log(img);
$('<img>',{src:img}).appendTo($('img'));
//Set hidden field's value to image data (base-64 string)
$('#img_val').val(canvas.toDataURL("image/png"));
//Submit the form manually
document.getElementById("myForm").submit();
}
});
}
Save.php
<?php
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
echo $filteredData;
//Save the image
file_put_contents('img.png', $unencodedData);
?>
<h2>Save the image and show to user</h2>
<table>
<tr>
<td>
<a href="img.png" target="blank">
Click Here to See The Image Saved to Server</a>
</td>
<td align="right">
<a href="index.php">
Click Here to Go Back</a>
</td>
</tr>
<tr>
<td colspan="2">
<br />
<br />
<span>
Here is Client-sided image:
</span>
<br />
<?php
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
?>
</td>
</tr>
</table>
Upvotes: 3
Views: 7453
Reputation: 1587
You can do so by passing one of the option in html2canvas function.
html2canvas(document.body,
{
useCORS: true, //By passing this option in function Cross origin images will be rendered properly in the downloaded version of the PDF
onrendered: function (canvas) {
//your functions here
}
});
Upvotes: 4
Reputation: 4818
OK, well I figured out what the problem is. html2canvas can't capture an image if it's not from the same origin. Images are coming from a CDN. There are some limitation to html to canvas
Limitations
All the images that the script uses need to reside under the same origin for it to be able to read them without the assistance of a proxy. Similarly, if you have other canvas elements on the page, which have been tainted with cross-origin content, they will become dirty and no longer readable by html2canvas.
The script doesn't render plugin content such as Flash or Java applets. It doesn't render iframe content either.
Upvotes: 2