Convert SVG with custom fonts to PNG with Python using cairosvg on Ubuntu

1.8k views Asked by At

I need to convert SVG with custom fonts into PNG, I’m using the following Python code for this.

import cairosvg
png = cairosvg.svg2png(bytestring=svg_data)

The code works fine on my local machine (running under Mac OS) with installed fonts. However, when I try to deploy it to remote Ubuntu instance the fonts somehow couldn't be loaded and I end up with PNG having some default fonts in it. The fonts are installed in the system (I've already tried different font folders, also fontconfig has the fonts in the list of available fonts) and the access rights seem to be in order. Any hints what could be going wrong will be useful!

1

There are 1 answers

0
Valentin Denisenkov On

Managed to solve the problem using Wand:

from wand.image import Image

with Image(blob=svg_data, format="svg") as image:
    png_image = image.make_blob("png")

P.S. CairoSVG version still doesn't work.