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 + "(\\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

#do_type_check, #has_field?, included, #pdf_version_required, #pre_build, #set_default_value, #set_default_values

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.



117
118
119
120
# File 'lib/origami/trailer.rb', line 117

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

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



102
103
104
# File 'lib/origami/trailer.rb', line 102

def dictionary
  @dictionary
end

#pdfObject

Returns the value of attribute pdf.



100
101
102
# File 'lib/origami/trailer.rb', line 100

def pdf
  @pdf
end

#startxrefObject

Returns the value of attribute startxref.



101
102
103
# File 'lib/origami/trailer.rb', line 101

def startxref
  @startxref
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/origami/trailer.rb', line 122

def self.parse(stream) #:nodoc:
 
  if stream.skip(@@regexp_open)
    dictionary = Dictionary.parse(stream)
  else 
    dictionary = nil
  end
  
  if not stream.scan(@@regexp_xref)
    #raise InvalidTrailerError, "Cannot get startxref value"
  end

  startxref = (stream[3] && stream[3].to_i)

  if not stream.scan(@@regexp_close)
    #raise InvalidTrailerError, "No %%EOF token found"
  end
    
  Trailer.new(startxref, dictionary && dictionary.to_h)
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, val) ⇒ Object



147
148
149
# File 'lib/origami/trailer.rb', line 147

def []=(key,val)
  @dictionary[key] = val
end

#has_dictionary?Boolean

Returns:



156
157
158
# File 'lib/origami/trailer.rb', line 156

def has_dictionary?
  not @dictionary.nil?
end

#to_obfuscated_strObject



220
221
222
223
224
225
226
227
228
229
# File 'lib/origami/obfuscation.rb', line 220

def to_obfuscated_str
  content = ""
  if self.has_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.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/origami/trailer.rb', line 163

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