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?
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 .
"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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.