marfnk
marfnk

Reputation: 306

How to highlight text with background in a table with Prawn (prawn_to)?

I want to set some background color to parts of the content in a cell in a Prawn table.

My code looks like this:

#file: /show.pdf.prawn
pdf.table([ ["Type XY", "150", "1245.45"],
            ["Type ZA", "100", "1243.50"],
            ["Type BC", "20", "645.00"] ])

Only the XY, ZA, and BC should have a corresponding background color. In HTML I would write: <span style="background: yellow;">XY</span> - but this inline format is not supported yet by Prawn.

The only hint the Prawn manual is giving me is: text -> formatted callbacks. But this doesn't work in a table. Is there any possibility to do this? Is there any equivalent to the HTML span? Should I try a bounding box or an inner table?

Upvotes: 3

Views: 2114

Answers (2)

Shadoath
Shadoath

Reputation: 1061

Just came across code that can help you with this. Format by Cell instead of Column on the Table.

pdf.table(rows) do
  cells.style do |c|
    if c.column == 0 and c.row == 0
      c.background_color = "708DC6"
    elsif c.column == 1 and c.row == 0
      c.background_color = "2944ce"
    elsif c.column == 2 and c.row == 0
      c.background_color = "008000"
    end
  end
end

Now only the header row has colors and you can choose per column.

Upvotes: 0

ksugiarto
ksugiarto

Reputation: 951

Try

rows = [["Type XY", "150", "1245.45"],
        ["Type ZA", "100", "1243.50"],
        ["Type BC", "20", "645.00"]]

pdf.table(rows) do
  column(0).background_color = "708DC6" #the color
end

I suggest you to create another stand-alone model for prawn like on this tutorial.

Hope can help.

Upvotes: 1

Related Questions