3

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?

Nick Sergeant
  • 35,843
  • 12
  • 36
  • 44

2 Answers2

2

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.

Community
  • 1
  • 1
Yevgeny Doctor
  • 433
  • 3
  • 8
  • 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 Apr 24 '09 at 22:00
  • 1
    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 Apr 24 '09 at 22:31
  • 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 Apr 24 '09 at 22:33
  • 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 Apr 25 '09 at 00:10
  • 1
    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/pygtk@daa.com.au/msg05850.html – Yevgeny Doctor Apr 25 '09 at 00:31
  • 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 Apr 25 '09 at 00:43
  • 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 Apr 25 '09 at 01:40
1

"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
  • 185
  • 1
  • 10