[reportlab-users] Reportlab and SVG output

Peter peter at maubp.freeserve.co.uk
Thu Jan 29 08:19:41 EST 2009


I'm having a little trouble with the SVG output from reportlab - is it
expected to produce conforming SVG stand-alone files?

I've tried both ReportLab 2.1 on Mac OS X, and ReportLab 2.2 on Linux
with the same results. I've included a minimal example script at the
end of the email which uses the basic shape objects for strings,
rectangles, ellipses, circles and polygons to construct a simple
drawing which is then passed to the various render back ends. The
resulting PDF, PS and PNG files are fine. However, the SVG file only
works on a handful of tools:

Mac OS 10.5 preview, Safari 3.2.1 and Firefox 3.0.5 all fail to show
the SVG image, instead displaying roughly "... ... Hello world!" (no
graphics, just text - the dots appear to come from the default title
and description in the SVG file). On the bright side, Opera 9.20 and
Adobe Illustrator CS3 will show it fine (although Illustrator does
give a warning message "Clipping will be lost on roundtrip to Tiny").

Is this expected behaviour? I was hoping to use reportlab's SVG
output within a web application, but that fact that neither of the top
two browsers on the Mac (Safari and Firefox) work is a big drawback (I
haven't tried Internet Explorer yet, but could live with just Firefox
support as that is cross platform).

I've found that by manually replacing the reportlab generated <svg
...> and </svg> tags with <xml ...> and </xml> tags copied from the
example on http://www.mozilla.org/projects/svg/ the reportlab SVG
output can be made to work on all the software I have tested. A
similar solution was suggested back in 2006, just adding some missing
values to the <svg> tag, and this also seems to work fine:

Looking at SVN, this fix was never committed - was there a good reason for this?



from reportlab.graphics import renderPS, renderPDF, renderSVG, renderPM
from reportlab.lib.units import cm
from reportlab.graphics import shapes
from reportlab.lib import colors

width = 10*cm
height = 2*cm

#Create very simple drawing object,
drawing = shapes.Drawing(width, height)
drawing.add(shapes.String(1*cm, 1*cm, "Hello World!",
fillColor = colors.darkgreen))
drawing.add(shapes.Rect(4.5*cm, 0.5*cm, 5*cm, 1*cm,
strokeColor = colors.blue,
fillColor = colors.red))
drawing.add(shapes.Ellipse(7*cm, 1*cm, 2*cm, 0.95*cm,
strokeColor = colors.black,
fillColor = colors.yellow))
drawing.add(shapes.Circle(7*cm, 1*cm, 0.9*cm,
strokeColor = colors.black,
fillColor = colors.brown))
drawing.add(shapes.Ellipse(7*cm, 1*cm, 0.5*cm, 0.9*cm,
strokeColor = colors.black,
fillColor = colors.black))
drawing.add(shapes.Polygon([4.5*cm, 1.25*cm, 5*cm, 0.1*cm, 4*cm, 0.1*cm],
strokeColor = colors.darkgreen,
fillColor = colors.green))

renderPDF.drawToFile(drawing, "hello.pdf")
renderPS.drawToFile(drawing, "hello.ps")
renderSVG.drawToFile(drawing, "hello.svg")
renderPM.drawToFile(drawing, "hello.png", "PNG")
print "Done"

More information about the reportlab-users mailing list