Method: Prawn::Document#initialize

Defined in:
lib/prawn/document.rb

#initialize(options = {}, &block) ⇒ Document

Creates a new PDF Document. The following options are available (with the default values marked in [])

:page_size

One of the Document::PageGeometry sizes [LETTER]

:page_layout

Either :portrait or :landscape

:margin

Sets the margin on all sides in points [0.5 inch]

:left_margin

Sets the left margin in points [0.5 inch]

:right_margin

Sets the right margin in points [0.5 inch]

:top_margin

Sets the top margin in points [0.5 inch]

:bottom_margin

Sets the bottom margin in points [0.5 inch]

:skip_page_creation

Creates a document without starting the first page [false]

:compress

Compresses content streams before rendering them [false]

:optimize_objects

Reduce number of PDF objects in output, at expense of render time [false]

:background

An image path to be used as background on all pages [nil]

:info

Generic hash allowing for custom metadata properties [nil]

:template

The path to an existing PDF file to use as a template [nil]

Setting e.g. the :margin to 100 points and the :left_margin to 50 will result in margins of 100 points on every side except for the left, where it will be 50.

The :margin can also be an array much like CSS shorthand:

# Top and bottom are 20, left and right are 100.
:margin => [20, 100]
# Top is 50, left and right are 100, bottom is 20.
:margin => [50, 100, 20]
# Top is 10, right is 20, bottom is 30, left is 40.
:margin => [10, 20, 30, 40]

Additionally, :page_size can be specified as a simple two value array giving the width and height of the document you need in PDF Points.

Usage:

# New document, US Letter paper, portrait orientation
pdf = Prawn::Document.new

# New document, A4 paper, landscaped
pdf = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)

# New document, Custom size
pdf = Prawn::Document.new(:page_size => [200, 300])

# New document, with background
pdf = Prawn::Document.new(:background => "#{Prawn::DATADIR}/images/pigs.jpg")


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/prawn/document.rb', line 171

def initialize(options={},&block)
  options = options.dup

  Prawn.verify_options [:page_size, :page_layout, :margin, :left_margin,
    :right_margin, :top_margin, :bottom_margin, :skip_page_creation,
    :compress, :skip_encoding, :background, :info,
    :optimize_objects, :template], options

  # need to fix, as the refactoring breaks this
  # raise NotImplementedError if options[:skip_page_creation]

  self.class.extensions.reverse_each { |e| extend e }
  @internal_state = Prawn::Core::DocumentState.new(options)
  @internal_state.populate_pages_from_store(self)
  min_version(state.store.min_version) if state.store.min_version

  @background = options[:background]
  @font_size  = 12

  @bounding_box  = nil
  @margin_box    = nil

  @page_number = 0

  options[:size] = options.delete(:page_size)
  options[:layout] = options.delete(:page_layout)

  if options[:template]
    fresh_content_streams(options)
    go_to_page(1)
  else
    if options[:skip_page_creation] || options[:template]
      start_new_page(options.merge(:orphan => true))
    else
      start_new_page(options)
    end
  end

  @bounding_box = @margin_box

  if block
    block.arity < 1 ? instance_eval(&block) : block[self]
  end
end