Search code examples
ruby-on-railsrubyprawn

Prawn - Links inside table cells


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!


Solution

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