title: How to overlay text hyperlink on top of an image using reportlab?
body: <p>I am trying to create a pdf document with many pages. Each page contains an image on top of which are displayed texts with hyperlinks to other pages.</p>
  
  <p>I have managed to do this using <code>reportlab.pdfgen</code> and rectangle links, unfortunately the reader used (an e-reader) does not recognize this type of links.</p>
  
  <p>Instead of it, I managed to create text embedded hyperlinks (using <a href="https://stackoverflow.com/questions/35184311/how-to-add-a-link-to-the-word-using-reportlab">How to add a link to the word using reportlab?</a>) which are recognizes by my e-reader but I didn't manage to overlay them on an image.</p>
  
  <p>I tried to use the solution provided in this post (<a href="https://stackoverflow.com/questions/8185438/reportlab-add-background-image-by-using-platypus">reportlab: add background image by using platypus</a>) to use an image as background but it doesn't work. When I set the document's pagesize to the size of the image, the paragraph isn't shown. When I set it to a bigger size than the size of the image, the paragraph is shown above the image not overlaying it.</p>
  
  <p><strong>Here's my code:</strong></p>
  
  <pre><code>from reportlab.lib.styles import getSampleStyleSheet
  from reportlab.platypus import BaseDocTemplate, PageTemplate, Frame, Paragraph
  
  
  def draw_static(canvas, doc):
      # Save the current settings
      canvas.saveState()
  
      # Draw the image
      canvas.drawImage(r'path\to\the\image.png', 0, 0, width=500, height=500)
  
      # Restore setting to before function call
      canvas.restoreState()
  
  
  # Set up a basic template
  doc = BaseDocTemplate('test.pdf', pagesize=(500, 500))
  
  # Create a Frame for the Flowables (Paragraphs and such)
  frame = Frame(doc.leftMargin, doc.bottomMargin, 500, 500, id='normal')
  
  # Add the Frame to the template and tell the template to call draw_static for each page
  template = PageTemplate(id='test', frames=[frame], onPage=draw_static)
  
  # Add the template to the doc
  doc.addPageTemplates([template])
  
  # All the default stuff for generating a document
  styles = getSampleStyleSheet()
  story = []
  
  link = '&lt;link href="http://example.com"&gt;Text&lt;/link&gt;'
  
  P = Paragraph(link, styles["Normal"])
  
  story.append(P)
  doc.build(story)
  </code></pre>
  
  <p><strong>PS:</strong> This code is not complete (doesn't create multiple pages, etc ...) but is just a minimal example for trying to overlay text embedded hyperlinks on an image.</p>