0

I want to add some extra text with external webpage hyperlink to an existing PDF using Python, what is the best way to go about this.

This code is just adding text to pdf but not hyperlink.

Below code add link but it's regenerate new pdf -

from reportlab.platypus import Paragraph, PageBreak, SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet

pdf = SimpleDocTemplate('example.pdf')
story = []
styles = getSampleStyleSheet()
story.append(Paragraph('This <a href="http://google.com/" color="blue">is a link to</a>', style=styles["Normal"]))
pdf.build(story)
Puneet
  • 33
  • 1
  • 10

1 Answers1

1

I think you're looking for writer.add_uri(...):

from PyPDF2 import PdfReader, PdfWriter

reader = PdfReader("example.pdf")
writer = PdfWriter()

for page in reader.pages:
    writer.add_page(page)

# Make a black rectangle in the bottom-left corner with the link
writer.add_uri(
    pagenum=0,
    uri="https://martin-thoma.com/",
    rect=(10, 10, 100, 50)
)

with open("out.pdf", "wb") as fp:
    writer.write(fp)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958