Class: PDF::Reader::ObjectHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pdf/reader/object_hash.rb

Overview

Provides low level access to the objects in a PDF file via a hash-like object.

A PDF file can be viewed as a large hash map. It is a series of objects stored at precise byte offsets, and a table that maps object IDs to byte offsets. Given an object ID, looking up an object is an O(1) operation.

Each PDF object can be mapped to a ruby object, so by passing an object ID to the [] method, a ruby representation of that object will be retrieved.

The class behaves much like a standard Ruby hash, including the use of the Enumerable mixin. The key difference is no []= method - the hash is read only.

Basic Usage

h = PDF::Reader::ObjectHash.new("somefile.pdf")
h[1]
=> 3469

h[PDF::Reader::Reference.new(1,0)]
=> 3469

Direct Known Subclasses

Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, opts = {}) ⇒ ObjectHash

Creates a new ObjectHash object. Input can be a string with a valid filename or an IO-like object.

Valid options:

:password - the user password to decrypt the source PDF


42
43
44
45
46
47
48
49
50
# File 'lib/pdf/reader/object_hash.rb', line 42

def initialize(input, opts = {})
  @io          = extract_io_from(input)
  @xref        = PDF::Reader::XRef.new(@io)
  @pdf_version = read_version
  @trailer     = @xref.trailer
  @cache       = opts[:cache] || PDF::Reader::ObjectCache.new
  @sec_handler = NullSecurityHandler.new
  @sec_handler = build_security_handler(opts)
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



31
32
33
# File 'lib/pdf/reader/object_hash.rb', line 31

def default
  @default
end

#pdf_versionObject (readonly)

Returns the value of attribute pdf_version.



32
33
34
# File 'lib/pdf/reader/object_hash.rb', line 32

def pdf_version
  @pdf_version
end

#sec_handlerObject (readonly)

Returns the value of attribute sec_handler.



33
34
35
# File 'lib/pdf/reader/object_hash.rb', line 33

def sec_handler
  @sec_handler
end

#trailerObject (readonly)

Returns the value of attribute trailer.



32
33
34
# File 'lib/pdf/reader/object_hash.rb', line 32

def trailer
  @trailer
end

Instance Method Details

#[](key) ⇒ Object

Access an object from the PDF. key can be an int or a PDF::Reader::Reference object.

If an int is used, the object with that ID and a generation number of 0 will be returned.

If a PDF::Reader::Reference object is used the exact ID and generation number can be specified.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pdf/reader/object_hash.rb', line 73

def [](key)
  return default if key.to_i <= 0

  unless key.is_a?(PDF::Reader::Reference)
    key = PDF::Reader::Reference.new(key.to_i, 0)
  end

  if @cache.has_key?(key)
    @cache[key]
  elsif xref[key].is_a?(Integer)
    buf = new_buffer(xref[key])
    @cache[key] = decrypt(key, Parser.new(buf, self).object(key.id, key.gen))
  elsif xref[key].is_a?(PDF::Reader::Reference)
    container_key = xref[key]
    object_streams[container_key] ||= PDF::Reader::ObjectStream.new(object(container_key))
    @cache[key] = object_streams[container_key][key.id]
  end
rescue InvalidObjectError
  return default
end

#deref!(key) ⇒ Object

Recursively dereferences the object refered to be key. If key is not a PDF::Reader::Reference, the key is returned unchanged.



105
106
107
# File 'lib/pdf/reader/object_hash.rb', line 105

def deref!(key)
  deref_internal!(key, {})
end

#each(&block) ⇒ Object Also known as: each_pair

iterate over each key, value. Just like a ruby hash.



134
135
136
137
138
# File 'lib/pdf/reader/object_hash.rb', line 134

def each(&block)
  @xref.each do |ref|
    yield ref, self[ref]
  end
end

#each_key(&block) ⇒ Object

iterate over each key. Just like a ruby hash.



143
144
145
146
147
# File 'lib/pdf/reader/object_hash.rb', line 143

def each_key(&block)
  each do |id, obj|
    yield id
  end
end

#each_value(&block) ⇒ Object

iterate over each value. Just like a ruby hash.



151
152
153
154
155
# File 'lib/pdf/reader/object_hash.rb', line 151

def each_value(&block)
  each do |id, obj|
    yield obj
  end
end

#empty?Boolean

return true if there are no objects in this file

Returns:

  • (Boolean)


166
167
168
# File 'lib/pdf/reader/object_hash.rb', line 166

def empty?
  size == 0 ? true : false
end

#encrypted?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/pdf/reader/object_hash.rb', line 246

def encrypted?
  trailer.has_key?(:Encrypt)
end

#fetch(key, local_default = nil) ⇒ Object

Access an object from the PDF. key can be an int or a PDF::Reader::Reference object.

If an int is used, the object with that ID and a generation number of 0 will be returned.

If a PDF::Reader::Reference object is used the exact ID and generation number can be specified.

local_default is the object that will be returned if the requested key doesn’t exist.



121
122
123
124
125
126
127
128
129
130
# File 'lib/pdf/reader/object_hash.rb', line 121

def fetch(key, local_default = nil)
  obj = self[key]
  if obj
    return obj
  elsif local_default
    return local_default
  else
    raise IndexError, "#{key} is invalid" if key.to_i <= 0
  end
end

#has_key?(check_key) ⇒ Boolean Also known as: include?, key?, member?, value?

return true if the specified key exists in the file. key can be an int or a PDF::Reader::Reference

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pdf/reader/object_hash.rb', line 173

def has_key?(check_key)
  # TODO update from O(n) to O(1)
  each_key do |key|
    if check_key.kind_of?(PDF::Reader::Reference)
      return true if check_key == key
    else
      return true if check_key.to_i == key.id
    end
  end
  return false
end

#has_value?(value) ⇒ Boolean

return true if the specifiedvalue exists in the file

Returns:

  • (Boolean)


190
191
192
193
194
195
196
# File 'lib/pdf/reader/object_hash.rb', line 190

def has_value?(value)
  # TODO update from O(n) to O(1)
  each_value do |obj|
    return true if obj == value
  end
  return false
end

#keysObject

return an array of all keys in the file



205
206
207
208
209
# File 'lib/pdf/reader/object_hash.rb', line 205

def keys
  ret = []
  each_key { |k| ret << k }
  ret
end

#obj_type(ref) ⇒ Object

returns the type of object a ref points to



53
54
55
56
57
# File 'lib/pdf/reader/object_hash.rb', line 53

def obj_type(ref)
  self[ref].class.to_s.to_sym
rescue
  nil
end

#object(key) ⇒ Object Also known as: deref

If key is a PDF::Reader::Reference object, lookup the corresponding object in the PDF and return it. Otherwise return key untouched.



97
98
99
# File 'lib/pdf/reader/object_hash.rb', line 97

def object(key)
  key.is_a?(PDF::Reader::Reference) ? self[key] : key
end

#page_referencesObject

returns an array of PDF::Reader::References. Each reference in the array points a Page object, one for each page in the PDF. The first reference is page 1, second reference is page 2, etc.

Useful for apps that want to extract data from specific pages.



241
242
243
244
# File 'lib/pdf/reader/object_hash.rb', line 241

def page_references
  root  = fetch(trailer[:Root])
  @page_references ||= get_page_objects(root[:Pages]).flatten
end

#sec_handler?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/pdf/reader/object_hash.rb', line 250

def sec_handler?
  !!sec_handler
end

#sizeObject Also known as: length

return the number of objects in the file. An object with multiple generations is counted once.



159
160
161
# File 'lib/pdf/reader/object_hash.rb', line 159

def size
  xref.size
end

#stream?(ref) ⇒ Boolean

returns true if the supplied references points to an object with a stream

Returns:

  • (Boolean)


60
61
62
# File 'lib/pdf/reader/object_hash.rb', line 60

def stream?(ref)
  self.has_key?(ref) && self[ref].is_a?(PDF::Reader::Stream)
end

#to_aObject

return an array of arrays. Each sub array contains a key/value pair.



227
228
229
230
231
232
233
# File 'lib/pdf/reader/object_hash.rb', line 227

def to_a
  ret = []
  each do |id, obj|
    ret << [id, obj]
  end
  ret
end

#to_sObject



199
200
201
# File 'lib/pdf/reader/object_hash.rb', line 199

def to_s
  "<PDF::Reader::ObjectHash size: #{self.size}>"
end

#valuesObject

return an array of all values in the file



213
214
215
216
217
# File 'lib/pdf/reader/object_hash.rb', line 213

def values
  ret = []
  each_value { |v| ret << v }
  ret
end

#values_at(*ids) ⇒ Object

return an array of all values from the specified keys



221
222
223
# File 'lib/pdf/reader/object_hash.rb', line 221

def values_at(*ids)
  ids.map { |id| self[id] }
end