[reportlab-users] External hyperlinks in SVG output

Peter peter at maubp.freeserve.co.uk
Mon Feb 16 07:51:41 EST 2009



> Apparently "version" and "baseProfile" shouldn't be there for the

> currently used doctype. Interestingly renderSVG doesn't specify the

> doctype, letting python's xml.dom decide, so it could vary with

> version of python (and I am using python 2.4 on this machine). We

> should be able to specify them when creating the DOM...


My mistake - the current renderSVG.py actually has the doctype hard
coded in the save method - it does not handle this via the dom python
library.

Anyway, attached is my proof of concept updates to renderSVG.py
(replacement file and as a patch against
https://svn.reportlab.com/svn/public/reportlab/trunk/src/reportlab/graphics/renderSVG.py
which I assume it the latest) which allows the use of external
hyper-references. All you get to specify are the URL and its title
string (tooltip text on most browsers) via two optional properties of
the drawing objects.

As part of this work I have changed how the doctype is declared, and
made sure the new output does pass the W3 validation (the current
renderSVG output does not).

An example script using this follows below.

Peter

--------------

from reportlab.graphics import renderPS, renderPDF, renderPM #, renderSVG
from reportlab.lib.units import cm
from reportlab.graphics import shapes
from reportlab.lib import colors
import renderSVG_links as renderSVG #my local copy of
reportlab.graphics.renderSVG

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!",
hrefURL = "http://en.wikipedia.org/wiki/Hello_world",
hrefTitle = "Why 'Hello World'?",
fillColor = colors.darkgreen))
drawing.add(shapes.Rect(4.5*cm, 0.5*cm, 5*cm, 1*cm,
hrefURL = "http://en.wikipedia.org/wiki/Rectangle",
hrefTitle = "Wikipedia page on rectangles",
strokeColor = colors.blue,
fillColor = colors.red))
drawing.add(shapes.Ellipse(7*cm, 1*cm, 2*cm, 0.95*cm,
hrefURL = "http://en.wikipedia.org/wiki/Ellipse",
strokeColor = colors.black,
fillColor = colors.yellow))
drawing.add(shapes.Circle(7*cm, 1*cm, 0.9*cm,
hrefURL = "http://en.wikipedia.org/wiki/Circle",
strokeColor = colors.black,
fillColor = colors.brown))
drawing.add(shapes.Ellipse(7*cm, 1*cm, 0.5*cm, 0.9*cm,
hrefTitle = "Tooltip with no link? Does nothing!",
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],
hrefURL = "http://en.wikipedia.org/wiki/Polygon",
hrefTitle = "This triangle is a simple polygon.",
strokeColor = colors.darkgreen,
fillColor = colors.green))

if True :
print "Rotating the drawing... to check this all works still ;)"
group = drawing.asGroup()
group.rotate(90) #rotates about top left corner
group.translate(0, -2*cm)
drawing = shapes.Drawing(height, width)
drawing.add(group)

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

print "-" * 50
print renderSVG.drawToString(drawing)
print "-" * 50
print "Done"


More information about the reportlab-users mailing list