简体   繁体   English

将base64编码的svg转换为png

[英]Converting a base64 encoded svg to a png

I am in need of converting a base64 encoded svg to a png and then returning it to the client to be rendered. 我需要将base64编码的svg转换为png,然后将其返回给要渲染的客户端。

The idea is that I am drawing an svg on the client using d3.js and I need to convert it to png. 我的想法是,我正在使用d3.js在客户端上绘制一个svg,我需要将其转换为png。

I tried taking the javascript root and writing the svg on the canvas and then converting it toDataUrl but IE has problems with this on all versions so this is not a viable option. 我尝试将javascript根目录并在画布上编写svg,然后将其转换为DataUrl,但IE在所有版本上均存在问题,因此这不是可行的选择。

I have searched online a bit and all I could find is Inkscape. 我在网上搜索了一下,所有我能找到的就是Inkscape。

This is not a viable solution for me because of limited access on the server and frankly I don't think it's a good idea to install an entire application for a simple functionality. 对于我来说,这不是一个可行的解决方案,因为对服务器的访问受到限制,并且坦率地说,我认为安装整个应用程序以实现简单功能并不是一个好主意。

Is there any other solution that can take a base 64 encoded svg and return a png that can be displayed in an image? 还有其他解决方案可以采用以64为基数的svg编码,并返回可以在图像中显示的png吗?

I am facing the same issue, however I been able to render the SVG into an image so your users could right click to download the image or right click on the canvas. 我遇到了同样的问题,但是我能够将SVG渲染为图像,因此您的用户可以右键单击以下载图像或在画布上单击鼠标右键。 There is also Canvg which has a library that appears to work with IE, however it is not accurate for complex SVG and so does not meet my needs. 还有Canvg,它的库似乎可以与IE一起使用,但是对于复杂的SVG来说并不准确,因此不能满足我的需求。 Perhaps it will help you. 也许会对您有所帮助。

I have code in my question that will work in IE, just use the IMG or Canvas object, you do not need to call toDataURI. 我的问题中有可以在IE中运行的代码,只需使用IMG或Canvas对象即可,而无需调用toDataURI。

Checkout Canvg , it might be of help. 结帐Canvg ,可能会有帮助。

Good luck! 祝好运! If you find a better solution, please let me know. 如果您找到更好的解决方案,请告诉我。 :-) :-)

I found that since I have to support IE 11 I instead went with canvas to blob to PNG using "canvas-toBlob.js" and "FileSaver.js" 我发现,由于必须支持IE 11,我改为使用“ canvas-toBlob.js”和“ FileSaver.js”将画布blob到PNG。

$("#save-btn").click(function() 
{
    $("#myChart").get(0).toBlob(function(blob) 
    {
        saveAs(blob, "chart_1.png");
    });
});

https://jsfiddle.net/mfvmoy64/155 https://jsfiddle.net/mfvmoy64/155

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

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