views:

587

answers:

2

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?

+2  A: 

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
h = rsvg.Handle('svg-file.svg')
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')
Yevgeny Doctor
Ah, that would be awesome, but I have no idea how to get rsvg. It appears from a quick Google search others have similar problems 'getting' rsvg. Is this a Python 2.6 only thing?
Nick Sergeant
Rsvg (http://en.wikipedia.org/wiki/Librsvg) is part of the GNOME project. Here a link (http://mwusers.com/forums/showthread.php?p=27924) to a discussion about installing it on Windows.
Yevgeny Doctor
I'm actually on Ubuntu, and am having a pretty tough time installing on 8.10. It appears I have to install python-gnome2-desktop, but I'm not sure. I opened a SO post here: http://stackoverflow.com/questions/787812/how-to-install-python-rsvg-on-ubuntu-8-10
Nick Sergeant
Ah, okay, I got rsvg working, but here's what I get when I try to save:AttributeError: 'gtk.gdk.Pixbuf' object has no attribute 'save'
Nick Sergeant
Here's what Google has to say about it:"You've got an old version of PyGTK-2. save() was added on 2002-11-16 by James, so it seems to have been included in 1.99.14."http://www.mail-archive.com/[email protected]/msg05850.html
Yevgeny Doctor
Ah, perfect! Unfortunately, trying to 'make' the newest version of PyGTK returns: "gtkunixprintmodule.c:51: error: 'Pycairo_IMPORT' undeclared (first use in this function)" and fails. Oy.
Nick Sergeant
Ugh, I give up. PyGTK was installed with python-gnome2-desktop. I tried compiling PyGTK2 overtop and ran into multiple installation errors (from dependencies, to the final dagger: >>> import gtk/var/lib/python-support/python2.5/gtk-2.0/gtk/__init__.py:72: GtkWarning: could not open display warnings.warn(str(e), _gtk.Warning)
Nick Sergeant
A: 

"I got rsvg working, but here's what I get when I try to save: AttributeError: 'gtk.gdk.Pixbuf' object has no attribute 'save' – Nick Sergeant Apr 25 '09 at 0:10"

You need to import gdk to have access to pixbuf methods:

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

And to convert from string that contains svg data:

import rsvg
from gtk import gdk
h = rsvg.Handle()
h.write(svg_string)
h.close()
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')
Lukasz