Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using ReportLab to make a pdf using Python. I want to add a shape to the canvas, and have that shape act as a hyperlink. What is the simplest way to make the 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()
like image 657
japhyr Avatar asked May 21 '12 16:05

japhyr


1 Answers

Call linkURL on the Canvas:

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

The rectangle is the clickable area, so you'd have to match that to the drawn rectangle. The arguments are two coordinates, twice x, y for the bottom-left and top-right corner.

See more examples in this blog post: http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/

like image 118
Martijn Pieters Avatar answered Oct 23 '22 12:10

Martijn Pieters