2012-05-21 48 views
3

我正在使用ReportLab使用Python制作pdf。我想在画布上添加一个形状,并将该形状用作超链接。以下示例中的矩形链接到google.com的最简单方法是什么?在ReportLab中将超链接添加到画布元素的最简单方法是什么?

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() 

回答

9

呼叫linkURL画布上:

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

的矩形是可点击区域,所以你必须匹配到绘制的矩形。参数是两个坐标,左下角和右上角的两个x, y

查看更多的例子在这篇博客文章:http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/

+0

最有可能@MartjinPieters不知道其他两个答案突出了问题。请参阅Crebits对修正方法的回答。 –

+1

@fatih_dur:谢谢你的提醒,我确实没有意识到其他帖子。 –

相关问题