【问题标题】:What is the simplest way to add a hyperlink to a canvas element in ReportLab?在 ReportLab 中向画布元素添加超链接的最简单方法是什么?
【发布时间】:2012-05-21 16:25:19
【问题描述】:

我正在使用 ReportLab 使用 Python 制作 pdf。我想在画布上添加一个形状,并让该形状充当超链接。将以下示例中的矩形链接到 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()

【问题讨论】:

    标签: python canvas hyperlink reportlab


    【解决方案1】:

    在画布上致电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/

    【讨论】:

    • 很可能@MartjinPieters 不知道其他两个答案突出显示的问题。请参阅 Crebits 对修改后方法的回答。
    • @fatih_dur:感谢提醒,我确实不知道其他帖子。
    【解决方案2】:

    为了补充 Martijn 的答案,linkURL 使用“默认”坐标系绘制一个矩形,即底部+上/左+右。由于默认画布使用自上而下坐标,我建议您根据画布高度进行快速修复。

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 2013-08-03
      • 1970-01-01
      • 2010-09-12
      • 2011-01-14
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多