Prawn - Links inside table cells
Asked Answered
M

2

5

I am trying to generate PDFs with Prawn. Inside my PDF template I have tables with cells. In one of those cells I have an email address:

cell_email = pdf.make_cell(:content => booking.user_email, :border_width => 0)

I want to make email to link to "mailto" link. I know I can link some way like this:

pdf.formatted_text([{:text => booking.user_email, :link => "mailto: #{booking.user_email}"}])

However combining those two lines (giving formatted text as a content) doesn't work:

cell_email = pdf.make_cell(:content => pdf.formatted_text([{:text => booking.user_email, :link => "mailto: #{booking.user_email}"}]), :border_width => 0)

Any ideas how can I overcome this issue (create an email link inside the table cell)?

Kind Regards and Many Thanks!

Mosul answered 9/7, 2012 at 7:13 Comment(0)
E
10

You can specify inline_format for the cell and create the link yourself:

cell_email = pdf.make_cell(
  :content => "<link href='mailto:#{booking.user_email}'>#{booking.user_email}</link>",
  :inline_format => true
)

You can specify inline_format for the whole table, too:

table data, :cell_style => { :inline_format => true }

Prawn's inline_format supports <b>, <i>, <u>, <strikethrough>, <sub>, <sup>, <font>, <color> and <link>.

Emotionality answered 9/7, 2012 at 8:46 Comment(1)
Great, thanks! The only caveat I have is, if you have nested tables, you'll need to add the inline format in an initializer block, like so: table(data) do; cells.each { |c| c.inline_format = true if c.respond_to?(:inline_format) }; endQualitative
D
2

The following underlines the link and gives it a blue colour:

link = make_cell(content: "<color rgb='0000FF'> <u> <link href='https://mcmap.net/q/1912922/-prawn-links-inside-table-cells'> #{booking.user_email} </link></u> </color>", inline_format: true)

data = [[link]]

table(data,:header => true, :row_colors =>["F0F0F0","FFFFCC"]) do      
end
Dorcy answered 22/5, 2020 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.