报告实验室图片链接 [英] ReportLab Image Link

查看:43
本文介绍了报告实验室图片链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 link for an Image.

推荐答案

这可以通过 HyperlinkedImage 类 由missmely 提出:

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)

这篇关于报告实验室图片链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆