SVG to PNG with custom fonts in Python

2024/10/18 13:35:00

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.

I've found this page describing embedding SVG fonts.

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).

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

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.

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

Answer

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.

https://en.xdnf.cn/q/72920.html

Related Q&A

How to solve an equation with variables in a matrix in Python?

im coding in Pyhon, and Im working on stereo-correlation. I want to resolve this equation : m = K.T.Mm,K,M are know.where :M is the homogeneous coordinate of a point in the cartesian coordinate system…

how to create a new method with signature of another

How can I copy the signature of a method from one class, and create a "proxy method" with same signature in another ?.I am writing a RPC library in python. The server supports remote calls t…

Converting a full column of integer into string with thousands separated using comma in pandas

Say I have population data stored in a column of a dataframe using pandas in python with Country names as row indices. How do I convert the whole column of numbers into string thousands separator using…

Nested WHILE loops in Python

I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> whil…

Fastest way to drop rows / get subset with difference from large DataFrame in Pandas

Question Im looking for the fastest way to drop a set of rows which indices Ive got or get the subset of the difference of these indices (which results in the same dataset) from a large Pandas DataFram…

Python inheritance: when and why __init__

Im a Python newbie, trying to understand the philosophy/logic behind the inheritance methods. Questions ultimately regards why and when one has to use the __init__ method in a subclass. Example:It seem…

TypeError: A Future or coroutine is required

I try make auto-reconnecting ssh client on asyncssh. (SshConnectManager must stay in background and make ssh sessions when need)class SshConnectManager(object): def __init__(self, host, username, passw…

Python socket closed before all data have been consumed by remote

I am writing a Python module which is communicating with a go program through unix sockets. The client (the python module) write data to the socket and the server consume them.# Simplified version of t…

Python child process silently crashes when issuing an HTTP request

Im running into an issue when combining multiprocessing, requests (or urllib2) and nltk. Here is a very simple code:>>> from multiprocessing import Process >>> import requests >>…

Shared variable in concurrent.futures.ProcessPoolExecutor() python

I want to use parallel to update global variable using module concurrent.futures in pythonIt turned out that using ThreadPoolExecutor can update my global variable but the CPU did not use all their pot…