How do I set the font size in a prawn table?
Asked Answered
S

3

14

How do I set the font size in a PDF table using the prawn gem?

When I call prawn like the following:

pdf = Prawn::Document.new(:page_size => 'LEGAL', :page_layout => :landscape)
pdf.table data, 
    :header => true,
    :column_widths => widths,
    :font_size => 7,
    :row_colors => ["EEEEEE", "FFFFFF"]

I get an NoMethodError

undefined method `font_size=' for #<Prawn::Table:0x6ce37ea4>

When I remove the ":font_size => 7", it renders but I get an undesirable font size.

I am using prawn 0.12.0, ruby 1.9.3p194, and Rails 3.1.9.

Shelli answered 17/7, 2013 at 15:59 Comment(0)
B
24

You have to apply the size property to the cell text directly. Here is how to do this:

pdf.table data, 
  :header => true,
  :column_widths => widths,
  :cell_style => { size: 7 },
  :row_colors => ["EEEEEE", "FFFFFF"]

Source: http://prawn.majesticseacreature.com/manual.pdf

Barbabas answered 17/7, 2013 at 16:25 Comment(0)
J
4

:font_size => 7 not works.

The correct way is :size => 7

    pdf = Prawn::Document.new(:page_size => 'LEGAL', :page_layout => :landscape)
    pdf.table data, 
    :header => true,
    :column_widths => widths,
    :size => 7,
    :row_colors => ["EEEEEE", "FFFFFF"]
Jacksmelt answered 24/5, 2016 at 20:26 Comment(1)
What is your prawn version?Jacksmelt
N
0
pdf.table(data) do
  style row(0), :font_size => 7
end

I believe for 0.12.0 you could also use something like this:

table([[ {:font_size => 7 } ]])

https://github.com/prawnpdf/prawn/wiki/CHANGELOG

Noseband answered 17/7, 2013 at 16:20 Comment(2)
First example (block) raises NoMethodError "undefined method `font_size=' for #<Prawn::Table::Cell::Text:0x6b479454>"Shelli
Try size= instead of font_size=. "style row(0), :font_size => 7" does not work, but "style row(0), :size => 7" does work in more recent versions of Prawn.Dubai

© 2022 - 2024 — McMap. All rights reserved.