ReportLab Image Link

后端 未结 2 541
傲寒
傲寒 2021-01-05 16:41

Is there a way to add an href/link to a Platypus Image object in ReportLab? I know how to add a link on text in a Paragraph but I can\'t seem to find anything about adding a

2条回答
  •  迷失自我
    2021-01-05 17:12

    This can be easily achieved with the HyperlinkedImage class proposed by missmely:

    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)
    

提交回复
热议问题