class Prawn::Table

Next-generation table drawing for Prawn.

Data

Data, for a Prawn table, is a two-dimensional array of objects that can be converted to cells (“cellable” objects). Cellable objects can be:

String

Produces a text cell. This is the most common usage.

Prawn::Table::Cell

If you have already built a Cell or have a custom subclass of Cell you want to use in a table, you can pass through Cell objects.

Prawn::Table

Creates a subtable (a table within a cell). You can use Prawn::Document#make_table to create a table for use as a subtable without immediately drawing it. See examples/table/bill.rb for a somewhat complex use of subtables.

Array

Creates a simple subtable. Create a Table object using make_table (see above) if you need more control over the subtable’s styling.

Options

Prawn/Layout provides many options to control style and layout of your table. These options are implemented with a uniform interface: the :foo option always sets the foo= accessor. See the accessor and method documentation for full details on the options you can pass. Some highlights:

cell_style

A hash of style options to style all cells. See the documentation on Prawn::Table::Cell for all cell style options.

header

If set to true, the first row will be repeated on every page. If set to an Integer, the first x rows will be repeated on every page. Row numbering (for styling and other row-specific options) always indexes based on your data array. Whether or not you have a header, row(n) always refers to the nth element (starting from 0) of the data array.

column_widths

Sets widths for individual columns. Manually setting widths can give better results than letting Prawn guess at them, as Prawn’s algorithm for defaulting widths is currently pretty boneheaded. If you experience problems like weird column widths or CannotFit errors, try manually setting widths on more columns.

position

Either :left (the default), :center, :right, or a number. Specifies the horizontal position of the table within its bounding box. If a number is provided, it specifies the distance in points from the left edge.

Initializer Block

If a block is passed to methods that initialize a table (Prawn::Table.new, Prawn::Document#table, Prawn::Document#make_table), it will be called after cell setup but before layout. This is a very flexible way to specify styling and layout constraints. This code sets up a table where the second through the fourth rows (1-3, indexed from 0) are each one inch (72 pt) wide:

pdf.table(data) do |table|
  table.rows(1..3).width = 72
end

As with Prawn::Document#initialize, if the block has no arguments, it will be evaluated in the context of the object itself. The above code could be rewritten as:

pdf.table(data) do
  rows(1..3).width = 72
end