4

I am confused with the links and reportlab. I would like to have a text with a word, which 'contains' a clickable link. For example, I write such text: This web-site is called StackOverflow. And I want the word StackOverflow to be clickable and to lead to the corresponding page.

Is there any way to achieve that?

UPDATE

In this case I want to add the hyperlink to the word, not just to the canvas.

Anna-Lischen
  • 856
  • 1
  • 16
  • 35
  • Possible duplicate of [What is the simplest way to add a hyperlink to a canvas element in ReportLab?](http://stackoverflow.com/questions/10688923/what-is-the-simplest-way-to-add-a-hyperlink-to-a-canvas-element-in-reportlab) – Steve Byrne Feb 03 '16 at 18:16
  • Did you look at this: http://stackoverflow.com/questions/10688923/what-is-the-simplest-way-to-add-a-hyperlink-to-a-canvas-element-in-reportlab – Steve Byrne Feb 03 '16 at 18:16
  • @StevenByrne I have seen it, but it's different, as far as I understand. They are just adding the link, like `http://google.com`, but I need to add the link to the word/behind the word. – Anna-Lischen Feb 03 '16 at 18:50
  • I'm not sure if you can in report lab, I'm fairly sure you have to do it their way of making a hyperlink rectangle over/under the text. I could be wrong though – Steve Byrne Feb 03 '16 at 18:52
  • @StevenByrne thank you. Are there any other options? What about documentation? I haven't found much on the URLs. Can you advice anything? – Anna-Lischen Feb 03 '16 at 19:00
  • Take a look at the user guide, page 70-73 looks interesting. https://www.reportlab.com/docs/reportlab-userguide.pdf What are you trying to accomplish, are you creating a PDF or editing one? If you are creating one I know there is a library that lets you do pure HTML to design PDFs which you could use an tag for links – Steve Byrne Feb 03 '16 at 19:06
  • I know this isn't directly what you are looking for, but I think it would solve your problem. I've been looking around and it doesn't look like you can directly add a URL to just text in report lab. http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/ Let me know if that works for you, good luck! – Steve Byrne Feb 05 '16 at 16:29
  • @StevenByrne THANK YOU!!! It worked! I am extremely tired (is there such a thing as overcoded? I like to code, but I am also a human), so will upload my code attempt probably tomorrow! But thanks! Thanks! Thanks!!! – Anna-Lischen Feb 05 '16 at 22:17
  • I know this is not the solution but it was a workaround for me. Actually most of current pdf readers see normal text written as a URL as clickable texts. I just used `canvas.drawString` and added 'www.stackoverflow.com'. As for the position to put the text next to it, `reportlab.pdfbase.pdfmetrics.stringWidth` gives you the tip. – Sedat Kestepe Oct 15 '20 at 16:15

2 Answers2

6

So, super great thanks to Steven Byrne, who suggested the solution by Jerry Stratton <- to whom also super great thanks!

I am just uploading this simple code, so for anyone who's in need of such a solution to have a look:

from reportlab import platypus
from  reportlab.lib.styles import ParagraphStyle as PS
from reportlab.platypus import SimpleDocTemplate

items = []
address = 'WHATEVERYOUWNATTOTYPE'
address = '<link href="' + 'http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/' + '">' + address + '</link>'
items.append(platypus.Paragraph(address, PS('body')))
doc = SimpleDocTemplate('testLInk.pdf')#, pagesize = reportlab.lib.pagesizes.portrait(reportlab.lib.pagesizes.A4))
doc.multiBuild(items)

This link will lead to the original solution.

Anna-Lischen
  • 856
  • 1
  • 16
  • 35
1

For those interested report lab alone 'doesn't add links to text', but it has an extension called platypus that will allow you to use HTML link tags to link text. The code below is provided by Jerry Stratton's blog post The code below is an example that worked for Anna-Lischen (modified to be a bit more generic)

from reportlab import platypus
from  reportlab.lib.styles import ParagraphStyle as PS
from reportlab.platypus import SimpleDocTemplate

items = []
address = '<link href="' + 'http://YourWebsite.com' + '">' + My Text + '</link>'
items.append(platypus.Paragraph(address, PS('body')))
doc = SimpleDocTemplate('testLInk.pdf')#, pagesize = reportlab.lib.pagesizes.portrait(reportlab.lib.pagesizes.A4))
doc.multiBuild(items)

As a side note a more common way would be to use the first option in the blog and use rectangles on the page however it is up to you to choose what works best for your situation. This is also a great resource for the Platypus FAQ

Steve Byrne
  • 1,320
  • 1
  • 16
  • 29