Module: PDF::Core

Included in:
Prawn::Document::Security
Defined in:
lib/pdf/core.rb,
lib/pdf/core/page.rb,
lib/pdf/core/text.rb,
lib/prawn/security.rb,
lib/pdf/core/stream.rb,
lib/pdf/core/filters.rb,
lib/pdf/core/outline.rb,
lib/pdf/core/name_tree.rb,
lib/pdf/core/reference.rb,
lib/pdf/core/pdf_object.rb,
lib/pdf/core/annotations.rb,
lib/pdf/core/byte_string.rb,
lib/pdf/core/filter_list.rb,
lib/pdf/core/destinations.rb,
lib/pdf/core/object_store.rb,
lib/pdf/core/page_geometry.rb,
lib/pdf/core/document_state.rb,
lib/pdf/core/graphics_state.rb,
lib/pdf/core/literal_string.rb

Defined Under Namespace

Modules: Annotations, Destinations, Errors, Filters, NameTree, PageGeometry, Text Classes: ByteString, DocumentState, FilterList, GraphicState, GraphicStateStack, LiteralString, ObjectStore, Outline, OutlineItem, OutlineRoot, Page, Reference, Stream

Class Method Summary collapse

Class 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.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# 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
        raise 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

.PdfObject(obj, in_content_stream = false) ⇒ Object

Serializes Ruby objects to their PDF equivalents. Most primitive objects will work as expected, but please note that Name objects are represented by Ruby Symbol objects and Dictionary objects are represented by Ruby hashes (keyed by symbols)

Examples:

   PdfObject(true)      #=> "true"
   PdfObject(false)     #=> "false"
   PdfObject(1.2124)    #=> "1.2124"
   PdfObject("foo bar") #=> "(foo bar)"
   PdfObject(:Symbol)   #=> "/Symbol"
   PdfObject(["foo",:bar, [1,2]]) #=> "[foo /bar [1 2]]"


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pdf/core/pdf_object.rb', line 40

def PdfObject(obj, in_content_stream = false)
  case(obj)
  when NilClass   then "null"
  when TrueClass  then "true"
  when FalseClass then "false"
  when Numeric
    if (str = String(obj)) =~ /e/i
      # scientific notation is not supported in PDF
      sprintf("%.16f", obj).gsub(/\.?0+\z/, "")
    else
      str
    end
  when Array
    "[" << obj.map { |e| PdfObject(e, in_content_stream) }.join(' ') << "]"
  when PDF::Core::LiteralString
    obj = obj.gsub(/[\\\n\r\t\b\f\(\)]/n) { |m| "\\#{m}" }
    "(#{obj})"
  when Time
    obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
    obj = obj.gsub(/[\\\n\r\t\b\f\(\)]/n) { |m| "\\#{m}" }
    "(#{obj})"
  when PDF::Core::ByteString
    "<" << obj.unpack("H*").first << ">"
  when String
    obj = utf8_to_utf16(obj) unless in_content_stream
    "<" << string_to_hex(obj) << ">"
   when Symbol
     "/" + obj.to_s.unpack("C*").map { |n|
      if n < 33 || n > 126 || [35,40,41,47,60,62].include?(n)
        "#" + n.to_s(16).upcase
      else
        [n].pack("C*")
      end
     }.join
  when ::Hash
    output = "<< "
    obj.each do |k,v|
      unless String === k || Symbol === k
        raise PDF::Core::Errors::FailedObjectConversion,
          "A PDF Dictionary must be keyed by names"
      end
      output << PdfObject(k.to_sym, in_content_stream) << " " <<
                PdfObject(v, in_content_stream) << "\n"
    end
    output << ">>"
  when PDF::Core::Reference
    obj.to_s
  when PDF::Core::NameTree::Node
    PdfObject(obj.to_hash)
  when PDF::Core::NameTree::Value
    PdfObject(obj.name) + " " + PdfObject(obj.value)
  when PDF::Core::OutlineRoot, PDF::Core::OutlineItem
    PdfObject(obj.to_hash)
  else
    raise PDF::Core::Errors::FailedObjectConversion,
      "This object cannot be serialized to PDF (#{obj.inspect})"
  end
end

.Reference(*args, &block) ⇒ Object

:nodoc:



99
100
101
# File 'lib/pdf/core/reference.rb', line 99

def Reference(*args, &block) #:nodoc:
  Reference.new(*args, &block)
end

.string_to_hex(str) ⇒ Object

encodes any string into a hex representation. The result is a string with only 0-9 and a-f characters. That result is valid ASCII so tag it as such to account for behaviour of different ruby VMs



22
23
24
# File 'lib/pdf/core/pdf_object.rb', line 22

def string_to_hex(str)
  str.unpack("H*").first.force_encoding(::Encoding::US_ASCII)
end

.utf8_to_utf16(str) ⇒ Object



15
16
17
# File 'lib/pdf/core/pdf_object.rb', line 15

def utf8_to_utf16(str)
  "\xFE\xFF".force_encoding(::Encoding::UTF_16BE) + str.encode(::Encoding::UTF_16BE)
end