How to set table headers in Prawn?
Asked Answered
C

3

9

I'm following this tutorial to create a pdf file using prawn gem, and I found this reference documentation to generate a table.

How do I set the header row and the header titles to each column?

invoiceData = [["foo","bar"]]
pdf.table(invoiceData) do |table|
table.rows(1..3).width = 72
end
Carrasquillo answered 15/4, 2011 at 15:36 Comment(1)
Disclaimer: This doesn't answer your question. I have turned HTML to PDF with Wkhtmltopdf. WAY easier, though there is a bit configuration to get it set up. I think I read about a gem being available that uses it. Maybe that simplifies the config. I think it's worth checking out.Suspect
K
11

If you pass :header => true as an option it should use the first row of your array as a repeating header. From the docs:

data = [["This row should be repeated on every new page"]]
data += [["..."]] * 30
table(data, :header => true)
Kitchener answered 15/4, 2011 at 16:21 Comment(2)
Also, try the new "self documenting manual". It's a bit more illustrative than the rdoc. pdf manualKitchener
If you look at page 85 (I think) in the pdf manual it appears to be working with column headers. I haven't had the chance to actually try it myself yet.Kitchener
I
2

In addition to the answer of @dogenpunk, it is also possible to set the styling of the header row:

table_data = generate_lots_of_table_data
table_data.unshift %w(id name address) # add headers

table(table_data, header: true) do
  row(0).style font_style: :bold # header style
end
Insert answered 6/2, 2017 at 10:21 Comment(0)
P
0

this works for me:

pdf.table data, :headers => ['foo', 'bar']
Proceeding answered 11/8, 2011 at 9:27 Comment(1)
Would be great if this worked, but ends up throwing NoMethodError (undefined method headers=' for an instance of Prawn::Table)` Maybe it used to work, not sure.Maurer

© 2022 - 2024 — McMap. All rights reserved.