Class: Prawn::Document::Table
- Inherits:
-
Object
- Object
- Prawn::Document::Table
- 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
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.
Instance Attribute Summary collapse
-
#col_widths ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#draw ⇒ Object
Draws the table onto the PDF document.
-
#initialize(data, document, options = {}) ⇒ Table
constructor
Creates a new Document::Table object.
-
#width ⇒ Object
Width of the table in PDF points.
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-
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,:centerorn, wherenis 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 [:left]
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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/prawn/document/table.rb', line 97 def initialize(data, document,={}) @data = data @document = document @font_size = [:font_size] || 12 @border_style = [:border_style] @border = [:border] || 1 @position = [:position] || :left @headers = [:headers] @row_colors = [:row_colors] @align = [:align] @horizontal_padding = [:horizontal_padding] || 5 @vertical_padding = [:vertical_padding] || 5 if [:padding] @horizontal_padding = @vertical_padding = [:padding] end @row_colors = ["ffffff","cccccc"] if @row_colors == :pdf_writer @original_row_colors = @row_colors.dup if @row_colors calculate_column_widths([:widths]) end |
Instance Attribute Details
#col_widths ⇒ Object (readonly)
:nodoc:
66 67 68 |
# File 'lib/prawn/document/table.rb', line 66 def col_widths @col_widths end |
Instance Method Details
#draw ⇒ Object
Draws the table onto the PDF document
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/prawn/document/table.rb', line 131 def draw case(@position) when :center x = (@document.bounds.width - width) / 2.0 y = @document.y - @document.bounds.absolute_bottom @document.bounding_box [x, y], :width => width do generate_table end when Numeric x = @position y = @document.y - @document.bounds.absolute_bottom @document.bounding_box [x,y], :width => width do generate_table end else generate_table end end |
#width ⇒ Object
Width of the table in PDF points
125 126 127 |
# File 'lib/prawn/document/table.rb', line 125 def width @col_widths.inject(0) { |s,r| s + r } end |