prawn-layout 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates various table and cell features.
4
+ #
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
6
+
7
+ require "rubygems"
8
+ require "prawn"
9
+ require "prawn/layout"
10
+
11
+ headers, body = nil, nil
12
+
13
+ dir = File.expand_path(File.dirname(__FILE__))
14
+
15
+ ruby_18 do
16
+ require "fastercsv"
17
+ headers, *body = FasterCSV.read("#{dir}/addressbook.csv")
18
+ end
19
+
20
+ ruby_19 do
21
+ require "csv"
22
+ headers, *body = CSV.read("#{dir}/addressbook.csv",
23
+ :encoding => "utf-8")
24
+ end
25
+
26
+ Prawn::Document.generate("fancy_table.pdf", :page_layout => :landscape) do
27
+
28
+ #font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
29
+
30
+ mask(:y) { table body, :headers => headers,
31
+ :align => :center,
32
+ :border_style => :grid }
33
+
34
+ table [["This is", "A Test" ],
35
+ [ Prawn::Table::Cell.new( :text => "Of tables",
36
+ :background_color => "ffccff" ),
37
+ "Drawn Side"], ["By side", "and stuff" ]],
38
+ :position => 600,
39
+ :headers => ["Col A", "Col B"],
40
+ :border_width => 1,
41
+ :vertical_padding => 5,
42
+ :horizontal_padding => 3,
43
+ :font_size => 10,
44
+ :row_colors => :pdf_writer,
45
+ :column_widths => { 1 => 50 }
46
+
47
+ move_down 150
48
+
49
+ table [%w[1 2 3],%w[4 5 6],%w[7 8 9]],
50
+ :position => :center,
51
+ :border_width => 0,
52
+ :font_size => 40
53
+
54
+ cell [500,300],
55
+ :text => "This free flowing textbox shows how you can use Prawn's "+
56
+ "cells outside of a table with ease. Think of a 'cell' as " +
57
+ "simply a limited purpose bounding box that is meant for laying " +
58
+ "out blocks of text and optionally placing a border around it",
59
+ :width => 225, :padding => 10, :border_width => 2
60
+
61
+ font_size 24
62
+
63
+ cell [50,75],
64
+ :text => "This document demonstrates a number of Prawn's table features",
65
+ :border_style => :no_top, # :all, :no_bottom, :sides
66
+ :horizontal_padding => 5
67
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Generates a couple simple tables, including some UTF-8 text cells.
4
+ # Although this does not show all of the options available to table, the most
5
+ # common are used here. See fancy_table.rb for a more comprehensive example.
6
+ #
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
8
+
9
+ require "rubygems"
10
+ require "prawn"
11
+ require "prawn/layout"
12
+
13
+ Prawn::Document.generate("table.pdf") do
14
+ font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
15
+ table [["ὕαλον ϕαγεῖν", "baaar", "1" ],
16
+ ["This is","a sample", "2" ],
17
+ ["Table", "dont\ncha\nknow?", "3" ],
18
+ [ "It", "Rules", "4" ],
19
+ [ "It", "Rules", "4" ],
20
+ [ "It", "Rules", "4123231" ],
21
+ [ "It", "Rules", "22.5" ],
22
+ [ "It", "Rules", "4" ],
23
+ [ "It", "Rules", "4" ],
24
+ [ "It", "Rules", "4" ],
25
+ [ "It", "Rules", "4" ],
26
+ [ "It", "Rules", "4" ],
27
+ [ "It", "Rules\nwith an iron fist", "x" ],
28
+ [ "It", "Rules", "4" ],
29
+ [ "It", "Rules", "4" ],
30
+ [ "It", "Rules", "4" ],
31
+ [ "It", "Rules", "4" ],
32
+ [ "It", "Rules", "4" ],
33
+ [ "It", "Rules", "4" ],
34
+ [ "It", "Rules", "4" ],
35
+ [ "It", "Rules", "4" ],
36
+ [ "It", "Rules", "4" ]],
37
+
38
+ :font_size => 24,
39
+ :horizontal_padding => 10,
40
+ :vertical_padding => 3,
41
+ :border_width => 2,
42
+ :position => :center,
43
+ :headers => ["Column A","Column B","#"],
44
+ :align => {1 => :center},
45
+ :align_headers => :center
46
+
47
+ text "This should appear in the original font size, just below the table"
48
+ move_down 10
49
+
50
+ table [[ "Wide", "columns", "streeetch"],
51
+ ["are","mighty fine", "streeeeeeeech"]],
52
+ :column_widths => { 0 => 200, 1 => 200 }, :position => 5
53
+
54
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates the many controls over alignment and positioning in Prawn
4
+ # tables.
5
+ #
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
7
+
8
+ require "rubygems"
9
+ require "prawn"
10
+ require "prawn/layout"
11
+
12
+ Prawn::Document.generate "table_header_align.pdf" do
13
+ table [ ['01/01/2008', 'John Doe', '4.2', '125.00', '525.00'],
14
+ ['01/12/2008', 'Jane Doe', '3.2', '75.50', '241.60'] ] * 20,
15
+ :position => :center,
16
+ :headers => ['Date', 'Employee', 'Hours', 'Rate', 'Total'],
17
+ :column_widths => { 0 => 75, 1 => 100, 2 => 50, 3 => 50, 4 => 50},
18
+ :border_style => :grid,
19
+ :align => { 0 => :right, 1 => :left, 2 => :right, 3 => :right, 4 => :right },
20
+ :align_headers => { 0 => :center, 2 => :left, 3 => :left, 4 => :right }
21
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates how to set the table border color with the :border_color
4
+ # attribute.
5
+ #
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
7
+
8
+ require "rubygems"
9
+ require "prawn"
10
+ require "prawn/layout"
11
+
12
+ Prawn::Document.generate "table_border_color.pdf" do
13
+ table [ ['01/01/2008', 'John Doe', '4.2', '125.00', '525.00'],
14
+ ['01/12/2008', 'Jane Doe', '3.2', '75.50', '241.60'] ] * 20,
15
+ :position => :center,
16
+ :headers => ['Date', 'Employee', 'Hours', 'Rate', 'Total'],
17
+ :column_widths => { 0 => 75, 1 => 100, 2 => 50, 3 => 50, 4 => 50},
18
+ :border_style => :grid,
19
+ :border_color => "ff0000"
20
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates the use of the :col_span option when using Document#table
4
+ #
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
6
+
7
+ require "rubygems"
8
+ require "prawn"
9
+ require "prawn/layout"
10
+
11
+ Prawn::Document.generate "table_colspan.pdf" do
12
+ data = [ ['01/01/2008', 'John Doe', '4.2', '125.00', '525.00'],
13
+ ['01/12/2008', 'Jane Doe', '3.2', '75.50', '241.60'] ] * 5
14
+
15
+ data << [{:text => 'Total', :colspan => 2}, '37.0', '1002.5', '3833']
16
+
17
+ table data,
18
+ :position => :center,
19
+ :headers => ['Date', 'Employee', 'Hours', 'Rate', 'Total'],
20
+ :column_widths => { 0 => 75, 1 => 100, 2 => 50, 3 => 50, 4 => 50},
21
+ :border_style => :grid
22
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates explicitly setting the :header_color rather than inferring
4
+ # it from :row_colors in Document#table
5
+ #
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
7
+
8
+ require "rubygems"
9
+ require "prawn"
10
+ require "prawn/layout"
11
+
12
+ Prawn::Document.generate "table_header_color.pdf" do
13
+ table [ ['01/01/2008', 'John Doe', '4.2', '125.00', '525.00'],
14
+ ['01/12/2008', 'Jane Doe', '3.2', '75.50', '241.60'] ] * 20,
15
+ :position => :center,
16
+ :headers => ['Date', 'Employee', 'Hours', 'Rate', 'Total'],
17
+ :column_widths => { 0 => 75, 1 => 100, 2 => 50, 3 => 50, 4 => 50},
18
+ :border_style => :grid,
19
+ :header_color => 'f07878',
20
+ :header_text_color => "990000",
21
+ :row_colors => ["FFCCFF","CCFFCC"]
22
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates the :underline_header border style for Document#table.
4
+ #
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
6
+
7
+ require "rubygems"
8
+ require "prawn"
9
+ require "prawn/layout"
10
+
11
+ Prawn::Document.generate "table_header_underline.pdf" do
12
+ table [ ['01/01/2008', 'John Doe', '4.2', '125.00', '525.00'],
13
+ ['01/12/2008', 'Jane Doe', '3.2', '75.50', '241.60'] ] * 5,
14
+ :position => :center,
15
+ :headers => ['Date', 'Employee', 'Hours', 'Rate', 'Total'],
16
+ :column_widths => { 0 => 75, 1 => 100, 2 => 50, 3 => 50, 4 => 50},
17
+ :border_style => :underline_header
18
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Generates a couple simple tables, including some UTF-8 text cells.
4
+ # Although this does not show all of the options available to table, the most
5
+ # common are used here. See fancy_table.rb for a more comprehensive example.
6
+ #
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
8
+
9
+ require "rubygems"
10
+ require "prawn"
11
+ require "prawn/layout"
12
+
13
+ Prawn::Document.generate("table_widths.pdf") do
14
+
15
+ data = [
16
+ %w(one two three four),
17
+ %w(five six seven eight),
18
+ %w(nine ten eleven twelve),
19
+ %w(thirteen fourteen fifteen sixteen),
20
+ %w(seventeen eighteen nineteen twenty)
21
+ ]
22
+ headers = ["Column A","Column B","Column C", "Column D"]
23
+
24
+ text "A table with a specified width of the document width (within margins)"
25
+ move_down 10
26
+
27
+ table data,
28
+ :position => :center,
29
+ :headers => headers,
30
+ :width => margin_box.width
31
+
32
+
33
+ move_down 20
34
+ text "A table with a specified width of the document width (within margins) and two fixed width columns"
35
+ move_down 10
36
+
37
+ table data,
38
+ :position => :center,
39
+ :headers => headers,
40
+ :width => margin_box.width,
41
+ :column_widths => {0 => 70, 1 => 70}
42
+
43
+
44
+ move_down 20
45
+ text "A table with a specified width of 300"
46
+ move_down 10
47
+
48
+ table data,
49
+ :position => :center,
50
+ :headers => headers,
51
+ :width => 300
52
+
53
+
54
+ move_down 20
55
+ text "A table with too much data is automatically limited to the document width"
56
+ move_down 10
57
+
58
+ data << ['some text', 'A long piece of text that will make this cell too wide for the page', 'some more text', 'And more text']
59
+
60
+ table data,
61
+ :position => :center,
62
+ :headers => headers
63
+
64
+ end
@@ -0,0 +1,19 @@
1
+ require "prawn/table"
2
+
3
+ module Prawn
4
+
5
+ module Errors
6
+
7
+ # This error is raised when table data is malformed
8
+ #
9
+ InvalidTableData = Class.new(StandardError)
10
+
11
+ # This error is raised when an empty or nil table is rendered
12
+ #
13
+ EmptyTable = Class.new(StandardError)
14
+ end
15
+
16
+ module Layout
17
+ VERSION = "0.1.0"
18
+ end
19
+ end
@@ -0,0 +1,370 @@
1
+ # encoding: utf-8
2
+ #
3
+ # table.rb : Simple table drawing functionality
4
+ #
5
+ # Copyright June 2008, Gregory Brown. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require "prawn/table/cell"
10
+
11
+ module Prawn
12
+ class Document
13
+
14
+ # Builds and renders a Document::Table object from raw data.
15
+ # For details on the options that can be passed, see
16
+ # Document::Table.new
17
+ #
18
+ # data = [["Gregory","Brown"],["James","Healy"],["Jia","Wu"]]
19
+ #
20
+ # Prawn::Document.generate("table.pdf") do
21
+ #
22
+ # # Default table, without headers
23
+ # table(data)
24
+ #
25
+ # # Default table with headers
26
+ # table data, :headers => ["First Name", "Last Name"]
27
+ #
28
+ # # Very close to PDF::Writer's default SimpleTable output
29
+ # table data, :headers => ["First Name", "Last Name"],
30
+ # :font_size => 10,
31
+ # :vertical_padding => 2,
32
+ # :horizontal_padding => 5,
33
+ # :position => :center,
34
+ # :row_colors => :pdf_writer,
35
+ #
36
+ # # Grid border style with explicit column widths.
37
+ # table data, :border_style => :grid,
38
+ # :column_widths => { 0 => 100, 1 => 150 }
39
+ #
40
+ # end
41
+ #
42
+ # Will raise <tt>Prawn::Errors::EmptyTable</tt> given
43
+ # a nil or empty <tt>data</tt> paramater.
44
+ #
45
+ def table(data,options={})
46
+ if data.nil? || data.empty?
47
+ raise Prawn::Errors::EmptyTable,
48
+ "data must be a non-empty, non-nil, two dimensional array of Prawn::Cells or strings"
49
+ end
50
+ Prawn::Table.new(data,self,options).draw
51
+ end
52
+ end
53
+
54
+
55
+ # This class implements simple PDF table generation.
56
+ #
57
+ # Prawn tables have the following features:
58
+ #
59
+ # * Can be generated with or without headers
60
+ # * Can tweak horizontal and vertical padding of text
61
+ # * Minimal styling support (borders / row background colors)
62
+ # * Can be positioned by bounding boxes (left/center aligned) or an
63
+ # absolute x position
64
+ # * Automated page-breaking as needed
65
+ # * Column widths can be calculated automatically or defined explictly on a
66
+ # column by column basis
67
+ # * Text alignment can be set for the whole table or by column
68
+ #
69
+ # The current implementation is a bit barebones, but covers most of the
70
+ # basic needs for PDF table generation. If you have feature requests,
71
+ # please share them at: http://groups.google.com/group/prawn-ruby
72
+ #
73
+ # Tables will be revisited before the end of the Ruby Mendicant project and
74
+ # the most commonly needed functionality will likely be added.
75
+ #
76
+ class Table
77
+
78
+ include Prawn::Configurable
79
+
80
+ attr_reader :column_widths # :nodoc:
81
+
82
+ NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+(?:[eE][+-]?\d+)?)?$/ #:nodoc:
83
+
84
+ # Creates a new Document::Table object. This is generally called
85
+ # indirectly through Document#table but can also be used explictly.
86
+ #
87
+ # The <tt>data</tt> argument is a two dimensional array of strings,
88
+ # organized by row, e.g. [["r1-col1","r1-col2"],["r2-col2","r2-col2"]].
89
+ # As with all Prawn text drawing operations, strings must be UTF-8 encoded.
90
+ #
91
+ # The following options are available for customizing your tables, with
92
+ # defaults shown in [] at the end of each description.
93
+ #
94
+ # <tt>:headers</tt>:: An array of table headers, either strings or Cells. [Empty]
95
+ # <tt>:align_headers</tt>:: Alignment of header text. Specify for entire header (<tt>:left</tt>) or by column (<tt>{ 0 => :right, 1 => :left}</tt>). If omitted, the header alignment is the same as the column alignment.
96
+ # <tt>:header_text_color</tt>:: Sets the text color of the headers
97
+ # <tt>:header_color</tt>:: Manually sets the header color
98
+ # <tt>:font_size</tt>:: The font size for the text cells . [12]
99
+ # <tt>:horizontal_padding</tt>:: The horizontal cell padding in PDF points [5]
100
+ # <tt>:vertical_padding</tt>:: The vertical cell padding in PDF points [5]
101
+ # <tt>:padding</tt>:: Horizontal and vertical cell padding (overrides both)
102
+ # <tt>:border_width</tt>:: With of border lines in PDF points [1]
103
+ # <tt>:border_style</tt>:: If set to :grid, fills in all borders. If set to :underline_header, underline header only. Otherwise, borders are drawn on columns only, not rows
104
+ # <tt>:border_color</tt>:: Sets the color of the borders.
105
+ # <tt>:position</tt>:: One of <tt>:left</tt>, <tt>:center</tt> or <tt>n</tt>, where <tt>n</tt> is an x-offset from the left edge of the current bounding box
106
+ # <tt>:width:</tt> A set width for the table, defaults to the sum of all column widths
107
+ # <tt>:column_widths:</tt> A hash of indices and widths in PDF points. E.g. <tt>{ 0 => 50, 1 => 100 }</tt>
108
+ # <tt>:row_colors</tt>:: An array of row background colors which are used cyclicly.
109
+ # <tt>:align</tt>:: Alignment of text in columns, for entire table (<tt>:center</tt>) or by column (<tt>{ 0 => :left, 1 => :center}</tt>)
110
+ # <tt>:minimum_rows</tt>:: The minimum rows to display on a page, including header.
111
+ #
112
+ # Row colors are specified as html encoded values, e.g.
113
+ # ["ffffff","aaaaaa","ccaaff"]. You can also specify
114
+ # <tt>:row_colors => :pdf_writer</tt> if you wish to use the default color
115
+ # scheme from the PDF::Writer library.
116
+ #
117
+ # See Document#table for typical usage, as directly using this class is
118
+ # not recommended unless you know why you want to do it.
119
+ #
120
+ def initialize(data, document,options={})
121
+ unless data.all? { |e| Array === e }
122
+ raise Prawn::Errors::InvalidTableData,
123
+ "data must be a two dimensional array of Prawn::Cells or strings"
124
+ end
125
+
126
+ @data = data
127
+ @document = document
128
+
129
+ Prawn.verify_options [:font_size,:border_style, :border_width,
130
+ :position, :headers, :row_colors, :align, :align_headers,
131
+ :header_text_color, :border_color, :horizontal_padding,
132
+ :vertical_padding, :padding, :column_widths, :width, :header_color ],
133
+ options
134
+
135
+ configuration.update(options)
136
+
137
+ if padding = options[:padding]
138
+ C(:horizontal_padding => padding, :vertical_padding => padding)
139
+ end
140
+
141
+ if options[:row_colors] == :pdf_writer
142
+ C(:row_colors => ["ffffff","cccccc"])
143
+ end
144
+
145
+ if options[:row_colors]
146
+ C(:original_row_colors => C(:row_colors))
147
+ end
148
+
149
+ calculate_column_widths(options[:column_widths], options[:width])
150
+ end
151
+
152
+ attr_reader :column_widths #:nodoc:
153
+
154
+ # Width of the table in PDF points
155
+ #
156
+ def width
157
+ @column_widths.inject(0) { |s,r| s + r }
158
+ end
159
+
160
+ # Draws the table onto the PDF document
161
+ #
162
+ def draw
163
+ @parent_bounds = @document.bounds
164
+ case C(:position)
165
+ when :center
166
+ x = (@document.bounds.width - width) / 2.0
167
+ dy = @document.bounds.absolute_top - @document.y
168
+ @document.bounding_box [x, @parent_bounds.top], :width => width do
169
+ @document.move_down(dy)
170
+ generate_table
171
+ end
172
+ when Numeric
173
+ x, y = C(:position), @document.y - @document.bounds.absolute_bottom
174
+ @document.bounding_box([x,y], :width => width) { generate_table }
175
+ else
176
+ generate_table
177
+ end
178
+ end
179
+
180
+ private
181
+
182
+ def default_configuration
183
+ { :font_size => 12,
184
+ :border_width => 1,
185
+ :position => :left,
186
+ :horizontal_padding => 5,
187
+ :vertical_padding => 5 }
188
+ end
189
+
190
+ def calculate_column_widths(manual_widths=nil, width=nil)
191
+ @column_widths = [0] * @data[0].inject(0){ |acc, e|
192
+ acc += (e.is_a?(Hash) && e.has_key?(:colspan)) ? e[:colspan] : 1 }
193
+ renderable_data.each do |row|
194
+ row.each_with_index do |cell,i|
195
+ length = cell.to_s.lines.map { |e|
196
+ @document.font.width_of(e, :size => C(:font_size)) }.max.to_f +
197
+ 2*C(:horizontal_padding)
198
+ @column_widths[i] = length.ceil if length > @column_widths[i]
199
+ end
200
+ end
201
+
202
+ manual_width = 0
203
+ manual_widths.each { |k,v|
204
+ @column_widths[k] = v; manual_width += v } if manual_widths
205
+
206
+ #Ensures that the maximum width of the document is not exceeded
207
+ #Takes into consideration the manual widths specified (With full manual
208
+ # widths specified, the width can exceed the document width as manual
209
+ # widths are taken as gospel)
210
+ max_width = width || @document.margin_box.width
211
+ calculated_width = @column_widths.inject {|sum,e| sum += e }
212
+
213
+ if calculated_width > max_width
214
+ shrink_by = (max_width - manual_width).to_f /
215
+ (calculated_width - manual_width)
216
+ @column_widths.each_with_index { |c,i|
217
+ @column_widths[i] = c * shrink_by if manual_widths.nil? ||
218
+ manual_widths[i].nil?
219
+ }
220
+ elsif width && calculated_width < width
221
+ grow_by = (width - manual_width).to_f /
222
+ (calculated_width - manual_width)
223
+ @column_widths.each_with_index { |c,i|
224
+ @column_widths[i] = c * grow_by if manual_widths.nil? ||
225
+ manual_widths[i].nil?
226
+ }
227
+ end
228
+ end
229
+
230
+ def renderable_data
231
+ C(:headers) ? [C(:headers)] + @data : @data
232
+ end
233
+
234
+ def generate_table
235
+ page_contents = []
236
+ y_pos = @document.y
237
+
238
+ @document.font_size C(:font_size) do
239
+ renderable_data.each_with_index do |row,index|
240
+ c = Prawn::Table::CellBlock.new(@document)
241
+
242
+ col_index = 0
243
+ row.each do |e|
244
+ case C(:align)
245
+ when Hash
246
+ align = C(:align)[col_index]
247
+ else
248
+ align = C(:align)
249
+ end
250
+
251
+
252
+ align ||= e.to_s =~ NUMBER_PATTERN ? :right : :left
253
+
254
+ case e
255
+ when Prawn::Table::Cell
256
+ e.document = @document
257
+ e.width = @column_widths[col_index]
258
+ e.horizontal_padding = C(:horizontal_padding)
259
+ e.vertical_padding = C(:vertical_padding)
260
+ e.border_width = C(:border_width)
261
+ e.border_style = :sides
262
+ e.align = align
263
+ c << e
264
+ else
265
+ text = e.is_a?(Hash) ? e[:text] : e.to_s
266
+ width = if e.is_a?(Hash) && e.has_key?(:colspan)
267
+ @column_widths.slice(col_index, e[:colspan]).inject {
268
+ |sum, width| sum + width }
269
+ else
270
+ @column_widths[col_index]
271
+ end
272
+
273
+ c << Prawn::Table::Cell.new(
274
+ :document => @document,
275
+ :text => text,
276
+ :width => width,
277
+ :horizontal_padding => C(:horizontal_padding),
278
+ :vertical_padding => C(:vertical_padding),
279
+ :border_width => C(:border_width),
280
+ :border_style => :sides,
281
+ :align => align )
282
+ end
283
+
284
+ col_index += (e.is_a?(Hash) && e.has_key?(:colspan)) ? e[:colspan] : 1
285
+ end
286
+
287
+ bbox = @parent_bounds.stretchy? ? @document.margin_box : @parent_bounds
288
+ if c.height > y_pos - bbox.absolute_bottom
289
+ if C(:headers) && page_contents.length == 1
290
+ @document.start_new_page
291
+ y_pos = @document.y
292
+ else
293
+ draw_page(page_contents)
294
+ @document.start_new_page
295
+ if C(:headers) && page_contents.any?
296
+ page_contents = [page_contents[0]]
297
+ y_pos = @document.y - page_contents[0].height
298
+ else
299
+ page_contents = []
300
+ y_pos = @document.y
301
+ end
302
+ end
303
+ end
304
+
305
+ page_contents << c
306
+
307
+ y_pos -= c.height
308
+
309
+ if index == renderable_data.length - 1
310
+ draw_page(page_contents)
311
+ end
312
+
313
+ end
314
+ end
315
+ end
316
+
317
+ def draw_page(contents)
318
+ return if contents.empty?
319
+
320
+ if C(:border_style) == :underline_header
321
+ contents.each { |e| e.border_style = :none }
322
+ contents.first.border_style = :bottom_only if C(:headers)
323
+ elsif C(:border_style) == :grid || contents.length == 1
324
+ contents.each { |e| e.border_style = :all }
325
+ else
326
+ contents.first.border_style = C(:headers) ? :all : :no_bottom
327
+ contents.last.border_style = :no_top
328
+ end
329
+
330
+ if C(:headers)
331
+ contents.first.cells.each_with_index do |e,i|
332
+ if C(:align_headers)
333
+ case C(:align_headers)
334
+ when Hash
335
+ align = C(:align_headers)[i]
336
+ else
337
+ align = C(:align_headers)
338
+ end
339
+ end
340
+ e.align = align if align
341
+ e.text_color = C(:header_text_color) if C(:header_text_color)
342
+ e.background_color = C(:header_color) if C(:header_color)
343
+ end
344
+ end
345
+
346
+ contents.each do |x|
347
+ unless x.background_color
348
+ x.background_color = next_row_color if C(:row_colors)
349
+ end
350
+ x.border_color = C(:border_color) if C(:border_color)
351
+
352
+ x.draw
353
+ end
354
+
355
+ reset_row_colors
356
+ end
357
+
358
+
359
+ def next_row_color
360
+ color = C(:row_colors).shift
361
+ C(:row_colors).push(color)
362
+ color
363
+ end
364
+
365
+ def reset_row_colors
366
+ C(:row_colors => C(:original_row_colors).dup) if C(:row_colors)
367
+ end
368
+
369
+ end
370
+ end