简体   繁体   English

使用HTML2CANVAS将html保存为图像

[英]Save html as image using HTML2CANVAS

I have added HTML2CANVAS to save a canvas wrapped in a div as an image in .png format. 我添加了HTML2CANVAS以将包含在div中的画布保存为.png格式的图像。 It works great in Chrome, but nothign happens in IE 9, 10, or 11. I have set breakpoints in IE to debug, but no errors are ever thrown. 它在Chrome中运行良好,但在IE 9,10或11中没有发生。我在IE中设置断点进行调试,但不会抛出任何错误。

My reason for wrapping the canvas in a div was to be able to export the legend with the chart/graph that is generated in the canvas tag. 我在div中包装画布的原因是能够使用canvas标记中生成的图表/图形导出图例。

I am using AngularJS, but in this instance, only a click event is used. 我使用的是AngularJS,但在这种情况下,只使用了一个click事件。

Again, this is working fine in Chrome. 同样,这在Chrome中运行良好。 No errors are thrown in IE. IE中没有错误。 When debugging it goes over javascript below and appears to be hitting each line appropriately. 调试时,它会通过下面的javascript,并且似乎正在适当地点击每一行。 I do have the html2canvas.js file added in the page FYI. 我确实在页面FYI中添加了html2canvas.js文件。

I have researched this and came across some posts saying IE does not handle promises and I needed to add a polyfill to compensate for this. 我已经研究了这个并且发现一些帖子说IE没有处理承诺,我需要添加一个polyfill来弥补这一点。 I've done that and nothing changes. 我已经做到了,没有任何改变。

Thanks 谢谢

Here is my code. 这是我的代码。

HTML HTML

<div class="row">
    <div id="widget" class="col-md-12">
    <canvas id="lineChartCanvas" class="chart chart-line" chart-data="data" chart-labels="labels" style="height: 300px; width: 95%;"
            chart-legend="true" chart-series="series" chart-click="onClick"></canvas>
    </div>
</div>

Button click event 按钮单击事件

<input type="button" class="btn btn-primary" id="btnSave" ng-click="exportChart()" ng-show="isIEbrowser" value="EXPORT CHART"/>

Javascript/Angular 使用Javascript /角

function exportChart() {

    var canvasdiv = document.getElementById("widget");

    html2canvas(canvasdiv,{

        onrendered: function (canvas) {
            var a = document.createElement("a");
            a.href = canvas.toDataURL("image/png");
            a.download = userContext.mrn + "_chart_" + datetime + ".png";
            a.click();
        }
    });
}

For IE, use the BlobBuilder to convert the canvas to a blob object and save it as an image stream. 对于IE,使用BlobBuilder将画布转换为blob对象并将其另存为图像流。

Below is a sample code that I have used in one of my project: 下面是我在我的一个项目中使用的示例代码:

if (navigator.msSaveBlob) {
                    var URL = window.URL;
                    var BlobBuilder = window.MSBlobBuilder;
                    navigator.saveBlob = navigator.msSaveBlob;
                    var imgBlob = canvas.msToBlob();
                    if (BlobBuilder && navigator.saveBlob) {
                        var showSave = function (data, name, mimetype) {
                            var builder = new BlobBuilder();
                            builder.append(data);
                            var blob = builder.getBlob(mimetype || "application/octet-stream");
                            navigator.saveBlob(blob, name);
                        };

                        showSave(imgBlob, fileName, "image/png");
                    }
                }

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

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