最近做的一个项目要求把图片导出到word中
我是使用amcharts 然后生成图片,这是svg的,然后用一个js 把svg变成字符串 传到后台处理:下面是js
// Extend AmCharts object
AmCharts.getExport = function(id) {
var wrapper = document.getElementById(id);
var svgs = wrapper.getElementsByTagName('svg');
var options = {
ignoreAnimation : true,
ignoreMouse : true,
ignoreClear : true,
ignoreDimensions: true,
offsetX : 0,
offsetY : 0
};
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var counter = {
height : 0,
width : 0
}
// Nasty workaround until somebody figured out to support images
function removeImages(svg) {
var startStr = '<image';
var stopStr = '</image>';
var stopStrAlt = '/>';
var start = svg.indexOf(startStr);
var match = '';
// Recursion
if ( start != -1 ) {
var stop = svg.slice(start,start+1000).indexOf(stopStr);
if ( stop != -1 ) {
svg = removeImages(svg.slice(0,start) + svg.slice(start + stop + stopStr.length,svg.length));
} else {
stop = svg.slice(start,start+1000).indexOf(stopStrAlt);
if ( stop != -1 ) {
svg = removeImages(svg.slice(0,start) + svg.slice(start + stop + stopStr.length,svg.length));
}
}
}
return svg;
};
// Setup canvas
canvas.height = wrapper.offsetHeight;
canvas.width = wrapper.offsetWidth;
context.fillStyle = '#FFFFFF';
context.fillRect(0,0,canvas.width,canvas.height);