【问题标题】:ReportLab Image LinkReportLab 图片链接
【发布时间】:2016-08-25 06:55:08
【问题描述】:

有没有办法在 ReportLab 中向鸭嘴兽图像对象添加 href/链接?我知道如何在段落中的文本上添加链接,但我似乎找不到任何关于为图片添加链接的信息。

【问题讨论】:

  • 对于未来的探索者:也可以在段落<link> 中放置<img>。但危险等待着。与 HTML 不同,它是 valign 而不是 align。它是 valign=baseline 而不是 valign=bottomdocs 相反。然后神秘地链接可能根本不起作用,除非您在链接内部或链接之间添加纯文本。试试下划线。我知道是对的。

标签: python reportlab


【解决方案1】:

这可以通过missmely提出的HyperlinkedImage class轻松实现:

from reportlab.platypus import Image

class HyperlinkedImage(Image, object):

    # The only variable I added to __init__() is hyperlink. I default it to None for the if statement I use later.
    def __init__(self, filename, hyperlink=None, width=None, height=None, kind='direct', mask='auto', lazy=1):
        super(HyperlinkedImage, self).__init__(filename, width, height, kind, mask, lazy)
        self.hyperlink = hyperlink

    def drawOn(self, canvas, x, y, _sW=0):
        if self.hyperlink: # If a hyperlink is given, create a canvas.linkURL()
            x1 = self.hAlignAdjust(x, _sW) # This is basically adjusting the x coordinate according to the alignment given to the flowable (RIGHT, LEFT, CENTER)
            y1 = y
            x2 = x1 + self._width
            y2 = y1 + self._height
            canvas.linkURL(url=self.hyperlink, rect=(x1, y1, x2, y2), thickness=0, relative=1)
        super(HyperlinkedImage, self).drawOn(canvas, x, y, _sW)

【讨论】:

    【解决方案2】:

    这是一个小更新,可让 @Meilo 的出色答案适用于 reportlab 3.3.0。它修复了_hAlignAdjust 方法名称并添加了hAlign kwarg:

    from reportlab.platypus import Image
    
    class HyperlinkedImage(Image, object):
        """Image with a hyperlink, adopted from http://stackoverflow.com/a/26294527/304209."""
    
        def __init__(self, filename, hyperlink=None, width=None, height=None, kind='direct',
                     mask='auto', lazy=1, hAlign='CENTER'):
            """The only variable added to __init__() is hyperlink.
    
            It defaults to None for the if statement used later.
            """
            super(HyperlinkedImage, self).__init__(filename, width, height, kind, mask, lazy,
                                                   hAlign=hAlign)
            self.hyperlink = hyperlink
    
        def drawOn(self, canvas, x, y, _sW=0):
            if self.hyperlink:  # If a hyperlink is given, create a canvas.linkURL()
                # This is basically adjusting the x coordinate according to the alignment
                # given to the flowable (RIGHT, LEFT, CENTER)
                x1 = self._hAlignAdjust(x, _sW)
                y1 = y
                x2 = x1 + self._width
                y2 = y1 + self._height
                canvas.linkURL(url=self.hyperlink, rect=(x1, y1, x2, y2), thickness=0, relative=1)
            super(HyperlinkedImage, self).drawOn(canvas, x, y, _sW)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2015-03-09
      • 2012-04-11
      • 2012-12-23
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多