简体   繁体   English

如何将 svg 中的字体真棒图标导出为 png?

[英]How to export font awesome icons in svg to png?

I am trying to export mermaid to png.我正在尝试将美人鱼导出为 png。 I have been successful to an extent, however, I am stuck exporting the font awesome icons in the svg.我在一定程度上取得了成功,但是,我被困在 svg 中导出字体真棒图标。 They are called simply via fa class, and usual canvas export is not considering them in the export.它们只是通过fa类调用,通常的画布导出不会在导出中考虑它们。 In below MWE, you could see car icon is missing in PNG output.在下面的 MWE 中,您可以看到 PNG 输出中缺少汽车图标。 Kindly help.请帮忙。

My understanding is, everything has to be inline in svg for canvas export to work, but I could not find a way how to do that, because in mermaid, one could insert any font awesome icon as he needs for the graphs.我的理解是,所有内容都必须在 svg 中内联才能使画布导出工作,但我找不到如何做到这一点的方法,因为在美人鱼中,人们可以根据图形需要插入任何字体真棒图标。

MWE移动电源

function drawInlineSVG(svgElement, ctx, callback)
{
    var svgURL = new XMLSerializer().serializeToString(svgElement);
    console.log(svgElement);
    // console.log(svgURL);
    var img = new Image();
    img.onload = function ()
    {
        ctx.drawImage(this,0,0);
        callback(this);
    }
    // img.src = 'data:image/svg+xml; charset=utf8, ' + encodeURIComponent(svgURL);
    img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgURL ) );
}

Output:输出:
在此处输入图片说明

I have only included the export function from svg to png above, for brevity.为简洁起见,我仅在上面包含了从 svg 到 png 的导出功能。 The complete code is available in Jsfiddle : https://jsfiddle.net/parthi2929/r6wb9ka5/6/完整代码在 Jsfiddle 中可用: https ://jsfiddle.net/parthi2929/r6wb9ka5/6/

  • Note car is missing above output (png part).注意汽车在输出上方丢失(png 部分)。 Also note change of font.还要注意字体的变化。 I want to export png exactly as seen in svg.我想完全像在 svg 中看到的那样导出 png。
  • One could also note in jsfiddle, i have tried few more methods including using canvg , rasterizeHTML but former, not even renders any font, while latter gives same result with car missing.人们还可以在 jsfiddle 中注意到,我尝试了更多的方法,包括使用canvgrasterizeHTML但前者,甚至不呈现任何字体,而后者在汽车丢失的情况下给出了相同的结果。

I could not reproduce in SO's own snippet area as it was throwing error, so kindly excuse on that.我无法在 SO 自己的片段区域中重现,因为它抛出了错误,所以请原谅。

I had exactly the same question and Google took me here. 我有完全相同的问题,谷歌把我带到了这里。 I'm surprised you did not get a reply earlier. 我很惊讶你没有早点得到答复。 I found the site faToPng that provides a converter (to .PNG, .JPG or base64) which you can use to do that. 我找到了 faToPng网站,它提供了一个转换器(到 .PNG、.JPG 或 base64),您可以使用它来执行此操作。 Unfortunately it does not support FA4. 不幸的是,它不支持 FA4。

Update 2021 : the site I linked to is no more, the domain now points to something unrelated to our subject. 2021 年更新:我链接的站点已不存在,该域现在指向与我们的主题无关的内容。

In the process I also came across this site which is an online SVGtoPNG-Converter that helped with some other files I was dealing with.在这个过程中,我还遇到了这个网站,它是一个在线 SVGtoPNG-Converter,它帮助处理了我正在处理的其他一些文件。

Alternatively, here's a Chrome plugin that can be used for the same purpose.或者, 这是一个可用于相同目的的 Chrome 插件。

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

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