Module: Prawn

Defined in:
lib/prawn/security.rb

Defined Under Namespace

Classes: Document, Reference

Instance Method Summary collapse

Instance Method Details

#EncryptedPdfObject(obj, key, id, gen, in_content_stream = false) ⇒ Object

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.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/prawn/security.rb', line 198

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 Prawn::LiteralString
    # FIXME: encrypted?
    obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
    "(#{obj})"
  when Time
    # FIXME: encrypted?
    obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
    obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
    "(#{obj})"
  when String
    PdfObject(
      ByteString.new(Document::Security.encrypt_string(obj, key, id, gen)),
      in_content_stream)
  when Hash
    output = "<< "
    obj.each do |k,v|
      unless String === k || Symbol === k
        raise Prawn::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 Prawn::NameTree::Value
    PdfObject(obj.name) + " " +
      EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
  else # delegate back to PdfObject
    PdfObject(obj, in_content_stream)
  end
end