Class: PDF::Reader::ObjectHash
- Inherits:
-
Object
- Object
- PDF::Reader::ObjectHash
- 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 an exact 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
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#pdf_version ⇒ Object
readonly
Returns the value of attribute pdf_version.
-
#trailer ⇒ Object
readonly
Returns the value of attribute trailer.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access an object from the PDF.
-
#each(&block) ⇒ Object
(also: #each_pair)
iterate over each key, value.
-
#each_key(&block) ⇒ Object
iterate over each key.
-
#each_value(&block) ⇒ Object
iterate over each value.
-
#empty? ⇒ Boolean
return true if there are no objects in this file.
-
#fetch(key, local_default = nil) ⇒ Object
Access an object from the PDF.
-
#has_key?(check_key) ⇒ Boolean
(also: #include?, #key?, #member?, #value?)
return true if the specified key exists in the file.
-
#has_value?(value) ⇒ Boolean
return true if the specifiedvalue exists in the file.
-
#initialize(input) ⇒ ObjectHash
constructor
Creates a new ObjectHash object.
-
#keys ⇒ Object
return an array of all keys in the file.
-
#obj_type(ref) ⇒ Object
returns the type of object a ref points to.
-
#object(key) ⇒ Object
If key is a PDF::Reader::Reference object, lookup the corresponding object in the PDF and return it.
-
#page_references ⇒ Object
returns an array of PDF::Reader::References.
-
#size ⇒ Object
(also: #length)
return the number of objects in the file.
-
#stream?(ref) ⇒ Boolean
returns true if the supplied references points to an object with a stream.
-
#to_a ⇒ Object
return an array of arrays.
- #to_s ⇒ Object
-
#values ⇒ Object
return an array of all values in the file.
-
#values_at(*ids) ⇒ Object
return an array of all values from the specified keys.
Constructor Details
#initialize(input) ⇒ ObjectHash
Creates a new ObjectHash object. input can be a string with a valid filename, a string containing a PDF file, or an IO object.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pdf/reader/object_hash.rb', line 37 def initialize(input) if input.respond_to?(:seek) && input.respond_to?(:read) @io = input elsif File.file?(input.to_s) if File.respond_to?(:binread) input = File.binread(input.to_s) else input = File.read(input.to_s) end @io = StringIO.new(input) else raise ArgumentError, "input must be an IO-like object or a filename" end @pdf_version = read_version @xref = PDF::Reader::XRef.new(@io) @trailer = @xref.trailer end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
31 32 33 |
# File 'lib/pdf/reader/object_hash.rb', line 31 def default @default end |
#pdf_version ⇒ Object (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 |
#trailer ⇒ Object (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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pdf/reader/object_hash.rb', line 78 def [](key) return default if key.to_i <= 0 begin unless key.kind_of?(PDF::Reader::Reference) key = PDF::Reader::Reference.new(key.to_i, 0) end if xref[key].is_a?(Fixnum) buf = new_buffer(xref[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)) object_streams[container_key][key.id] end rescue InvalidObjectError return default end end |
#each(&block) ⇒ Object Also known as: each_pair
iterate over each key, value. Just like a ruby hash.
129 130 131 132 133 |
# File 'lib/pdf/reader/object_hash.rb', line 129 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.
138 139 140 141 142 |
# File 'lib/pdf/reader/object_hash.rb', line 138 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.
146 147 148 149 150 |
# File 'lib/pdf/reader/object_hash.rb', line 146 def each_value(&block) each do |id, obj| yield obj end end |
#empty? ⇒ Boolean
return true if there are no objects in this file
161 162 163 |
# File 'lib/pdf/reader/object_hash.rb', line 161 def empty? size == 0 ? true : false 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.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pdf/reader/object_hash.rb', line 116 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
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/pdf/reader/object_hash.rb', line 168 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
185 186 187 188 189 190 191 |
# File 'lib/pdf/reader/object_hash.rb', line 185 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 |
#keys ⇒ Object
return an array of all keys in the file
200 201 202 203 204 |
# File 'lib/pdf/reader/object_hash.rb', line 200 def keys ret = [] each_key { |k| ret << k } ret end |
#obj_type(ref) ⇒ Object
returns the type of object a ref points to
56 57 58 59 60 |
# File 'lib/pdf/reader/object_hash.rb', line 56 def obj_type(ref) self[ref].class.to_s.to_sym rescue nil end |
#object(key) ⇒ Object
If key is a PDF::Reader::Reference object, lookup the corresponding object in the PDF and return it. Otherwise return key untouched.
100 101 102 |
# File 'lib/pdf/reader/object_hash.rb', line 100 def object(key) key.is_a?(PDF::Reader::Reference) ? self[key] : key end |
#page_references ⇒ Object
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.
236 237 238 239 |
# File 'lib/pdf/reader/object_hash.rb', line 236 def page_references root = fetch(trailer[:Root]) @page_references ||= get_page_objects(root[:Pages]).flatten end |
#size ⇒ Object Also known as: length
return the number of objects in the file. An object with multiple generations is counted once.
154 155 156 |
# File 'lib/pdf/reader/object_hash.rb', line 154 def size xref.size end |
#stream?(ref) ⇒ Boolean
returns true if the supplied references points to an object with a stream
63 64 65 66 67 |
# File 'lib/pdf/reader/object_hash.rb', line 63 def stream?(ref) self[ref].class == PDF::Reader::Stream rescue false end |
#to_a ⇒ Object
return an array of arrays. Each sub array contains a key/value pair.
222 223 224 225 226 227 228 |
# File 'lib/pdf/reader/object_hash.rb', line 222 def to_a ret = [] each do |id, obj| ret << [id, obj] end ret end |
#to_s ⇒ Object
194 195 196 |
# File 'lib/pdf/reader/object_hash.rb', line 194 def to_s "<PDF::Reader::ObejctHash size: #{self.size}>" end |
#values ⇒ Object
return an array of all values in the file
208 209 210 211 212 |
# File 'lib/pdf/reader/object_hash.rb', line 208 def values ret = [] each_value { |v| ret << v } ret end |
#values_at(*ids) ⇒ Object
return an array of all values from the specified keys
216 217 218 |
# File 'lib/pdf/reader/object_hash.rb', line 216 def values_at(*ids) ids.map { |id| self[id] } end |