问题描述
我正在使用 html2canvas 和 canvg 插件将角度 nvd3 图表转换为 svg,但是当我将饼图转换为 png 时,我看起来与图表相同,但是当我转换折线图或面积图时,它的背景变为黑色,一些圆圈被淹没图片.我的代码是
i am converting angular nvd3 chart to svg using html2canvas and canvg plugings but when i convert pie chart to png then i looks same as chart but when i convert line chart or area chart then its background goes to black and some circle drown on image.My code is
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');
帮帮我吧.提前致谢
推荐答案
您的 svg 元素由外部样式表 nv.d3.min.css 设置样式.
Your svg elements are being styled by the external stylesheet nv.d3.min.css .
canvg 似乎无法访问外部样式表,因此您需要将其直接附加到您的 svg 节点中.
canvg seems unable to access external style sheets, so you need to append it directly in your svg node.
为此,如果您的样式表与脚本托管在同一域中,您可以执行以下操作:
To do so, if your style sheet is hosted on the same domain as your scripts, you can do something like :
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 会很高兴.
So now you can call the XMLSerializer, and canvg will be happy.
分叉的 plunkr,我在那里复制了 nv.d3.min.css 的内容到同源 style.css.
Forked plunkr, where I copied the nv.d3.min.css's content to a same-origin style.css.
这篇关于将 SVG 转换为 PNG 格式的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!