Class: Prawn::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/table.rb,
lib/prawn/table/cell.rb,
lib/prawn/layout/grid.rb,
lib/prawn/layout/page.rb

Defined Under Namespace

Classes: Box, Grid, LazyBoundingBox, MultiBox

Instance Method Summary collapse

Instance Method Details

#cell(point, options = {}) ⇒ Object

Builds and renders a Table::Cell. A cell is essentially a special-purpose bounding box designed for flowing text within a bordered area. For available options, see Table::Cell#new.

Prawn::Document.generate("cell.pdf") do
   cell [100,500], 
     :width => 200,
     :text  => "The rain in Spain falls mainly on the plains"
end


21
22
23
24
# File 'lib/prawn/table/cell.rb', line 21

def cell(point, options={})
  Prawn::Table::Cell.new(
    options.merge(:document => self, :point => point)).draw
end

#define_grid(options = {}) ⇒ Object

Defines the grid system for a particular document. Takes the number of rows and columns and the width to use for the gutter as the keys :rows, :columns, :gutter



8
9
10
# File 'lib/prawn/layout/grid.rb', line 8

def define_grid(options = {})
  @grid = Grid.new(self, options)
end

A footer is a LazyBoundingBox drawn relative to the margins that can be repeated on every page of the document.

Unless :width or :height are specified, the margin_box width and height are used.

footer [margin_box.left, margin_box.bottom + 25] do
  stroke_horizontal_rule
  text "And here's a sexy footer", :size => 16
end


76
77
78
# File 'lib/prawn/layout/page.rb', line 76

def footer(top_left,options={},&block)       
  @footer = repeating_page_element(top_left,options,&block)
end

#grid(*args) ⇒ Object

A method that can either be used to access a particular grid on the page or work with the grid system directly.

@pdf.grid                 # Get the Grid directly
@pdf.grid([0,1])          # Get the box at [0,1]
@pdf.grid([0,1], [1,2])   # Get a multi-box spanning from [0,1] to [1,2]


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prawn/layout/grid.rb', line 19

def grid(*args)
  @boxes ||= {}
  @boxes[args] ||= if args.empty?
    @grid
  else
    g1, g2 = args
    if(g1.class == Array && g2.class == Array && 
      g1.length == 2 && g2.length == 2)
      multi_box(single_box(*g1), single_box(*g2))
    else
      single_box(g1, g2)
    end
  end
end

#header(top_left, options = {}, &block) ⇒ Object

A header is a LazyBoundingBox drawn relative to the margins that can be repeated on every page of the document.

Unless :width or :height are specified, the margin_box width and height are used.

 header margin_box.top_left do 
  text "Here's My Fancy Header", :size => 25, :align => :center   
  stroke_horizontal_rule
end


61
62
63
# File 'lib/prawn/layout/page.rb', line 61

def header(top_left,options={},&block)   
  @header = repeating_page_element(top_left,options,&block)
end

#lazy_bounding_box(*args, &block) ⇒ Object

A LazyBoundingBox is simply a BoundingBox with an action tied to it to be executed later. The lazy_bounding_box method takes the same arguments as bounding_box, but returns a LazyBoundingBox object instead of executing the code block directly.

You can then call LazyBoundingBox#draw at any time (or multiple times if you wish), and the contents of the block will then be run. This can be useful for assembling repeating page elements or reusable components.

file = "lazy_bounding_boxes.pdf"
Prawn::Document.generate(file, :skip_page_creation => true) do                    
  point = [bounds.right-50, bounds.bottom + 25]
  page_counter = lazy_bounding_box(point, :width => 50) do   
    text "Page: #{page_count}"
  end 

  10.times do         
   start_new_page
    text "Some text"  
    page_counter.draw
  end
end


34
35
36
37
38
39
# File 'lib/prawn/layout/page.rb', line 34

def lazy_bounding_box(*args,&block)
  translate!(args[0])  
  box = LazyBoundingBox.new(self,*args)
  box.action(&block)
  return box 
end

#padded_box(margin, &block) ⇒ Object

A bounding box with the same dimensions of its parents, minus a margin on all sides



44
45
46
47
48
# File 'lib/prawn/layout/page.rb', line 44

def padded_box(margin, &block)
  bounding_box [bounds.left + margin, bounds.top - margin],
    :width  => bounds.width - (margin * 2), 
    :height => bounds.height - (margin * 2), &block 
end

#table(data, options = {}) ⇒ Object

Builds and renders a Document::Table object from raw data. For details on the options that can be passed, see Document::Table.new

data = [["Gregory","Brown"],["James","Healy"],["Jia","Wu"]]

Prawn::Document.generate("table.pdf") do

  # Default table, without headers
  table(data)

  # Default table with headers
  table data, :headers => ["First Name", "Last Name"]

  # Very close to PDF::Writer's default SimpleTable output
  table data, :headers            => ["First Name", "Last Name"],
              :font_size          => 10,
              :vertical_padding   => 2,
              :horizontal_padding => 5,
              :position           => :center,
              :row_colors         => :pdf_writer,

  # Grid border style with explicit column widths.
  table data, :border_style => :grid,
              :column_widths       => { 0 => 100, 1 => 150 }

end

Will raise <tt>Prawn::Errors::EmptyTable</tt> given 
a nil or empty <tt>data</tt> paramater.


45
46
47
48
49
50
51
52
# File 'lib/prawn/table.rb', line 45

def table(data, options={})           
  if data.nil? || data.empty?
    raise Prawn::Errors::EmptyTable,
      "data must be a non-empty, non-nil, two dimensional array " +
      "of Prawn::Cells or strings"
  end
  Prawn::Table.new(data,self,options).draw
end