将SVG转换为PNG中的图像

时间:2016-12-27 06:57:45

标签: angularjs nvd3.js html2canvas angular-nvd3 canvg

我正在使用html2canvas和canvg插件将角度nvd3图表转换为svg但是当我将饼图转换为png然后我看起来与图表相同但是当我转换折线图或区域图表然后其背景变为黑色并且一些圆圈淹没图片。 我的代码是

var svgElements = $("#container").find('svg');

            //replace all svgs with a temp canvas
            svgElements.each(function () {
                var canvas, xml;

                // canvg doesn't cope very well with em font sizes so find the calculated size in pixels and replace it in the element.
                $.each($(this).find('[style*=em]'), function (index, el) {
                    $(this).css('font-size', getStyle(el, 'font-size'));
                });

                canvas = document.createElement("canvas");
                canvas.className = "screenShotTempCanvas";
                //convert SVG into a XML string
                xml = (new XMLSerializer()).serializeToString(this);

                // Removing the name space as IE throws an error
                xml = xml.replace(/xmlns=\"http:\/\/www\.w3\.org\/2000\/svg\"/, '');

                //draw the SVG onto a canvas
                canvg(canvas, xml);
                $(canvas).insertAfter(this);
                //hide the SVG element
                ////this.className = "tempHide";
                $(this).attr('class', 'tempHide');
                $(this).hide();
            });


            html2canvas($("#container"), {
                onrendered: function (canvas) {
                    var a = document.createElement("a");
                    a.download = "Dashboard.png";
                    a.href = canvas.toDataURL("image/png");
                    a.click();
                    var imgData = canvas.toDataURL('image/png');

                    var doc = new jsPDF('p', 'mm','a4');
                    var width = doc.internal.pageSize.width;    
                    var height = doc.internal.pageSize.height;

                    doc.addImage(imgData, 'PNG',  0, 0, width, height);
                    doc.save('Dashboard.pdf');
                }
            });

            $("#container").find('.screenShotTempCanvas').remove();
            $("#container").find('.tempHide').show().removeClass('tempHide');

this is area chart image with some unknown circle on chart

帮助我们。 在此先感谢

2 个答案:

答案 0 :(得分:3)

您的svg元素由外部样式表 nv.d3.min.css 设置样式。

canvg似乎无法访问外部样式表,因此您需要将其直接附加到svg节点中。

为此,如果您的样式表与脚本位于同一个域,则可以执行以下操作:

var sheets = document.styleSheets;
var styleStr = '';
Array.prototype.forEach.call(sheets, function(sheet){
    try{ // we need a try-catch block for external stylesheets that could be there...
        styleStr += Array.prototype.reduce.call(sheet.cssRules, function(a, b){
            return a + b.cssText; // just concatenate all our cssRules' text
            }, "");
        }
    catch(e){console.log(e);}
});
// create our svg nodes that will hold all these rules
var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
var style = document.createElementNS('http://www.w3.org/2000/svg', 'style');
style.innerHTML = styleStr;
defs.appendChild(style);
// now append it in your svg node
thesvg[0].insertBefore(defs, thesvg[0].firstElementChild);

所以现在你可以调用XMLSerializer,并且canvg会很高兴 (请注意,这不仅仅是一个canvg限制,同样适用于在画布上绘制svg的每种方式。)

Forked plunkr,我将 nv.d3.min.css 的内容复制到同源 style.css

答案 1 :(得分:1)

在谈话中很晚,但是我只想补充一下,Kaiido描述的解决方案,很简单地说,就是将样式直接嵌入SVG文档中。

为此,您可以操作DOM以使SVG元素如下所示:

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1">
   <defs>
     <style>
       .rectangleStyle{
           width:200px;
           height:100px;
           stroke:black;
           stroke-width: 6;
           fill: green;
       }       
     </style>
   </defs>
   <rect class="rectangleStyle"/>
</svg>