My HyperlinkedImage subclass of reportlab Image class is not working
<p>I'm trying to export a PDF with hyperlinked images for a report. I have images throughout my report, and wanted to add a hyperlink to that picture added in the PDF to open up the jpg in the folder where I grabbed the image. I found great solutions from @Meilo <a href="http://stackoverflow.com/questions/19596300/reportlab-image-link/26294527#26294527">ReportLab Image Link</a> and @missmely <a href="http://stackoverflow.com/questions/18114820/is-it-possible-to-get-a-flowables-coordinate-position-once-its-rendered-using">Is it possible to get a Flowable's coordinate position once it's rendered using ReportLab.platypus?</a></p>

<p>But, I keep getting errors when attempting to create an object from the HyperlinkClass. Could someone help me create the object and reference it correctly?</p>

<pre><code>from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import Flowable, SimpleDocTemplate, Paragraph, Spacer
from reportlab.platypus import Image, PageBreak, KeepTogether, ListFlowable,
from reportlab.platypus import ListItem, Table, ListItem, Table
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch

class HyperlinkedImage(Image, object):
    """
    Class reportlab.platypus.flowables has a class called Flowable that Image
    inherits from. Flowable has a method called
    drawOn(self, canvas, x, y, _sW=0) that I override in a new class I created
    called HyperlinkedImage...Now instead of creating a reportlab.platypus.
    Image as your image flowable, use the new HyperlinkedImage instead.
    See original post:
    http://stackoverflow.com/questions/18114820/is-it-possible-to-get-a-flowables-coordinate-position-once-its-rendered-using
    """
    # 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)

# Create PDF
doc = SimpleDocTemplate(
    "myHyperlinkedPics.pdf",
    pagesize=letter,
    rightMargin=60, leftMargin=60,
    topMargin=60, bottomMargin=80)

Story = []
styleSheet = getSampleStyleSheet()

logo = "IamZeroInjury.png"
im = Image(logo, 1 * inch, 1 * inch)

Story.append(KeepTogether(im))

myHyperlinkedImage = HyperlinkedImage(logo, hyperlink=logo)
Story.append(KeepTogether(myhyperlinkedImage(hyperlink=logo, 1*inch, 1*inch,
                                             1*inch, 1*inch)))

styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))

Story.append(PageBreak())

doc.build(Story)
</code></pre>

<p>When I run this code, I get the error:</p>

<pre class="lang-none prettyprint-override"><code>in __getattr__
  raise AttributeError("&lt;Image @ 0x%x&gt;.%s" % (id(self),a))
    AttributeError: &lt;Image @ 0x3752990&gt;.hAlignAdjust
</code></pre>

<p>The answer is actually proposed in @Dennis Golomazov's post on the second answer for the modified HyperlinkedImage subclass <a href="http://stackoverflow.com/questions/19596300/reportlab-image-link/39134216#39134216">ReportLab Image Link</a>. I knew something wasn't working with the hAlign parameter, and @Dennis Golmazov proposed a simple fix by adding the hAlign parameter into the <strong>init</strong> method.</p>

<p>For those wondering what the code is to use the HyperlinkedImage subclass, here's what works:</p>

<pre><code>from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import Flowable, SimpleDocTemplate, Paragraph
from reportlab.platypus import Image, KeepTogether, ListFlowable,
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch

doc = SimpleDocTemplate("myHyperlinkedPics.pdf", pagesize=letter, rightMargin=60, leftMargin=60, topMargin=60, bottomMargin=80)

Story = []
styleSheet = getSampleStyleSheet()

logo = "mypic.png"
im = Image(logo, 1 * inch, 1 * inch)
myHyperlinkedImage = HyperlinkedImage(logo, hyperlink='http://www.google.com', width=1*inch, height=1*inch)

Story.append(KeepTogether(myHyperlinkedImage))
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))

doc.build(Story)
</code></pre>

python image hyperlink reportlab