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)
0

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.