Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html2canvas returns black image

Tags:

javascript

php

I have a problem rendering divs as images via html2canvas.

Here is my javascript code:

function saveImages(i)
        {
            html2canvas($("#page"+i), {
                logging:true,
                onrendered: function(canvas){
                    var url = canvas.toDataURL();
                    $.post("saveImage.php", {"path": url, "index":i}, function(data){
                        var p = Math.round(((i+1)/totalPages)*100);
                        $("#progressGraphical").css("width", p+"%");
                        if(p>=100){p = "fertig!"}else{p += "%";}
                        $("#progress").html((i+1)+"/"+totalPages+" Seiten gespeichert - "+p);
                        if(i<totalPages) saveImages(i+1);
                    });
                }
            });
        }
        $(document).ready(function(){
            saveImages(0);
        });

I had to work recursively because of the post-request done with jquery functions. The php file which is requested takes the base64 encoded url generated by the canvas.toDataUrl-Method, decodes it and saves it to a image file called "page0/1/2/.../n.png".

All this works fine - Until a total number of about 24 recursive calls is exceeded. If this is the case, then all the images generated by html2canvas(even the images before the 24th recursive call) are drawn blank. I already looked at the base64-encoded urls - And there are just two equal strings which alternate.

Where is the error?

like image 387
dezajno Avatar asked Oct 20 '22 12:10

dezajno


1 Answers

https://stackoverflow.com/a/23928038/1815624

simply add css background-color:#ffffff to your table :)

hope this helps

I wrapped the content into a div for each desired page and gave class="pdfpage" then set the css as pdfpage{background:#FFFFFF;}

As I had commented it was not first thoughts that I needed to set the background of the rendered element as white since it was white already...But in truth it was colorless...

copied my answer from: https://stackoverflow.com/a/25457584/1815624

like image 194
CrandellWS Avatar answered Oct 23 '22 01:10

CrandellWS