将 SVG 转换为 PNG 时包括字体 [英] Including fonts when converting SVG to PNG

查看:59
本文介绍了将 SVG 转换为 PNG 时包括字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一些 SVG 并允许我网站的用户将这些 SVG 下载为 PNG.
阅读

虽然在使用上面的下载脚本后,我最终会得到:

解决方案

我可以使用以下代码将字体包含在 png 本身中,试试看

var svg = document.getElementById('generated-svg');var svgData = new XMLSerializer().serializeToString(svg);var canvas = document.createElement("canvas");画布宽度 = 300;画布高度 = 500;var ctx = canvas.getContext("2d");//显示图片var img = document.createElement("img");img.setAttribute( "src", "data:image/svg+xml;base64," + btoa(svgData));img.onload = 函数(){ctx.drawImage( img, 0, 0 );//图片链接console.log( canvas.toDataURL( "image/png" ) );//打开图片window.location.href=canvas.toDataURL( "image/png" );};

https://jsfiddle.net/user3839189/hutvL4ks/1/

I am trying to generate some SVG and allow users of my website to download these SVGs as PNGs.
After reading this I get all my external images included in the downloaded PNG.
Now I am trying to get the fonts on the PNG correct. This seems to answer that and so I added:

<defs>
    <style type="text/css">
        @font-face {
            font-family: Parisienne;
            src: url('data:application/font-woff;charset=utf-8;base64,.....')
        }
    </style>
</defs>

Where ..... is base64 encoded woff2 font. And then used it in text like so:

<text x="55" y="55" stroke="red" font-family="Parisienne">Blah</text>

The font gets displayed in the browser correctly (I haven't installed it on my OS), however it is still not included in the PNG.
Do I have to add some additional processing to the script I used from the first link?
Thanks.

--EDIT--
I have been asked for a complete example, here it is:

<svg id="generated-svg" class="generated-svg" width="300px" height="500px"
    version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns-xlink="http://www.w3.org/1999/xlink">
  <defs>
    <style type="text/css">
      @font-face {
        font-family: Parisienne;
        src: url('data:application/font-woff;charset=utf-8;base64,.....')
      }
    </style>
  </defs>

  <rect width="300" height="500" fill="#222"/>
  <text x="55" y="55" stroke="red" font-family="Parisienne" font-size="20px">Test text</text>
</svg>

I haven't added the base64 encoded font as it's simply too big. But you can encode any font you like and replace the ....... I am using Parisienne.
Here is working jsfiddle with the actual font: https://jsfiddle.net/z8539err/ In my browser this is the output:

Whilst after using the download script above I would end up with:

解决方案

I'm able to include the font in the png itself with the following code, give it a try

var svg = document.getElementById('generated-svg');
var svgData = new XMLSerializer().serializeToString( svg );

var canvas = document.createElement("canvas");
canvas.width = 300;
canvas.height = 500;
var ctx = canvas.getContext("2d");

//display image
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );


img.onload = function() {
ctx.drawImage( img, 0, 0 );

//image link
console.log( canvas.toDataURL( "image/png" ) );


//open image 
window.location.href=canvas.toDataURL( "image/png" );
};

https://jsfiddle.net/user3839189/hutvL4ks/1/

这篇关于将 SVG 转换为 PNG 时包括字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆