Prawn PDF table mixed formats

Chris picture Chris · Sep 10, 2012 · Viewed 8.2k times · Source

I am trying to generate a simple table in ruby with prawn pdf.

I need some text in a cell to be bold, and some to be not bold. For example:

Example table Now following some examples, I have the basic table rendering with this code:

pdf.table([
  ["1. Row example text", "433"],
  ["2. Row example text", "2343"],
  ["3. Row example text", "342"],
  ["4. Row example text", "36"]], :width => 500, :cell_style => {
    :font_style => :bold})

But I can see absolutely no way of inserting more text into the first cell with a different format. (In this case I want it to be unbolded)

Does anyone know how to accomplish this?

Thanks for any help

Answer

saihgala picture saihgala · Sep 10, 2012
Prawn::Document.generate("test.pdf") do |pdf|
     table_data = [[Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>1. Row example text</b> \n\nExample Text Not Bolded", :inline_format => true), "433"],
                   [Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>2. Row example text</b>", :inline_format => true), "2343"],
                   [Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>3. Row example text</b>", :inline_format => true), "342"],                    
                   [Prawn::Table::Cell::Text.new( pdf, [0,0], :content => "<b>4. Row example text</b>", :inline_format => true), "36"]]

    pdf.table(table_data,:width => 500)
end

You can also do

Prawn::Document.generate("test.pdf") do |pdf|
    table_data = [["<b>1. Row example text</b>\n\nExample Text Not Bolded", "<b>433<b>"], 
                  ["<b>2. Row example text</b>", "<b>2343</b>"], 
                  ["<i>3. Row example text</i>", "<i>342</i>"],
                  ["<b>4. Row example text</b>", "<sub>36</sub>"]]

    pdf.table(table_data, :width => 500, :cell_style => { :inline_format => true })
end

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