Class: PDF::Core::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/core/page.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pdf/core/page.rb', line 5

def initialize(document, options = {})
  @document = document
  @margins = options[:margins] || {
    left: 36,
    right: 36,
    top: 36,
    bottom: 36
  }
  @crops = options[:crops] || ZERO_INDENTS
  @bleeds = options[:bleeds] || ZERO_INDENTS
  @trims = options[:trims] || ZERO_INDENTS
  @art_indents = options[:art_indents] || ZERO_INDENTS
  @stack = GraphicStateStack.new(options[:graphic_state])
  if options[:object_id]
    init_from_object(options)
  else
    init_new_page(options)
  end
end

Instance Method Details

#__dimensionsObject



62
# File 'lib/pdf/core/page.rb', line 62

alias __dimensions dimensions

#__init_from_objectObject



84
# File 'lib/pdf/core/page.rb', line 84

alias __init_from_object init_from_object

#__init_new_pageObject



101
# File 'lib/pdf/core/page.rb', line 101

alias __init_new_page init_new_page

#__initializeObject



4
# File 'lib/pdf/core/page.rb', line 4

alias __initialize initialize

#dimensionsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pdf/core/page.rb', line 63

def dimensions
  if imported_page?
    media_box = inherited_dictionary_value(:MediaBox)
    return media_box.data if media_box.is_a?(PDF::Core::Reference)
    return media_box
  end

  coords = PDF::Core::PageGeometry::SIZES[size] || size
  [0, 0] +
    case layout
    when :portrait
      coords
    when :landscape
      coords.reverse
    else
      raise PDF::Core::Errors::InvalidPageLayout,
        'Layout must be either :portrait or :landscape'
    end
end

#imported_page?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pdf/core/page.rb', line 57

def imported_page?
  @imported_page
end

#init_from_object(options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pdf/core/page.rb', line 86

def init_from_object(options)
  @dictionary = options[:object_id].to_i
  if options[:page_template]
    dictionary.data[:Parent] = document.state.store.pages
  end

  unless dictionary.data[:Contents].is_a?(Array) # content only on leafs
    @content = dictionary.data[:Contents].identifier
  end

  @stamp_stream = nil
  @stamp_dictionary = nil
  @imported_page = true
end

#init_new_page(options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pdf/core/page.rb', line 102

def init_new_page(options)
  @size = options[:size] || 'LETTER'
  @layout = options[:layout] || :portrait

  @stamp_stream = nil
  @stamp_dictionary = nil
  @imported_page = false

  @content = document.ref({})
  content << 'q' << "\n"
  @dictionary = document.ref(
    Type: :Page,
    Parent: document.state.store.pages,
    MediaBox: dimensions,
    CropBox: crop_box,
    BleedBox: bleed_box,
    TrimBox: trim_box,
    ArtBox: art_box,
    Contents: content
  )

  resources[:ProcSet] = %i[PDF Text ImageB ImageC ImageI]
end

#new_content_streamObject

As per the PDF spec, each page can have multiple content streams. This will add a fresh, empty content stream this the page, mainly for use in loading template files.



47
48
49
50
51
52
53
54
# File 'lib/pdf/core/page.rb', line 47

def new_content_stream
  return if in_stamp_stream?

  dictionary.data[:Contents] = Array(dictionary.data[:Contents])
  @content = document.ref({})
  dictionary.data[:Contents] << document.state.store[@content]
  document.open_graphics_state
end

#wrap_graphics_stateObject

Prepend a content stream containing ‘q’, and append a content stream containing ‘Q’. This ensures that prawn has a pristine graphics state before it starts adding content.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pdf/core/page.rb', line 29

def wrap_graphics_state
  dictionary.data[:Contents] = Array(dictionary.data[:Contents])

  # Save graphics context
  @content = document.ref({})
  dictionary.data[:Contents].unshift(document.state.store[@content])
  document.add_content 'q'

  # Restore graphics context
  @content = document.ref({})
  dictionary.data[:Contents] << document.state.store[@content]
  document.add_content 'Q'
end