Search code examples
pythonhyperlinkreportlabclickableplatypus

How to create a link and anchor using platypus and ReportLab Pdf python


This is what the platypus documentation states:

The link tag can be used as a reference, but not as an anchor. The a and link hyperlink tags have additional attributes fontName, fontSize, color & backColor attributes. The hyperlink reference can have a scheme of http:(external webpage), pdf:(different pdf document) or document:(same pdf document); a missing scheme is treated as document as is the case when the reference starts with # (in which case the anchor should omit it). Any other scheme is treated as some kind of URI.

I want to create a clickable link that takes you to another page in the same pdf but im not sure how to create the "anchor", if anyone understands please can you help! (I am obviously not using a canvas) Thanks


Solution

  • Solution: Just copy and paste code to test.

    from reportlab.platypus import Paragraph, PageBreak, SimpleDocTemplate
    from reportlab.lib.styles import getSampleStyleSheet
    
    pdf = SimpleDocTemplate('PDFname.pdf')
    story = []
    styles = getSampleStyleSheet()
    story.append(Paragraph('This <a href="#MYANCHOR" color="blue">is a link to</a>', style=styles["Normal"])) # Linking the anchor
    story.append(PageBreak())
    story.append(Paragraph('<a name="MYANCHOR"/><font color="green">here</font>', style=styles["Normal"])) # Creating anchor
    pdf.build(story)