Class: PDF::Reader::Filter
- Inherits:
-
Object
- Object
- PDF::Reader::Filter
- Defined in:
- lib/pdf/reader/filter.rb
Overview
Various parts of a PDF file can be passed through a filter before being stored to provide support for features like compression and encryption. This class is for decoding that content.
Currently only 1 filter type is supported. Hopefully support for others will be added in the future.
Instance Method Summary collapse
-
#filter(data) ⇒ Object
attempts to decode the specified data with the current filter.
-
#flate(data) ⇒ Object
Decode the specified data with the Zlib compression algorithm.
-
#initialize(name, options) ⇒ Filter
constructor
creates a new filter for decoding content.
Constructor Details
#initialize(name, options) ⇒ Filter
creates a new filter for decoding content
38 39 40 41 42 43 44 45 |
# File 'lib/pdf/reader/filter.rb', line 38 def initialize (name, ) @options = case name.to_sym when :FlateDecode then @filter = :flate #else raise UnsupportedFeatureError, "Unknown filter: #{name}" end end |
Instance Method Details
#filter(data) ⇒ Object
attempts to decode the specified data with the current filter
48 49 50 51 52 53 54 |
# File 'lib/pdf/reader/filter.rb', line 48 def filter (data) # leave the data untouched if we don't support the required filter return data if @filter.nil? # decode the data self.send(@filter, data) end |
#flate(data) ⇒ Object
Decode the specified data with the Zlib compression algorithm
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pdf/reader/filter.rb', line 57 def flate (data) begin Zlib::Inflate.new.inflate(data) rescue Zlib::DataError => e # by default, Ruby's Zlib assumes the data it's inflating # is RFC1951 deflated data, wrapped in a RFC1951 zlib container. # If that fails, then use an undocumented 'feature' to attempt to inflate # the data as a raw RFC1951 stream. # # See # - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/243545 # - http://www.gzip.org/zlib/zlib_faq.html#faq38 Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(data) end rescue Exception => e # Oops, there was a problem inflating the stream raise MalformedPDFError, "Error occured while inflating a compressed stream (#{e.class.to_s}: #{e.to_s})" end |