module PDF::Core

Public Instance Methods

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

Like pdf_object, 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 221
def encrypted_pdf_object(obj, key, id, gen, in_content_stream = false)
  case obj
  when Array
    array_content = obj.map do |e|
      encrypted_pdf_object(e, key, id, gen, in_content_stream)
    end.join(' ')
    "[#{array_content}]"
  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
    pdf_object(
      ByteString.new(
        Prawn::Document::Security.encrypt_string(obj, key, id, gen)
      ),
      in_content_stream
    )
  when ::Hash
    hash_content = obj.map do |k, v|
      unless k.is_a?(String) || k.is_a?(Symbol)
        raise PDF::Core::Errors::FailedObjectConversion,
          'A PDF Dictionary must be keyed by names'
      end
      "#{pdf_object(k.to_sym, in_content_stream)} #{encrypted_pdf_object(v, key, id, gen, in_content_stream)}\n"
    end.join('')
    "<< #{hash_content}>>"
  when NameTree::Value
    "#{pdf_object(obj.name)} #{encrypted_pdf_object(obj.value, key, id, gen, in_content_stream)}"
  when PDF::Core::OutlineRoot, PDF::Core::OutlineItem
    encrypted_pdf_object(obj.to_hash, key, id, gen, in_content_stream)
  else # delegate back to pdf_object
    pdf_object(obj, in_content_stream)
  end
end