Method: CombinePDF::Page_Methods#inject_page

Defined in:
lib/combine_pdf/page_methods.rb

#inject_page(obj, top = true) ⇒ Object

Raises:

  • (TypeError)


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/combine_pdf/page_methods.rb', line 56

def inject_page(obj, top = true)
  raise TypeError, "couldn't inject data, expecting a PDF page (Hash type)" unless obj.is_a?(Page_Methods)

  obj = obj.copy(should_secure?(obj)) # obj.copy(secure_injection)

  # following the reference chain and assigning a pointer to the correct Resouces object.
  # (assignments of Strings, Arrays and Hashes are pointers in Ruby, unless the .dup method is called)

  # setup references to avoid method calls.
  local_res = resources
  local_val = nil
  # # setup references to avoid method calls.
  # remote_res = obj.resources
  # remote_val = nil

  # add each of the new resources in the uncoming Page to the local resource Hash
  obj.resources.each do |key, new_val|
    # keep CombinePDF structural data intact.
    next if PDF::PRIVATE_HASH_KEYS.include?(key)
    # review
    if local_res[key].nil?
      # no local data, adopt data from incoming page
      local_res[key] = new_val
      # go back to looping, no need to parse the rest of the Ruby
      next
    elsif (local_val = actual_object(local_res[key])).is_a?(Hash) && (new_val = actual_object(new_val)).is_a?(Hash)
      # marge data with priority to the incoming page's data
      new_val.update local_val # make sure the old values are respected
      local_val.update new_val # transfer old and new values to the injected page
    end # Do nothing if array or anything else
  end

  # concat the Annots array? (what good are named links if the names are in the unaccessible Page Catalog?)
  # if obj[:Annots]
  #   if (local_val = actual_object(self[:Annots])).nil?
  #     self[:Annots] = obj[:Annots]
  #   elsif local_val.is_a?(Array) && (remote_val = actual_object(obj[:Annots])).is_a?(Array)
  #     local_val.concat remote_val
  #   end
  # end

  # set ProcSet to recommended value
  resources[:ProcSet] ||= [:PDF, :Text, :ImageB, :ImageC, :ImageI] # this was recommended by the ISO. 32000-1:2008

  if top # if this is a stamp (overlay)
    insert_content CONTENT_CONTAINER_START, 0
    insert_content CONTENT_CONTAINER_MIDDLE
    self[:Contents].concat obj[:Contents]
    insert_content CONTENT_CONTAINER_END
  else # if this was a watermark (underlay? would be lost if the page was scanned, as white might not be transparent)
    insert_content CONTENT_CONTAINER_MIDDLE, 0
    insert_content CONTENT_CONTAINER_START, 0
    self[:Contents].insert 1, *obj[:Contents]
    insert_content CONTENT_CONTAINER_END
  end
  init_contents

  self
end