Search code examples
ruby-on-railsrubypdfprawn

Can prawn generate PDFs with links?


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.


Solution

  • 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.