使用Python中的自定义字体将SVG转换为PNG [英] SVG to PNG with custom fonts in Python

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

问题描述

我正在使用基于Cairo/RSVG的解决方案将SVG光栅化为PNG.它已经在将SVG转换为Python中的PNG 中的StackOverflow上进行了描述. 但是,此解决方案似乎不适用于自定义字体.

I'm using Cairo/RSVG based solution for rasterizing SVG to PNG. It's already beeb described on StackOverflow in Convert SVG to PNG in Python. However, this solution doesn't seem to work with custom fonts.

我发现此页面描述了嵌入SVG字体.

我已尝试通过XLink从外部SVG包含它们,如示例中所述.我尝试将字体直接嵌入同一SVG文件中.失败了,我尝试了CSS Web Fonts语法.使用Cairo渲染时,这三种方法均无效(在Ubuntu的默认查看器Eye of GNOME中也无效).

I've tried to include them from external SVG via XLink, as described in example. I've tried embedding the font directly in the same SVG file. Having failed with that, I've tried CSS Web Fonts syntax. None of the 3 methods works when rendering with Cairo (nor does it work in the Ubuntu's default viewer Eye of GNOME).

我尝试过ImageMagick,结果与开罗完全相同.

I've tried ImageMagick, with exactly same results as Cairo.

另一方面,使用所有3种字体嵌入方法,可以使用Google Chrome或

On the other hand with all 3 font embedding methods, fonts are rendered fine in WebKit, either using Google Chrome or webkit2png.py. However, if possible I'd like to avoid using QT WebKit on server, as it's non-trivial setup (including xvfb etc.), and I'm afraid that might not result very efficient nor stable.

是否有其他方法可以将Python的SVG渲染为PNG?

Is there any alternative method of rendering SVG to PNG from Python?

推荐答案

我花了一周时间研究这个问题,并得出结论,使用自定义字体处理SVG的服务器端渲染/光栅化的最佳方法是安装这些字体在服务器上.我尝试过的工具(rsvg,imagemagick,phantomjs,qtwebkit ...)无法处理网络字体和svg字体.

I have spent a week researching this very issue and concluded that the best way to handle server-side rendering/rasterizing of SVG with custom fonts is to install those fonts on the server. The tools I tried (rsvg, imagemagick, phantomjs, qtwebkit...) could not handle web fonts and svg fonts.

Google有数百种字体(并且还在不断增长),人们可以在其中下载和使用.服务器.

Google has several hundred fonts (and growing) that one can download and use on a server.

  • 下载OTF或TTF字体
  • 在服务器上安装它们并刷新字体缓存
  • 在SVG文档中用字体系列名称替换外部字体的CSS定义.

如果您还需要在网页中使用这些相同的字体,则可以直接链接到Google CDN以获得WOFF文件,以节省您自己的服务器时间和网络带宽.

And if you also need to use those same fonts in a web page, you could link to Google CDN directly for the WOFF files to conserve your own server time and network bandwidth.

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

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