Class: PDF::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader.rb,
lib/pdf/reader/lzw.rb,
lib/pdf/reader/cmap.rb,
lib/pdf/reader/font.rb,
lib/pdf/reader/page.rb,
lib/pdf/reader/xref.rb,
lib/pdf/reader/error.rb,
lib/pdf/reader/token.rb,
lib/pdf/reader/buffer.rb,
lib/pdf/reader/filter.rb,
lib/pdf/reader/parser.rb,
lib/pdf/reader/stream.rb,
lib/pdf/reader/encoding.rb,
lib/pdf/reader/text_run.rb,
lib/pdf/reader/reference.rb,
lib/pdf/reader/cid_widths.rb,
lib/pdf/reader/filter/lzw.rb,
lib/pdf/reader/glyph_hash.rb,
lib/pdf/reader/page_state.rb,
lib/pdf/reader/filter/null.rb,
lib/pdf/reader/object_hash.rb,
lib/pdf/reader/page_layout.rb,
lib/pdf/reader/filter/flate.rb,
lib/pdf/reader/form_xobject.rb,
lib/pdf/reader/object_cache.rb,
lib/pdf/reader/object_stream.rb,
lib/pdf/reader/text_receiver.rb,
lib/pdf/reader/filter/ascii85.rb,
lib/pdf/reader/pages_strategy.rb,
lib/pdf/reader/print_receiver.rb,
lib/pdf/reader/font_descriptor.rb,
lib/pdf/reader/filter/ascii_hex.rb,
lib/pdf/reader/filter/depredict.rb,
lib/pdf/reader/resource_methods.rb,
lib/pdf/reader/abstract_strategy.rb,
lib/pdf/reader/filter/run_length.rb,
lib/pdf/reader/metadata_strategy.rb,
lib/pdf/reader/register_receiver.rb,
lib/pdf/reader/page_text_receiver.rb,
lib/pdf/reader/synchronized_cache.rb,
lib/pdf/reader/transformation_matrix.rb,
lib/pdf/reader/standard_security_handler.rb,
lib/pdf/reader/width_calculator/composite.rb,
lib/pdf/reader/width_calculator/true_type.rb,
lib/pdf/reader/width_calculator/type_zero.rb,
lib/pdf/reader/width_calculator/type_one_or_three.rb,
lib/pdf/reader/width_calculator/built_in.rb

Overview

Copyright © 2010 James Healy ([email protected])

Defined Under Namespace

Modules: Filter, ResourceMethods, WidthCalculator Classes: AbstractStrategy, Buffer, CMap, CidWidths, Encoding, EncryptedPDFError, Error, Font, FontDescriptor, FormXObject, GlyphHash, InvalidObjectError, LZW, MalformedPDFError, MetadataStrategy, ObjectCache, ObjectHash, ObjectStream, Page, PageLayout, PageState, PageTextReceiver, PagesStrategy, Parser, PrintReceiver, Reference, RegisterReceiver, StandardSecurityHandler, Stream, SynchronizedCache, TextReceiver, TextRun, Token, TransformationMatrix, UnsupportedFeatureError, XRef

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, opts = {}) ⇒ Reader

creates a new document reader for the provided PDF.

input can be an IO-ish object (StringIO, File, etc) containing a PDF or a filename

reader = PDF::Reader.new("somefile.pdf")

File.open("somefile.pdf","rb") do |file|
  reader = PDF::Reader.new(file)
end

If the source file is encrypted you can provide a password for decrypting

reader = PDF::Reader.new("somefile.pdf", :password => "apples")


113
114
115
116
117
118
119
# File 'lib/pdf/reader.rb', line 113

def initialize(input = nil, opts = {})
  if input # support the deprecated Reader API
    @cache   = PDF::Reader::ObjectCache.new
    opts.merge!(:cache => @cache)
    @objects = PDF::Reader::ObjectHash.new(input, opts)
  end
end

Instance Attribute Details

#objectsObject (readonly)

lowlevel hash-like access to all objects in the underlying PDF



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

def objects
  @objects
end

Class Method Details

.file(name, receivers, opts = {}) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Parse the file with the given name, sending events to the given receiver.



169
170
171
172
173
# File 'lib/pdf/reader.rb', line 169

def self.file(name, receivers, opts = {})
  File.open(name,"rb") do |f|
    new.parse(f, receivers, opts)
  end
end

.object_file(name, id, gen = 0) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Parse the file with the given name, returning an unmarshalled ruby version of represents the requested pdf object



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

def self.object_file(name, id, gen = 0)
  File.open(name,"rb") { |f|
    new.object(f, id.to_i, gen.to_i)
  }
end

.object_string(str, id, gen = 0) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Parse the given string, returning an unmarshalled ruby version of represents the requested pdf object



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

def self.object_string(str, id, gen = 0)
  StringIO.open(str) { |s|
    new.object(s, id.to_i, gen.to_i)
  }
end

.open(input, opts = {}) {|PDF::Reader.new(input, opts)| ... } ⇒ Object

syntactic sugar for opening a PDF file. Accepts the same arguments as new().

PDF::Reader.open("somefile.pdf") do |reader|
  puts reader.pdf_version
end

or

PDF::Reader.open("somefile.pdf", :password => "apples") do |reader|
  puts reader.pdf_version
end

Yields:



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

def self.open(input, opts = {}, &block)
  yield PDF::Reader.new(input, opts)
end

.string(str, receivers, opts = {}) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Parse the given string, sending events to the given receiver.



180
181
182
183
184
# File 'lib/pdf/reader.rb', line 180

def self.string(str, receivers, opts = {})
  StringIO.open(str) do |s|
    new.parse(s, receivers, opts)
  end
end

Instance Method Details

#infoObject



121
122
123
124
# File 'lib/pdf/reader.rb', line 121

def info
  dict = @objects.deref(@objects.trailer[:Info])
  doc_strings_to_utf8(dict)
end

#metadataObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/pdf/reader.rb', line 126

def 
  stream = @objects.deref(root[:Metadata])
  if stream.nil?
    nil
  else
    xml = stream.unfiltered_data
    xml.force_encoding("utf-8") if xml.respond_to?(:force_encoding)
    xml
  end
end

#object(io, id, gen) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Given an IO object that contains PDF data, return the contents of a single object



274
275
276
277
278
# File 'lib/pdf/reader.rb', line 274

def object (io, id, gen)
  @objects = ObjectHash.new(io)

  @objects.deref(Reference.new(id, gen))
end

#page(num) ⇒ Object

returns a single PDF::Reader::Page for the specified page. Use this instead of pages method when you need to access just a single page

reader = PDF::Reader.new("somefile.pdf")
page   = reader.page(10)

puts page.text

See the docs for PDF::Reader::Page to read more about the methods available on each page



242
243
244
245
246
247
248
# File 'lib/pdf/reader.rb', line 242

def page(num)
  num = num.to_i
  if num < 1 || num > self.page_count
    raise ArgumentError, "valid pages are 1 .. #{self.page_count}"
  end
  PDF::Reader::Page.new(@objects, num, :cache => @cache)
end

#page_countObject



137
138
139
140
# File 'lib/pdf/reader.rb', line 137

def page_count
  pages = @objects.deref(root[:Pages])
  @page_count ||= @objects.deref(pages[:Count])
end

#pagesObject

returns an array of PDF::Reader::Page objects, one for each page in the source PDF.

reader = PDF::Reader.new("somefile.pdf")

reader.pages.each do |page|
  puts page.fonts
  puts page.images
  puts page.text
end

See the docs for PDF::Reader::Page to read more about the methods available on each page



224
225
226
227
228
# File 'lib/pdf/reader.rb', line 224

def pages
  (1..self.page_count).map { |num|
    PDF::Reader::Page.new(@objects, num, :cache => @cache)
  }
end

#parse(io, receivers, opts = {}) ⇒ Object

DEPRECATED: this method was deprecated in version 1.0.0 and will

eventually be removed

Given an IO object that contains PDF data, parse it.



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/pdf/reader.rb', line 256

def parse(io, receivers, opts = {})
  ohash    = ObjectHash.new(io)

  options = {:pages => true, :raw_text => false, :metadata => true}
  options.merge!(opts)

  strategies.each do |s|
    s.new(ohash, receivers, options).process
  end

  self
end

#pdf_versionObject



142
143
144
# File 'lib/pdf/reader.rb', line 142

def pdf_version
  @objects.pdf_version
end