hyperlink over image in table

19 views
Skip to first unread message

James Miller

unread,
Jan 19, 2023, 11:00:53 PM1/19/23
to reportlab-users
Hi -
After solving my first challenge I now have a second. How do I overlay an image with a https:/// hyper link in a table. I tried changing out an image with a html img src and a href text in a Paragraph and putting that in the table. One of 4 images barely appears at the top of the page, instead of the 2x2 table with 4 images and hyperlinks on a page that is my end goal.

Any suggestions on how to accomplish this result?

Thanks,
James


James Miller

unread,
Jan 20, 2023, 3:23:14 PM1/20/23
to reportlab-users
I found the solution in Stack Overflow. I think it may have been provided by what of this group's members. I put the hyperlinkimage reference in a table and works perfectly

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)

Casey Abernathey

unread,
Feb 28, 2023, 4:48:25 PM2/28/23
to reportlab-users
I use that same method for mine, however, if you're going to override the Image class, it maybe a good addition to include SVG file type support. Add this to the __init__ call and check for svg != None to provide it to the super.__init__ call.

svg = None
# convert svg files
if self._path.endswith('.svg'):
from svglib.svglib import svg2rlg
svg = svg2rlg(self._path)

Reply all
Reply to author
Forward
0 new messages