Search code examples
svgpngemojipython-unicode

Convert SVG (which contains multi-code emoji) to PNG fails


I'm able to convert a very large SVG which contain emoji's into PNG or even PDF.

But I struggle to get it working with multi-code emoji's

Please find below an extract with just a few lines.

The one that fails to convert is:

<text x="20" y="110" font-size="20px" >\U0001F1E8\U0001F1ED</text>
    </svg>

Please find below an extract with just a few lines.

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM

data = """<?xml version="1.0" encoding="utf-8" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

<text x="20" y="20" font-size="20px" >Test</text>
<text x="20" y="50" font-size="20px" >\uF1E8\uF1ED</text>
<text x="20" y="80" font-size="20px" >\U0001F1E8\U0001F1ED</text>
<text x="20" y="110" font-size="20px" >🇨🇭</text>
</svg>"""

with open('test.svg', 'w') as file:
    file.write(data)
    file.close()

drawing = svg2rlg("test.svg")
renderPDF.drawToFile(drawing, "test.pdf")
renderPM.drawToFile(drawing, "test.png", fmt="PNG")

Many thanks in advance for any hint and have a nice day:-)


Solution

  • Problem solved, I used the html2image Python module which converts the SVG (including embedded Emoji's) nicely to a PNG image.