Class: Origami::PDF::LazyParser

Inherits:
Parser show all
Defined in:
lib/origami/parsers/pdf/lazy.rb

Overview

Create a new PDF lazy Parser.

Constant Summary

Constants inherited from Origami::Parser

Origami::Parser::VERBOSE_DEBUG, Origami::Parser::VERBOSE_INFO, Origami::Parser::VERBOSE_QUIET, Origami::Parser::VERBOSE_TRACE

Instance Attribute Summary

Attributes inherited from Origami::Parser

#options

Instance Method Summary collapse

Methods inherited from Parser

#initialize

Methods inherited from Origami::Parser

#debug, #defer_type_cast, #error, #info, init_scanner, #initialize, #parse_object, #parse_trailer, #parse_xreftable, #pos, #pos=, #target_data, #target_filename, #target_filesize, #trace, #warn

Constructor Details

This class inherits a constructor from Origami::PDF::Parser

Instance Method Details

#parse(stream) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/origami/parsers/pdf/lazy.rb', line 29

def parse(stream)
  super

  pdf = parse_initialize
  revisions = []

  # Locate the last xref offset at the end of the file.
  xref_offset = locate_last_xref_offset

  while xref_offset && (xref_offset != 0)

    # Create a new revision based on the xref section offset.
    revision = parse_revision(pdf, xref_offset)

    # Locate the previous xref section.
    xref_offset = if revision.xrefstm? && revision.xrefstm[:Prev].is_a?(Integer)
      revision.xrefstm[:Prev].to_i
    elsif revision.trailer[:Prev].is_a?(Integer)
      revision.trailer[:Prev].to_i
    end

    # Prepend the revision.
    revisions.unshift(revision)
  end

  pdf.revisions.clear
  revisions.each do |rev|
    pdf.revisions.push(rev)
    pdf.insert(rev.xrefstm) if rev.xrefstm?
  end

  parse_finalize(pdf)

  pdf
end