html2canvas does not render images located in the element

Created on 9 Nov 2015  ·  36Comments  ·  Source: niklasvh/html2canvas

Let's say if I would add text and a background color, then I'd press save, it would show the text and background color, but if I add an image into the div it won't show the image, but still the other things.

Most helpful comment

Insert de option allowTaint to view all images into your screenshot

html2canvas(document.getElementById('invoice-panel'), { letterRendering: 1, allowTaint : true, onrendered : function (canvas) { } });

All 36 comments

Hi.
I have exactly the same problem.

Same issue here.
I'm trying to get the shot of a tweet, and the attached image doesn't come through.
Image attached.

stream-item-tweet-670397195221241856 2

I believe images from other sites are blocked by default because it "taints the HTML5 canvas". Here is the option you need to use when you call the renderer:

attribute: allowTaint
type: boolean
default: false
description: Whether to allow cross-origin images to taint the canvas

Found it here: https://html2canvas.hertzen.com/documentation.html.

same problem....
and my images are coming from same orign

I have faced the same issue, the best way to solve the issue is to run the project on the server and see the result.
In my case, earlier i was running the html files directly without any server, but then after googling a bit i got a solution to try it on a server.
I tried my project using Visual Studio and it was running perfectly, my images inside div tag was also being copied

Insert de option allowTaint to view all images into your screenshot

html2canvas(document.getElementById('invoice-panel'), { letterRendering: 1, allowTaint : true, onrendered : function (canvas) { } });

I have list of div element in.
When i try to capture each div by id, then some of the div half-portion captured, and some of the div's capture image blank.
can you tell me what is the problem and how can i fixed it. @

The same problem.Thanks for the solution.

html2canvas not converting imges given src in base64 format. I have printed the log. Kindly help

1283ms html2canvas: Document cloned html2canvas.js:1487
3936ms html2canvas: Initialized CanvasRenderer with size 601 x 965 html2canvas.js:1487
3937ms html2canvas: Starting NodeParser html2canvas.js:1487
4014ms html2canvas: Fetched nodes, total: 10 html2canvas.js:1487
4015ms html2canvas: Calculate overflow clips html2canvas.js:1487
4034ms html2canvas: Start fetching images html2canvas.js:1487
4044ms html2canvas: Added image #1 data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pL html2canvas.js:1487
4047ms html2canvas: Added image #2 data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH html2canvas.js:1487
4051ms html2canvas: Finished searching images html2canvas.js:1487
4052ms html2canvas: Succesfully loaded image #1
ImageContainer {src: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…AKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/2Q==", image: img, tainted: null, promise: h}
html2canvas.js:1487
4056ms html2canvas: Succesfully loaded image #2
ImageContainer {src: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…4PXOaUMB0/GlrcdiYtkcc/WkzjqfwqMPxzxRvAz3oSCx//9k=", image: img, tainted: null, promise: h}
html2canvas.js:1487
4059ms html2canvas: Images loaded, starting parsing html2canvas.js:1487
4060ms html2canvas: Creating stacking contexts html2canvas.js:1487
4064ms html2canvas: Sorting stacking contexts html2canvas.js:1487
4066ms html2canvas: Render queue created with 9 items html2canvas.js:1487
4104ms html2canvas: Finished rendering html2canvas.js:1487
4109ms html2canvas: Cropping canvas at: left: 28 top: 529 width: 545 height: 0 html2canvas.js:1487
4111ms html2canvas: Resulting crop with width 545 and height 0 with x 28 and y 529

html2canvsas not rendering images given with base64 src, call is not coming back to "onrendered"
function. Kindly help

1283ms html2canvas: Document cloned html2canvas.js:1487
3936ms html2canvas: Initialized CanvasRenderer with size 601 x 965 html2canvas.js:1487
3937ms html2canvas: Starting NodeParser html2canvas.js:1487
4014ms html2canvas: Fetched nodes, total: 10 html2canvas.js:1487
4015ms html2canvas: Calculate overflow clips html2canvas.js:1487
4034ms html2canvas: Start fetching images html2canvas.js:1487
4044ms html2canvas: Added image #1 data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pL html2canvas.js:1487
4047ms html2canvas: Added image #2 data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH html2canvas.js:1487
4051ms html2canvas: Finished searching images html2canvas.js:1487
4052ms html2canvas: Succesfully loaded image #1
ImageContainer {src: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…AKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/2Q==", image: img, tainted: null, promise: h}
html2canvas.js:1487
4056ms html2canvas: Succesfully loaded image #2
ImageContainer {src: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD…4PXOaUMB0/GlrcdiYtkcc/WkzjqfwqMPxzxRvAz3oSCx//9k=", image: img, tainted: null, promise: h}
html2canvas.js:1487
4059ms html2canvas: Images loaded, starting parsing html2canvas.js:1487
4060ms html2canvas: Creating stacking contexts html2canvas.js:1487
4064ms html2canvas: Sorting stacking contexts html2canvas.js:1487
4066ms html2canvas: Render queue created with 9 items html2canvas.js:1487
4104ms html2canvas: Finished rendering html2canvas.js:1487
4109ms html2canvas: Cropping canvas at: left: 28 top: 529 width: 545 height: 0 html2canvas.js:1487
4111ms html2canvas: Resulting crop with width 545 and height 0 with x 28 and y 529

@SebasAlvarez - the allowTaint option doesn't work anymore. Does it still work for you?

@SebasAlvarez , @sandeepnagra :
am using this code for image screenshot in new tab but image not rendering and also i tired with "allowTaint:true" it's working only in inside same window but not in new tab.
this is my code:
function render(){
html2canvas(document.body, { allowTaint : true,
onrendered: function(canvas) {
document.body.appendChild(canvas);
window.open(canvas.toDataURL());
}
});
}
can you tell me what is the problem, how can you fix this one??

@venkateshduddu - are you shifting the focus to new tab first before taking screenshot?

@sandeepnagra - no sandeep, am just using click event. one more thing screenshot working if i use local image path but server path not working in new tab.

@venkateshduddu - a click event won't shift the focus to new tab automatically, you have to trigger that. So try that first, I am 100% sure it will solve your issue.

Regarding the server path, is that network drive authenticated? If yes, are you authenticating before uploading files to the network drive? It's a whole different story if you want to upload them to S3 bucket.

hi when i try to screen an iframe with internal ip camera, it return a void image, please can you help me?

If I used allowTaint then download option not worked . Please help

same problem....
and my images are coming from same orign

I just fixed this problem.
pls enable the option 'useCORS' to 'true'
codes bellow:
html2canvas( document.querySelector(".preview_area"), { logging: true, letterRendering: 1, // allowTaint: false useCORS: true } ).then(canvas => { })

Thank you very much @Sotyoyo , My bug was fixed with the above code. THank you Thank you

@Sotyoyo Thanks a lot bro, god bless you <3

@Sotyoyo Thank you so much, its work for me :)

Image is not render is there any problem in my code ?

html2canvas(document.getElementById("html-2-pdfwrapper"), {
logging: true,
letterRendering: 1,
allowTaint: false,
useCORS: true,
onrendered: function(canvas) {
var img = canvas.toDataURL('image/png');
var doc = new jsPDF();
doc.addImage(img, 'JPEG', 20, 20);
doc.save('test2.pdf');
}
});

same problem....
and my images are coming from same orign

I just fixed this problem.
pls enable the option 'useCORS' to 'true'
codes bellow:
html2canvas( document.querySelector(".preview_area"), { logging: true, letterRendering: 1, // allowTaint: false useCORS: true } ).then(canvas => { })

its work! thx!

same problem....
and my images are coming from same orign

I just fixed this problem.
pls enable the option 'useCORS' to 'true'
codes bellow:
html2canvas( document.querySelector(".preview_area"), { logging: true, letterRendering: 1, // allowTaint: false useCORS: true } ).then(canvas => { })

that's is right /

html2canvas(document.querySelector("#id-of-element"),{ logging: true, letterRendering: 1, allowTaint: false, useCORS: true }).then(canvas => {
var imgData = canvas.toDataURL('image/jpeg');
});

Insert de option allowTaint to view all images into your screenshot

html2canvas(document.getElementById('invoice-panel'), { letterRendering: 1, allowTaint : true, onrendered : function (canvas) { } });

Thanks. It works fine.

But I can't clone image style to canvas

I still face the same issue even after using above options below is my code.
var element = document.querySelector("#map-image-id");
html2canvas(element, {
logging: true,
letterRendering: 1,
allowTaint: false,
useCORS: true
}).then(canvasElm => {
var imageType = "image/png";
var imageData = canvasElm.toDataURL(imageType);
var src = encodeURI(imageData);
console.log("src", src);
console.log("image Data", imageData);

I still face the same issue even after using above options below is my code.
var element = document.querySelector("#map-image-id");
html2canvas(element, {
logging: true,
letterRendering: 1,
allowTaint: false,
useCORS: true
}).then(canvasElm => {
var imageType = "image/png";
var imageData = canvasElm.toDataURL(imageType);
var src = encodeURI(imageData);
console.log("src", src);
console.log("image Data", imageData);

same to you. Have you solved it?

I still face the same issue even after using above options below is my code.
var element = document.querySelector("#map-image-id");
html2canvas(element, {
logging: true,
letterRendering: 1,
allowTaint: false,
useCORS: true
}).then(canvasElm => {
var imageType = "image/png";
var imageData = canvasElm.toDataURL(imageType);
var src = encodeURI(imageData);
console.log("src", src);
console.log("image Data", imageData);

same issue with me any help

I'm seeing the same issue. What am I doing wrong?

html2canvas(document.getElementById(<id>), {
    logging: true,
    letterRendering: 1,
    allowTaint: false,
    useCORS: true
}).then(result => {
    var img = result.toDataURL("image/png");
    savePng(img);
});

The below also works if you want to include <img src-"..." />:

html2canvas(report, {letterRendering: 1, allowTaint: true, useCORS: true})
            .then((canvas) => {      
                const imgData = canvas.toDataURL('image/png');
               // const pdf = new jsPDF();
               // pdf.addImage(imgData, 'PNG', 10, 10);
                //pdf.save("foo.pdf");  
            });

My content is contains image that come from Aws.s3(). The above mentioned Comments are not working for my case.

html2canvas(content, {letterRendering: 1,allowTaint: false,useCORS:true })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
console.log(imgData);
const pdf = new jsPDF();
pdf.addImage(imgData, 'PNG', 10, 10, canvas.width, canvas.height);
pdf.save('download.pdf');
});

The output canvas only contains a blank box instead of image. Any one have a solution for my case?

My content is contains image that come from Aws.s3(). The above mentioned Comments are not working for my case.

html2canvas(content, {letterRendering: 1,allowTaint: false,useCORS:true })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
console.log(imgData);
const pdf = new jsPDF();
pdf.addImage(imgData, 'PNG', 10, 10, canvas.width, canvas.height);
pdf.save('download.pdf');
});

The output canvas only contains a blank box instead of image. Any one have a solution for my case?

same issue here. images coming from S3 bucket

same problem....
and my images are coming from same orign

I just fixed this problem.
pls enable the option 'useCORS' to 'true'
codes bellow:
html2canvas( document.querySelector(".preview_area"), { logging: true, letterRendering: 1, // allowTaint: false useCORS: true } ).then(canvas => { })

Thanks
It works for me ❤❤

Access to image at 'https://image/logo link' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Is this the reason why my logo is not appearing in the pdf file.
Rest all the text with css applied and the brand logo which is in my static folder is comming.
Please do answer need help on this Iam trying this from more than 2 weeks.

For anyone coming here, after trying many things, I found out that for getting images from an external resources ( such as S3 bucket), you have to enable both useCORS = true for html2canvas, and enable CORS policy on the images served from S3.

I resolved it using the html2canvas proxy. You can see some details on usage and testing here:
https://github.com/zeusstl/html2canvas-proxy-nodejs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Josh10101010 picture Josh10101010  ·  3Comments

tibewww picture tibewww  ·  4Comments

deepender87 picture deepender87  ·  4Comments

arzyu picture arzyu  ·  3Comments

koreanman picture koreanman  ·  4Comments