Method: Prawn::Document#repeat

Defined in:
lib/prawn/repeater.rb

#repeat(page_filter, options = {}, &block) ⇒ Object

Provides a way to execute a block of code repeatedly based on a page_filter. Since Stamp is used under the hood, this method is very space efficient.

Available page filters are:

:all        -- repeats on every page
:odd        -- repeats on odd pages
:even       -- repeats on even pages
some_array  -- repeats on every page listed in the array
some_range  -- repeats on every page included in the range
some_lambda -- yields page number and repeats for true return values

Also accepts an optional second argument for dynamic content which executes the code in the context of the filtered pages without using a Stamp.

Example:

Prawn::Document.generate("repeat.pdf", :skip_page_creation => true) do

  repeat :all do
    draw_text "ALLLLLL", :at => bounds.top_left
  end

  repeat :odd do
    draw_text "ODD", :at => [0,0]
  end

  repeat :even do
    draw_text "EVEN", :at => [0,0]
  end

  repeat [1,2] do
    draw_text "[1,2]", :at => [100,0]
  end

  repeat 2..4 do
    draw_text "2..4", :at => [200,0]
  end

  repeat(lambda { |pg| pg % 3 == 0 }) do
    draw_text "Every third", :at => [250, 20]
  end

  10.times do
    start_new_page
    draw_text "A wonderful page", :at => [400,400]
  end

  repeat(:all, :dynamic => true) do
    text page_number, :at => [500, 0]
  end

end


76
77
78
79
80
81
# File 'lib/prawn/repeater.rb', line 76

def repeat(page_filter, options = {}, &block)
  dynamic = options.fetch(:dynamic, false)
  repeaters << Prawn::Repeater.new(
    self, page_filter, dynamic, &block
  )
end