What is the easiest way to add a hyperlink to a canvas element in ReportLab?

I am using ReportLab to create PDF using Python. I want to add a shape to the canvas, and this shape acts as a hyperlink. What is the easiest way to make a rectangle in the following example link to google.com?

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

c = canvas.Canvas("hello.pdf")

# move the origin up and to the left, draw square
c.translate(inch,9*inch)
# How do I make this rectangle link to google.com?
c.rect(inch,inch,1*inch,1*inch, fill=1)

c.showPage()
c.save()
+3
source share
1 answer

Call linkURLon canvas:

c.linkURL('http://google.com', (inch, inch, 2*inch, 2*inch), relative=1)

A rectangle is a clickable area, so you have to map it to a drawn rectangle. The arguments are two coordinates, twice x, yfor the lower and upper right corner.

More on this blog: http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/

+9
source

All Articles