简体   繁体   English

html2canvas返回黑色图像

[英]html2canvas returns black image

I have a problem rendering divs as images via html2canvas. 我有一个问题,通过html2canvas将div渲染为图像。

Here is my javascript code: 这是我的javascript代码:

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. 由于使用jquery函数完成了post请求,我不得不递归地工作。 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". 请求的php文件采用canvas.toDataUrl-Method生成的base64编码的url,对其进行解码并将其保存到名为“page0 / 1/2 /.../ n.png”的图像文件中。

All this works fine - Until a total number of about 24 recursive calls is exceeded. 所有这一切都运行良好 - 直到超过总共约24个递归调用。 If this is the case, then all the images generated by html2canvas(even the images before the 24th recursive call) are drawn blank. 如果是这种情况,则html2canvas生成的所有图像(即使是第24次递归调用之前的图像)也会被绘制为空白。 I already looked at the base64-encoded urls - And there are just two equal strings which alternate. 我已经查看了base64编码的url - 并且只有两个相等的字符串可以交替使用。

Where is the error? 错误在哪里?

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

simply add css background-color:#ffffff to your table :) 只需添加css background-color:#ffffff到你的表:)

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;} 我将内容包装到每个所需页面的div中,然后给出class="pdfpage"然后将css设置为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 复制了我的答案: https//stackoverflow.com/a/25457584/1815624

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM