Module: Pdfrb::Source::TrailerReader

Defined in:
lib/pdfrb/source/trailer_reader.rb

Overview

Locates startxref near EOF and returns its offset. Per s7.5.5, the last startxref keyword in the file is authoritative.

Constant Summary collapse

SCAN_BYTES =
1024

Class Method Summary collapse

Class Method Details

.startxref_offset(io) ⇒ Object

Returns the byte offset of the xref section (the value of the last startxref keyword in the file), or nil if not found.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pdfrb/source/trailer_reader.rb', line 14

def startxref_offset(io)
  size = io.size
  back = [SCAN_BYTES, size].min
  io.seek(size - back, IO::SEEK_SET)
  tail = io.read(back)
  return nil unless tail

  idx = tail.rindex("startxref")
  return nil unless idx

  rest = tail[(idx + "startxref".length)..]
  m = rest.match(/(\d+)/)
  return nil unless m

  m[1].to_i
end