Search code examples
pythonpdfpdf-generationsvgpisa

How to convert SVG images for use with Pisa / XHTML2PDF?


I'm using Pisa/XHTML2PDF to generate PDFs on the fly in Django. Unfortunately, I need to include SVG images as well, which I don't believe is an easy task.

What's the best way to go about either a) converting the SVGs to PNG / JPG (in Python) or b) including SVGs in the PDF export from Pisa?


Solution

  • There's the Java based Apache Batik SVG toolkit.

    In a similar question regarding C# it was proposed using the command line version of Inkscape for this.

    For Python, here's a useful suggestion from this discussion thread:

    import rsvg
    from gtk import gdk
    h = rsvg.Handle('svg-file.svg')
    pixbuf = h.get_pixbuf()
    pixbuf.save('foobar.png', 'png')
    

    the step from gtk import gdk, suggested by Lukasz, is necessary and has to precede creation of the pixbuf, otherwise you will not get the save method, as observed by the original poster.