我正在使用 ReportLab 使用 Python 制作 pdf。我想在 Canvas 上添加一个形状,并将该形状用作超链接。在以下指向 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()
最佳答案
在 Canvas 上调用 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/
关于python - 在 ReportLab 中向 Canvas 元素添加超链接的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688923/