Module: Prawn::Templates

Defined in:
lib/prawn/templates.rb

Defined Under Namespace

Modules: ObjectStoreExtensions

Instance Method Summary collapse

Instance Method Details

#initialize_first_page(options) ⇒ Object



16
17
18
19
20
21
# File 'lib/prawn/templates.rb', line 16

def initialize_first_page(options)
  return super unless options[:template]

  fresh_content_streams(options)
  go_to_page(1)
end

#merge_template_options(page_options, options) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/prawn/templates.rb', line 87

def merge_template_options(page_options, options)
  object_id = state.store.import_page(
    options[:template],
    options[:template_page] || 1
  )
  page_options.merge!(object_id: object_id, page_template: true)
end

#start_new_page(options = {}) ⇒ Object

FIXME: This is going to be terribly brittle because it copy-pastes the start_new_page method. But at least it should only run when templates are used.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/prawn/templates.rb', line 26

def start_new_page(options = {})
  return super unless options[:template]

  last_page = state.page
  if last_page
    last_page_size = last_page.size
    last_page_layout = last_page.layout
    last_page_margins = last_page.margins.dup
  end

  page_options = {
    size: options[:size] || last_page_size,
    layout: options[:layout] || last_page_layout,
    margins: last_page_margins
  }
  if last_page
    if last_page.graphic_state
      new_graphic_state = last_page.graphic_state.dup
    end

    # erase the color space so that it gets reset on new page for fussy
    # pdf-readers
    new_graphic_state.color_space = {} if new_graphic_state

    page_options[:graphic_state] = new_graphic_state
  end

  merge_template_options(page_options, options)

  state.page = PDF::Core::Page.new(self, page_options)

  apply_margin_options(options)
  generate_margin_box

  # Reset the bounding box if the new page has different size or layout
  if last_page && (last_page.size != state.page.size ||
                   last_page.layout != state.page.layout)
    @bounding_box = @margin_box
  end

  state.page.new_content_stream
  use_graphic_settings(true)
  forget_text_rendering_mode!

  unless options[:orphan]
    state.insert_page(state.page, @page_number)
    @page_number += 1

    if @background
      canvas do
        image(@background, scale: @background_scale, at: bounds.top_left)
      end
    end
    @y = @bounding_box.absolute_top

    float do
      state.on_page_create_action(self)
    end
  end
end