Method: Prawn::Document#initialize

Defined in:
lib/prawn/document.rb

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

Creates a new PDF Document.

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:

“‘ruby # 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.

Examples:

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"
)

Parameters:

  • options (Hash{Symbol => any}) (defaults to: {})

Options Hash (options):

  • :page_size (String, Array(Number, Number)) — default: LETTER

    One of the ‘PDF::Core::PageGeometry` sizes.

  • :page_layout (:portrait, :landscape)

    Page orientation.

  • :margin (Number, Array<Number>) — default: [32]

    Sets the margin on all sides in points.

  • :left_margin (Number) — default: 32

    Sets the left margin in points.

  • :right_margin (Number) — default: 32

    Sets the right margin in points.

  • :top_margin (Number) — default: 32

    Sets the top margin in points.

  • :bottom_margin (Number) — default: 32

    Sets the bottom margin in points.

  • :skip_page_creation (Boolean) — default: false

    Creates a document without starting the first page.

  • :compress (Boolean) — default: false

    Compresses content streams before rendering them.

  • :background (String?) — default: nil

    An image path to be used as background on all pages.

  • :background_scale (Number?) — default: 1

    Background image scale.

  • :info (Hash{Symbol => any}?) — default: nil

    Generic hash allowing for custom metadata properties.

  • :text_formatter (Object) — default: Prawn::Text::Formatted::Parser

    The text formatter to use for ‘:inline_format`ted text.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/prawn/document.rb', line 227

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

  Prawn.verify_options(VALID_OPTIONS, options)

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

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

  renderer.min_version(1.6) if options[:print_scaling] == :none

  @background = options[:background]
  @background_scale = options[:background_scale] || 1
  @font_size = 12

  @bounding_box = nil
  @margin_box = nil

  @page_number = 0

  @text_formatter = options.delete(:text_formatter) ||
    Text::Formatted::Parser

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

  initialize_first_page(options)

  @bounding_box = @margin_box

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