[reportlab-users] Clickable URLs sample

Dinu Gherman reportlab-users@reportlab.com
Fri, 6 Sep 2002 10:27:07 +0200


Hi,

given the many requests for real URLs I'm providing a code snippet
below for easing the pain at least in *some* situations, namely when
you need a "standalone" clickable URL which I've found useful as a
headline, footnote, table cell, etc.

This is still not a solution for intra-paragraph links, but is a
nice workaround in the described cases when you are using Platypus
anyway. It should also be easy to expand.

Regards,

Dinu


# urlpara.py

from reportlab.platypus import Paragraph


class UrlParagraph(Paragraph):
     "A clickable non-splittable paragraph."

     def __init__(self, text, style, url, bulletText=None, frags=None):
         "Set text color to blue and memorize URL."

         self.url = url
         format = '<font color="blue">%s</font>'
         text = format % text
         # Now, business as usual.
         apply(Paragraph.__init__, (self, text, style, bulletText, 
frags))


     def split(self):
         "Don't allow vertical splitting."

         return []


     def draw(self):
         "Make the consumed area a clickable URL."

         Paragraph.draw(self)
         rect = (0, 0, self.width, self.height)
         self.canv.linkURL(self.url, rect, relative=1, thickness=0)