SVG to PNG with custom fonts in Python

无人久伴 提交于 2019-12-03 20:43:31

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 has several hundred fonts (and growing) that one can download and use on a server.

  • Download OTF or TTF fonts
  • Install them on the server and refresh the font cache
  • Replace the CSS definitions of the external fonts with font family names in your SVG docs.

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.

Doc Brown

You can try to use inkscape, perhaps this gives you better results:

 inkscape inputfile.svg --export-png=exportfile.png

Running this from python is described here: Calling an external command in Python

With Imagemagick I still struggle with svg rasterizing with fonts that are installed on the server and can be used in certain operations, but fail when using -convert from .svg to .png.... It seems to turn every type of text into arial. I think it may be a bug with ImageMagick or a certain format needed in the .svg

A couple of things to check with RSVG:

  • That the font source files are in your system or user font path
  • That font names in the SVG are not quoted, even if they contain spaces. librsvg will assume the quotes are part of the font name and it won't find the source files.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!