Class: Origami::Trailer

Inherits:
Object
  • Object
show all
Includes:
StandardObject
Defined in:
lib/origami/trailer.rb,
lib/origami/obfuscation.rb

Overview

Class representing a PDF file Trailer.

Constant Summary collapse

TOKENS =

:nodoc:

%w{ trailer %%EOF }
XREF_TOKEN =

:nodoc:

"startxref"
@@regexp_open =
Regexp.new(WHITESPACES + TOKENS.first + WHITESPACES)
@@regexp_xref =
Regexp.new(WHITESPACES + XREF_TOKEN + WHITESPACES + "(?<startxref>\\d+)")
@@regexp_close =
Regexp.new(WHITESPACES + TOKENS.last + WHITESPACES)

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StandardObject

included, #pre_build, #version_required

Constructor Details

#initialize(startxref = 0, dictionary = {}) ⇒ Trailer

Creates a new Trailer.

startxref

The file offset to the XRef::Section.

dictionary

A hash of attributes to set in the Trailer Dictionary.



113
114
115
# File 'lib/origami/trailer.rb', line 113

def initialize(startxref = 0, dictionary = {})
    @startxref, self.dictionary = startxref, dictionary && Dictionary.new(dictionary)
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



98
99
100
# File 'lib/origami/trailer.rb', line 98

def dictionary
  @dictionary
end

#documentObject

Returns the value of attribute document.



96
97
98
# File 'lib/origami/trailer.rb', line 96

def document
  @document
end

#startxrefObject

Returns the value of attribute startxref.



97
98
99
# File 'lib/origami/trailer.rb', line 97

def startxref
  @startxref
end

Class Method Details

.parse(stream, parser = nil) ⇒ Object

:nodoc:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/origami/trailer.rb', line 117

def self.parse(stream, parser = nil) #:nodoc:

    if stream.skip(@@regexp_open)
        dictionary = Dictionary.parse(stream, parser)
    else
        dictionary = nil
    end

    if not stream.scan(@@regexp_xref)
        raise InvalidTrailerError, "Cannot get startxref value"
    end

    startxref = stream['startxref'].to_i

    if not stream.scan(@@regexp_close)
        parser.warn("No %%EOF token found") if parser
    end

    Trailer.new(startxref, dictionary)
end

Instance Method Details

#[](key) ⇒ Object



138
139
140
# File 'lib/origami/trailer.rb', line 138

def [](key)
    @dictionary[key] if dictionary?
end

#[]=(key, val) ⇒ Object



142
143
144
145
# File 'lib/origami/trailer.rb', line 142

def []=(key,val)
    self.dictionary = Dictionary.new unless dictionary?
    @dictionary[key] = val
end

#dictionary?Boolean

Returns:



152
153
154
# File 'lib/origami/trailer.rb', line 152

def dictionary?
    not @dictionary.nil?
end

#to_obfuscated_strObject



233
234
235
236
237
238
239
240
241
242
# File 'lib/origami/obfuscation.rb', line 233

def to_obfuscated_str
    content = ""
    if self.dictionary?
        content << TOKENS.first << EOL << @dictionary.to_obfuscated_str << EOL
    end

    content << XREF_TOKEN << EOL << @startxref.to_s << EOL << TOKENS.last << EOL

    content
end

#to_sObject

Outputs self into PDF code.



159
160
161
162
163
164
165
166
167
168
# File 'lib/origami/trailer.rb', line 159

def to_s
    content = ""
    if self.dictionary?
        content << TOKENS.first << EOL << @dictionary.to_s << EOL
    end

    content << XREF_TOKEN << EOL << @startxref.to_s << EOL << TOKENS.last << EOL

    content
end