Can prawn generate PDFs with links?
Asked Answered
D

4

18

I need to embed a link into a generated pdf in a ruby on rails app. Is there a way to do this with prawn?

Reading about this it turns out that prawn-format was the answer for awhile, but 0.7.x broke this.

prawn-format uses the link_annotate(rect, options={}) function to create links. What options need to be passed into this to get it to create a link in the PDF?

edit:
I would like to see a code example of this being done if anyone has one.

Disobedience answered 7/2, 2010 at 5:59 Comment(1)
It would be far simpler to use princexml.com Admittedly a proprietary product, though demo use is just fine.Groundwork
G
10

If you are attempting to create a link to an external page (http://google.com), for instance you could use the following, to place a link that is 100x100 and placed at 5, 5 from the bottom left of the page, with a 1px border:

pdf.link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => Prawn::LiteralString.new("http://google.com") } )

Prawn Format would parse the text passed to the pdf.text method and find html a tags. It would then use regular expressions to parse out the target and link text and finally create a link like the one above with a bounding box (the first param) that would fit around the text that was within the tags. I'm not sure how you could achieve this without Prawn Format. But that is how you can create a link using link_annotation.

Gone answered 23/2, 2010 at 5:57 Comment(2)
Your explanation of the position parameters is wrong. The correct meaning of [100, 100, 5, 5] is [rightEdgeX, bottomEdgeY, leftEdgeX, topEdgeY]. Although this shouldn't be valid, it prints out a box as you expect. But try out [100,100,100,100] and it doesn't print an expected outcome. Tried and tested. It's weird though that this information is not easily accessible, not even in the PDF spec partners.adobe.com/public/developer/en/pdf/PDFReference.pdfHinton
For prawn 1.0.0 I use this link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => PDF::Core::LiteralString.new("http://google.com") } )Leftwards
G
27

I know this is an old question, but for those still stumbling upon it, in current versions of Prawn, you can use inline format like this:

pdf.text 'Website: <color rgb="0000FF"><u><link href="http://www.stackoverflow.com">stackoverflow</link></u></color>', :inline_format => true
Geopolitics answered 7/5, 2013 at 17:46 Comment(0)
G
10

If you are attempting to create a link to an external page (http://google.com), for instance you could use the following, to place a link that is 100x100 and placed at 5, 5 from the bottom left of the page, with a 1px border:

pdf.link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => Prawn::LiteralString.new("http://google.com") } )

Prawn Format would parse the text passed to the pdf.text method and find html a tags. It would then use regular expressions to parse out the target and link text and finally create a link like the one above with a bounding box (the first param) that would fit around the text that was within the tags. I'm not sure how you could achieve this without Prawn Format. But that is how you can create a link using link_annotation.

Gone answered 23/2, 2010 at 5:57 Comment(2)
Your explanation of the position parameters is wrong. The correct meaning of [100, 100, 5, 5] is [rightEdgeX, bottomEdgeY, leftEdgeX, topEdgeY]. Although this shouldn't be valid, it prints out a box as you expect. But try out [100,100,100,100] and it doesn't print an expected outcome. Tried and tested. It's weird though that this information is not easily accessible, not even in the PDF spec partners.adobe.com/public/developer/en/pdf/PDFReference.pdfHinton
For prawn 1.0.0 I use this link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => PDF::Core::LiteralString.new("http://google.com") } )Leftwards
R
1

As of Prawn 0.7, prawn-format is completely unsupported, and will not work with versions of Prawn 0.7+. Feel free to fork and fix, of course - prawn-format's homepage on github

The other option is to use prawn's built in low-level annotation support: http://prawn.majesticseacreature.com/docs/prawn-core/classes/Prawn/Document/Annotations.html#M000158

Heres the method:

link_annotation(rect, options={})

A convenience method for creating Link annotations. rect must be an array of four numbers, describing the bounds of the annotation. The options hash should include either :Dest (describing the target destination, usually as a string that has been recorded in the document‘s Dests tree), or :A (describing an action to perform on clicking the link), or :PA (for describing a URL to link to).

Rochette answered 8/2, 2010 at 12:4 Comment(1)
Do you know how to use this link_annotation function? If so, an example would useful.Disobedience
H
0

I recently did it like this - works great:

      formatted_text_box([{:text=>"Google", :link=>"https://google.com", :color=>"0000ee"}])
Humanist answered 8/4, 2018 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.