Class: PDF::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader.rb,
lib/pdf/reader.rb,
lib/pdf/reader/cmap.rb,
lib/pdf/reader/font.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/content.rb,
lib/pdf/reader/explore.rb,
lib/pdf/reader/encoding.rb,
lib/pdf/reader/reference.rb,
lib/pdf/reader/text_receiver.rb,
lib/pdf/reader/print_receiver.rb,
lib/pdf/reader/register_receiver.rb

Overview

Copyright © 2006 Peter J Jones ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Buffer, CMap, Content, Encoding, Error, Explore, Filter, Font, InvalidObjectError, MalformedPDFError, Parser, PrintReceiver, Reference, RegisterReceiver, Stream, TextReceiver, Token, UnsupportedFeatureError, XRef

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

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



73
74
75
76
77
# File 'lib/pdf/reader.rb', line 73

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

.object_file(name, id, gen) ⇒ Object



86
87
88
89
90
# File 'lib/pdf/reader.rb', line 86

def self.object_file(name, id, gen)
  File.open(name,"rb") do |f|
    new.object(f, id, gen)
  end
end

.object_string(name, id, gen) ⇒ Object



92
93
94
95
96
# File 'lib/pdf/reader.rb', line 92

def self.object_string(name, id, gen)
  StringIO.open(str) do |s|
    new.object(s, id, gen)
  end
end

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

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



80
81
82
83
84
# File 'lib/pdf/reader.rb', line 80

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

Instance Method Details

#object(io, id, gen) ⇒ Object

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



139
140
141
142
143
144
145
# File 'lib/pdf/reader.rb', line 139

def object (io, id, gen)
  @buffer   = Buffer.new(io)
  @xref     = XRef.new(@buffer)
  @xref.load

  @xref.object(Reference.new(id, gen))
end

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

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



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

def parse (io, receiver, opts = {})
  @buffer   = Buffer.new(io)
  @xref     = XRef.new(@buffer)
  @parser   = Parser.new(@buffer, @xref)
  @content  = (receiver == Explore ? Explore : Content).new(receiver, @xref)

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

  trailer = @xref.load
  raise PDF::Reader::UnsupportedFeatureError, 'PDF::Reader cannot read encrypted PDF files' if trailer[:Encrypt]
  @content.(@xref.object(trailer[:Root]), @xref.object(trailer[:Info])) if options[:metadata]
  @content.document(@xref.object(trailer[:Root])) if options[:pages]
  self
end