Module: Prawn::Templates::ObjectStoreExtensions

Defined in:
lib/prawn/templates.rb

Instance Method Summary collapse

Instance Method Details

#import_page(input, page_num) ⇒ Object

imports all objects required to render a page from another PDF. The objects are added to the current object store, but NOT linked anywhere.

The object ID of the root Page object is returned, it’s up to the calling code to link that into the document structure somewhere. If this isn’t done the imported objects will just be removed when the store is compacted.

Imports nothing and returns nil if the requested page number doesn’t exist. page_num is 1 indexed, so 1 indicates the first page.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/prawn/templates.rb', line 108

def import_page(input, page_num)
  @loaded_objects = {}
  template_id = indexed_template(input, page_num)
  return template_id if template_id

  io = if input.respond_to?(:seek) && input.respond_to?(:read)
         input
       elsif File.file?(input.to_s)
         StringIO.new(File.binread(input.to_s))
       else
         raise ArgumentError, 'input must be an IO-like object or a ' \
         'filename'
       end

  hash = indexed_hash(input, io)
  ref = hash.page_references[page_num - 1]

  if ref.nil?
    nil
  else
    index_template(
      input, page_num,
      load_object_graph(hash, ref).identifier
    )
  end

rescue PDF::Reader::MalformedPDFError,
       PDF::Reader::InvalidObjectError => e
  msg = 'Error reading template file. If you are sure it\'s a valid PDF,'\
        " it may be a bug.\n#{e.message}"
  raise PDF::Core::Errors::TemplateError, msg
rescue PDF::Reader::UnsupportedFeatureError
  msg = 'Template file contains unsupported PDF features'
  raise PDF::Core::Errors::TemplateError, msg
end