Method: PDF::Writer#initialize

Defined in:
lib/extensions/pdf-writer/pdf/writer.rb

#initialize(options = {}) {|_self| ... } ⇒ Writer

Creates a new PDF document as a writing canvas. It accepts three named parameters:

:paper

Specifies the size of the default page in PDF::Writer. This may be a four-element array of coordinates specifying the lower-left (xll, yll) and upper-right (xur, yur) corners, a two-element array of width and height in centimetres, or a page name as defined in PAGE_SIZES.

:orientation

The orientation of the page, either long (:portrait) or wide (:landscape). This may be used to swap the width and the height of the page.

:version

The feature set available to the document is limited by the PDF version. Setting this version restricts the feature set available to PDF::Writer. PDF::Writer currently supports PDF version 1.3 features and does not yet support advanced features from PDF 1.4, 1.5, or 1.6.

Yields:

  • (_self)

Yield Parameters:

  • _self (PDF::Writer)

    the object that the method was called on



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/extensions/pdf-writer/pdf/writer.rb', line 325

def initialize(options = {})
  paper       = options[:paper] || "LETTER"
  orientation = options[:orientation] || :portrait
  version     = options[:version] || PDF_VERSION_13

  @mutex = Mutex.new
  @current_id = @current_font_id = 0

    # Start the document
  @objects              = []
  @callbacks            = []
  @font_families        = {}
  @fonts                = {}
  @stack                = []
  @state_stack          = StateStack.new
  @loose_objects        = []
  @current_text_state   = ""
  @options              = {}
  @destinations         = {}
  @add_loose_objects    = {}
  @images               = []
  @word_space_adjust    = nil
  @current_stroke_style = PDF::Writer::StrokeStyle.new(1)
  @page_numbering       = nil
  @arc4                 = nil
  @encryption           = nil
  @file_identifier      = nil

  @columns              = {}
  @columns_on           = false
  @insert_mode          = nil

  @catalog  = PDF::Writer::Object::Catalog.new(self)
  @outlines = PDF::Writer::Object::Outlines.new(self)
  @pages    = PDF::Writer::Object::Pages.new(self)

  @current_node	= @pages
  @procset  = PDF::Writer::Object::Procset.new(self)
  @info     = PDF::Writer::Object::Info.new(self)
  @page     = PDF::Writer::Object::Page.new(self)
  @current_text_render_style  = 0
  @first_page     = @page

  @version        = version

    # Initialize the default font families.
  init_font_families

  @font_size = 10
  @pageset = [@pages.first_page]

  if paper.kind_of?(Array)
    if paper.size == 4
      size = paper # Coordinate Array
    else
      size = [0, 0, PDF::Writer.cm2pts(paper[0]), PDF::Writer.cm2pts(paper[1])]
        # Paper size in centimeters has been passed
    end
  else
    size = PAGE_SIZES[paper.upcase].dup
  end
  size[3], size[2] = size[2], size[3] if orientation == :landscape

  @pages.media_box  = size

  @page_width       = size[2] - size[0]
  @page_height      = size[3] - size[1]
  @y = @page_height

    # Also set the margins to some reasonable defaults -- 1.27 cm, 36pt,
    # or 0.5 inches.
  margins_pt(36)

    # Set the current writing position to the top of the first page
  @y = absolute_top_margin
    # Get the ID of the page that was created during the instantiation
    # process.

  fill_color!   Color::RGB::Black
  stroke_color! Color::RGB::Black

  yield self if block_given?
end