Class: Prawn::Document::Table

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/prawn/document/table.rb

Overview

This class implements simple PDF table generation.

Prawn tables have the following features:

* Can be generated with or without headers
* Can tweak horizontal and vertical padding of text
* Minimal styling support (borders / row background colors)
* Can be positioned by bounding boxes (left/center aligned) or an
  absolute x position
* Automated page-breaking as needed
* Column widths can be calculated automatically or defined explictly on a 
  column by column basis
* Text alignment can be set for the whole table or by column

The current implementation is a bit barebones, but covers most of the basic needs for PDF table generation. If you have feature requests, please share them at: groups.google.com/group/prawn-ruby

Tables will be revisited before the end of the Ruby Mendicant project and the most commonly needed functionality will likely be added.

Constant Summary collapse

NUMBER_PATTERN =

:nodoc:

/^-?(?:0|[1-9]\d*)(?:\.\d+(?:[eE][+-]?\d+)?)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#configuration

Constructor Details

#initialize(data, document, options = {}) ⇒ Table

Creates a new Document::Table object. This is generally called indirectly through Document#table but can also be used explictly.

The data argument is a two dimensional array of strings, organized by row, e.g. [[“r1-col1”,“r1-col2”],]. As with all Prawn text drawing operations, strings must be UTF-8 encoded.

The following options are available for customizing your tables, with defaults shown in [] at the end of each description.

:font_size

The font size for the text cells . [12]

:horizontal_padding

The horizontal cell padding in PDF points [5]

:vertical_padding

The vertical cell padding in PDF points [5]

:padding

Horizontal and vertical cell padding (overrides both)

:border_width

With of border lines in PDF points [1]

:border_style

If set to :grid, fills in all borders. Otherwise, borders are drawn on columns only, not rows

:position

One of :left, :center or n, where n is an x-offset from the left edge of the current bounding box

:widths: A hash of indices and widths in PDF points. E.g. { 0 => 50, 1 => 100 }

:row_colors

An array of row background colors which are used cyclicly.

:align

Alignment of text in columns, for entire table (:center) or by column ({ 0 => :left, 1 => :center})

:align_headers

Alignment of header text.

Row colors are specified as html encoded values, e.g. [“ffffff”,“aaaaaa”,“ccaaff”]. You can also specify :row_colors => :pdf_writer if you wish to use the default color scheme from the PDF::Writer library.

See Document#table for typical usage, as directly using this class is not recommended unless you know why you want to do it.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/prawn/document/table.rb', line 103

def initialize(data, document,options={})     
  unless data.all? { |e| Array === e }
    raise Prawn::Errors::InvalidTableData,
      "data must be a two dimensional array of Prawn::Cells or strings"
  end
  
  @data     = data        
  @document = document
  
  Prawn.verify_options [:font_size,:border_style, :border_width,
   :position, :headers, :row_colors, :align, :align_headers, 
   :horizontal_padding, :vertical_padding, :padding, :widths, 
   :header_color ], options     
                                      
  configuration.update(options)  

  if padding = options[:padding]
    C(:horizontal_padding => padding, :vertical_padding => padding) 
  end
   
  if options[:row_colors] == :pdf_writer 
    C(:row_colors => ["ffffff","cccccc"])  
  end
  
  if options[:row_colors]
    C(:original_row_colors => C(:row_colors)) 
  end

  calculate_column_widths(options[:widths])
end

Instance Attribute Details

#col_widthsObject (readonly)

:nodoc:



69
70
71
# File 'lib/prawn/document/table.rb', line 69

def col_widths
  @col_widths
end

Instance Method Details

#drawObject

Draws the table onto the PDF document



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/prawn/document/table.rb', line 144

def draw  
  @parent_bounds = @document.bounds  
  case C(:position) 
  when :center
    x = (@document.bounds.width - width) / 2.0
    dy = @document.bounds.absolute_top - @document.y
    @document.bounding_box [x, @parent_bounds.top], :width => width do 
      @document.move_down(dy)
      generate_table
    end
  when Numeric     
    x, y = C(:position), @document.y - @document.bounds.absolute_bottom
    @document.bounding_box([x,y], :width => width) { generate_table }
  else
    generate_table
  end
end

#widthObject

Width of the table in PDF points



138
139
140
# File 'lib/prawn/document/table.rb', line 138

def width
   @col_widths.inject(0) { |s,r| s + r }
end