Module: Medivo::FdfGenerator

Defined in:
lib/pdf/medivo/fdf_generator.rb

Class Method Summary collapse

Class Method Details

.escape_data(value) ⇒ Object



32
33
34
# File 'lib/pdf/medivo/fdf_generator.rb', line 32

def self.escape_data(value)
  value.to_s.strip.gsub(/[\(]/, '\\(').gsub(/[\)]/, '\\)')
end

.file(info) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pdf/medivo/fdf_generator.rb', line 4

def self.file(info)
  PdfGenerator.tmp_pdf do |afile|
    afile.write "%FDF-1.2\x0d%\xe2\xe3\xcf\xd3\x0d\x0a"; # header
    afile.write "1 0 obj\x0d<< " # open the Root dictionary
    afile.write "\x0d/FDF << " # open the FDF dictionary
    afile.write "/Fields [ " # open the form Fields array

    info.each { |key, value|
      if value.class == Hash
        value.each { |sub_key, sub_value|
          afile.write '<< /T (' + key.to_s + '_' + sub_key.to_s + ') /V '
          afile.write '(' + escape_data(sub_value) + ') /ClrF 2 /ClrFf 1 >> '
        }
      else
        afile.write '<< /T (' + key.to_s + ') /V (' + escape_data(value) + ') /ClrF 2 /ClrFf 1 >> '
      end
    }

    afile.write "] \x0d" # close the Fields array
    afile.write ">> \x0d" # close the FDF dictionary
    afile.write ">> \x0dendobj\x0d" # close the Root dictionary

    # trailer note the "1 0 R" reference to "1 0 obj" above
    afile.write "trailer\x0d<<\x0d/Root 1 0 R \x0d\x0d>>\x0d"
    afile.write "%%EOF\x0d\x0a"
  end
end