generate a pdf with embedded vector art using reportlab and svglib

I was looking around for this and had a bit of trouble finding it, hopefully it will help out someone.

As far as I can tell getting vector artwork into reportlab in a sensible manner (i.e. aside from generating raw pdf commands) is practically restricted to using svg.

Dinu Gherman’s svglib is the tool to use.

Given tiger.svg, svglib and:

from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from svglib import svg2rlg, svg2pdfc = canvas.Canvas("hello.pdf")

c.drawString(9*cm, 22*cm, "Hello World!")
drawing = svg2rlg("tiger.svg")
drawing.drawOn(c, 200, 200)

c.showPage()
c.save()

(source)

You get: hello.pdf

Apparently svglib only implements a subset of svg but should be usable in many circumstances.

Easy peasy.