class HexaPDF::Type::Trailer

Represents the PDF file trailer.

The file trailer is the starting point for the PDF's object tree. It links to the Catalog (the main PDF document structure) and the Info dictionary and holds the information necessary for encrypting the PDF document.

Since a PDF document can contain multiple revisions, each revision needs to have its own file trailer (see HexaPDF::Revision#trailer).

When cross-reference streams are used the information that is normally stored in the file trailer is stored directly in the cross-reference stream dictionary. However, a HexaPDF::Revision object's trailer dictionary is always of this type. Only when a cross-reference stream is written is the trailer integrated into the stream's dictionary.

See: PDF1.7 s7.5.5, s14.4; XRefStream

Public Instance Methods

catalog() click to toggle source

Returns the document's Catalog (see Type::Catalog), creating it if needed.

# File lib/hexapdf/type/trailer.rb, line 70
def catalog
  self[:Root] ||= document.add({Type: :Catalog})
end
info() click to toggle source

Returns the document's information dictionary (see Type::Info), creating it if needed.

# File lib/hexapdf/type/trailer.rb, line 75
def info
  self[:Info] ||= document.add({}, type: :XXInfo)
end
set_random_id() click to toggle source

Sets the /ID field to an array of two copies of a random string and returns this array.

See: PDF1.7 14.4

# File lib/hexapdf/type/trailer.rb, line 82
def set_random_id
  value[:ID] = [Digest::MD5.digest(rand.to_s)] * 2
end
update_id() click to toggle source

Updates the second part of the /ID field (the first part should always be the same for a PDF file, the second part should change with each write).

# File lib/hexapdf/type/trailer.rb, line 88
def update_id
  if !self[:ID].kind_of?(PDFArray)
    set_random_id
  else
    value[:ID][1] = Digest::MD5.digest(rand.to_s)
  end
end

Private Instance Methods

perform_validation() { |msg, true| ... } click to toggle source

Validates the trailer.

Calls superclass method HexaPDF::Dictionary#perform_validation
# File lib/hexapdf/type/trailer.rb, line 99
def perform_validation(&block)
  super
  unless value[:ID]
    msg = if value[:Encrypt]
            "ID field is required when an Encrypt dictionary is present"
          else
            "ID field should always be set"
          end
    yield(msg, true)
    set_random_id
  end

  unless value[:Root]
    yield("A PDF document must have a Catalog dictionary", true)
    catalog.validate(&block)
  end

  if value[:Encrypt] && (!document.security_handler ||
                         !document.security_handler.encryption_key_valid?)
    yield("Encryption key doesn't match encryption dictionary", false)
  end
end