9

Can you tell me how to insert image which will be a link to for example page 20? I know how to make with normal text:

text "<link anchor='page20'>Go to page 20</link>", :inline_format=>true

and then on page 20 I have

add_dest('page20', dest_fit(page.dictionary))

but how to do this with image ?

3
  • I'm currently having the same issue. Did you ever find a way to embed a link in an image using Prawn? May 25, 2013 at 15:49
  • Can you not just use HTML? <a href="path2file.pdf"> <img src="filepath" other stuff /> </a>
    – jamesc
    May 26, 2013 at 23:55
  • Looking at the Prawn documentation and the Prawn repo, I could find no mention of support for embedding a link in an image in a pdf. Ideally, I'd be able to do something like image "local_image.png", href: "http://example.com" or in a table cell { image: open("remote_image.png"), href: "http://example.com" } but I can't seem to, so I'm wondering if there's a workaround. May 27, 2013 at 11:51

2 Answers 2

6

Partly thanks to lightswitch05 for some prodding in the right direction, I've found a way to get the effect I want through this inelegant way:

  • Insert an image in a bounding_box (the page cursor is at this point at the bottom of the image)
  • Move the cursor back up to the top of the image
  • Insert a text link over the image (in my case I just used however many vertical bars '|' were needed to cover the image)
  • Confirm visually that the clickable link area is about the same as the boundaries of the image
  • Make the text link transparent, and voilà, it looks like you're clicking the image.

Here's some example code (measurements not exact; there was a lot of tweaking involved):

bounding_box([0, cursor], width: 35) do
  image open("http://mysite.com/remote_image.jpg"), 
        fit: [35, 35], 
        align: :center
  move_up 35
  transparent(0) do
    formatted_text([{
      text: "|||", # placeholder
      size: 40,
      link: "http://example.com/"
    }], align: :center)
  end
  # stroke_bounds
end

Needless to say, this experience has got me looking a bit more at Wicked PDF in order to do what I think I want to do with PDFs.

I'm sure a better/more elegant solution exists, so I'm not planning on considering this my final answer.

1
  • Transparent text! Wow, I was so close to that conclusion I can't believe I missed it. Good thinking! Anyways, I hope Prawn works out for you, but I highly recommend Wicked PDF if it doesn't May 31, 2013 at 12:58
5
+50

Prawn does not support this functionality. In fact, even if you placed a formatted_text_box over an image and fill it with white space, it still will not work. An anchor has to include text to work. If you don't mind having text over your image, then that might be a solution.

Prawn's own readme states:

One thing Prawn is not, and will never be, is an HTML to PDF generator.

After dealing with many of Prawn's shortcomings, I've switched to using wicked_pdf for my Ruby on Rails PDF generation and have been very happy with it. If you can do it in html & css, it can be done with wicked_pdf.

1
  • 2
    +1 for the useful info, especially "If you don't mind having text over your image, then that might be a solution", which was the basis for my hack, and the link to Wicked PDF. May 31, 2013 at 7:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.