module PDF::Core

Public Instance Methods

EncryptedPdfObject(obj, key, id, gen, in_content_stream = false) click to toggle source

Like PdfObject, but returns an encrypted result if required. For direct objects, requires the object identifier and generation number from the indirect object referencing obj.

@private

# File lib/prawn/security.rb, line 211
def EncryptedPdfObject(obj, key, id, gen, in_content_stream = false)
  case obj
  when Array
    "[" << obj.map { |e|
      EncryptedPdfObject(e, key, id, gen, in_content_stream)
    }.join(' ') << "]"
  when LiteralString
    obj = ByteString.new(Prawn::Document::Security.encrypt_string(obj, key, id, gen)).gsub(/[\\n\(\)]/) { |m| "\\#{m}" }
    "(#{obj})"
  when Time
    obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
    obj = ByteString.new(Prawn::Document::Security.encrypt_string(obj, key, id, gen)).gsub(/[\\n\(\)]/) { |m| "\\#{m}" }
    "(#{obj})"
  when String
    PdfObject(
      ByteString.new(
        Prawn::Document::Security.encrypt_string(obj, key, id, gen)),
      in_content_stream)
  when ::Hash
    output = "<< "
    obj.each do |k, v|
      unless String === k || Symbol === k
        fail PDF::Core::Errors::FailedObjectConversion,
             "A PDF Dictionary must be keyed by names"
      end
      output << PdfObject(k.to_sym, in_content_stream) << " " << EncryptedPdfObject(v, key, id, gen, in_content_stream) << "\n"
    end
    output << ">>"
  when NameTree::Value
    PdfObject(obj.name) + " " +
      EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
  when PDF::Core::OutlineRoot, PDF::Core::OutlineItem
    EncryptedPdfObject(obj.to_hash, key, id, gen, in_content_stream)
  else # delegate back to PdfObject
    PdfObject(obj, in_content_stream)
  end
end