简体   繁体   English

在浏览器中将svg转换为png图像(包括IE在内的跨浏览器)

[英]In browser conversion of svg to png image (cross browser including IE)

I am using Highcharts and i need to convert all the charts on the page to an image that i can send to my server so i can merge it to a master export excel which already contains some tables and pivot grids. 我正在使用Highcharts,我需要将页面上的所有图表转换为我可以发送到我的服务器的图像,这样我就可以将它合并到主导出excel,它已经包含一些表格和透视网格。 This is my code so far 到目前为止这是我的代码

var svg = Highcharts.getSVG(charts);
img = new Image();
img.src = "data:image/svg+xml," + encodeURIComponent(svg);
mycanvas = document.createElement('canvas');
mycanvas.width = 1000
mycanvas.height = 1000
ctx = mycanvas.getContext("2d");
ctx.drawImage(img, 0, 0);
$("body").append("<image src='" + mycanvas.toDataURL("image/png") + "'/>");
$.ajax({
    type: "POST",
    url: '@Url.Action("getfile")',
    data: JSON.stringify({ file: mycanvas.toDataURL("image/png").replace("data:image/png;base64,", "") }),
    contentType: 'application/json; charset=utf-8'
});

This is the c# code where i tested if i get the data correctly from the client(i just write the base64 string to a file). 这是我测试的c#代码,如果我从客户端正确获取数据(我只是将base64字符串写入文件)。 Firefox and Chrome write the image correctly, IE just give me a black image Firefox和Chrome正确写入图像,IE只给我一个黑色图像

public void getfile(string file)
{
    System.IO.File.WriteAllBytes(@"c:\yourfile.png", Convert.FromBase64String(file));
}

the entire code you can find here http://jsfiddle.net/gd7bB/2291/ 你可以在这里找到整个代码http://jsfiddle.net/gd7bB/2291/

The image seems to be generated fine in Firefox and Chrome, but not IE(here i just get a black image). 图像似乎在Firefox和Chrome中生成,但不是IE(这里我只是得到一个黑色图像)。 I am using a canvas which from what i gathered IE supports IE9 Support for HTML5 Canvas tag . 我正在使用一个画布,从我收集的IE支持IE9支持HTML5 Canvas标签

If you could help me figure out what i'm doing wrong, or maybe you can point the way of a better solution i would much appreciate it. 如果你能帮助我弄清楚我做错了什么,或者你可以指出更好的解决方案,我会非常感激。

IE9 seem to indeed support the canvas element as per https://msdn.microsoft.com/fr-fr/library/ff975241(v=vs.85).aspx IE9似乎确实支持canvas元素,按照https://msdn.microsoft.com/fr-fr/library/ff975241(v=vs.85).aspx

The following sample/test code for toDataURL works for me in IE11 : http://samples.msdn.microsoft.com/Workshop/samples/canvas/todataurl.html 以下为toDataURL的示例/测试代码在IE11中适用于我: http ://samples.msdn.microsoft.com/Workshop/samples/canvas/todataurl.html

The fiddle you gave does not work in IE11 ; 你给的小提琴在IE11中不起作用; looking at the console errors, it leads to the fact that you cannot call drawImage before the image has finished its rendering. 查看控制台错误,导致您无法在图像完成渲染之前调用drawImage。 IE Throws a IE投掷

call to unsupported method or attributes of a method 调用不支持的方法或方法的属性

Adding a setTimeout allow IE to render the dynamic image and bypass this error. 添加setTimeout允许IE呈现动态图像并绕过此错误。

But it then leads to a 但它导致了一个

Security Error

because it seems that IE has tainted the canvas because of some "cross origin" issue 因为IE似乎因为某些“交叉起源”问题而污染了画布

SecurityError
The img or video element is not of the same origin or domain
as the document that owns the canvas element. Versions earlier
than Internet Explorer 10 use SECURITY_ERR.

IE seems to be tainting all images filled with SVG for security reasons - SVG tained canvas IE security error toDataURL 出于安全原因,IE似乎正在污染所有充满SVG的图像 - SVG删除了画布IE安全性错误toDataURL

I managed to have a working version using canvg , which transforms an SVG file into canvas instructions - https://github.com/gabelerner/canvg 我设法使用canvg工作版本,它将SVG文件转换为画布说明 - https://github.com/gabelerner/canvg

the solution boils down to 解决方案归结为

var svg = Highcharts.getSVG(charts);
var mycanvas = document.createElement('canvas');
canvg(mycanvas, svg)
console.log(mycanvas.toDataURL("image/png"))

check this fiddle http://jsfiddle.net/6bxqbaeb/1/ which works in IE11 检查这个在IE11中工作的小提琴http://jsfiddle.net/6bxqbaeb/1/

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

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