Class: PdftkForms::Xfdf

Inherits:
Object
  • Object
show all
Defined in:
lib/pdftk_forms/xfdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Xfdf

Returns a new instance of Xfdf.



4
5
6
# File 'lib/pdftk_forms/xfdf.rb', line 4

def initialize(data = {})
  @data = data
end

Instance Method Details

#save_to(path) ⇒ Object



31
32
33
34
# File 'lib/pdftk_forms/xfdf.rb', line 31

def save_to(path)
  xml = to_xfdf.target!
  (File.open(path, 'w') << xml).close
end

#to_xfdfObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pdftk_forms/xfdf.rb', line 8

def to_xfdf
  xfdf = Builder::XmlMarkup.new
  xfdf.instruct!
  xfdf.xfdf(:xmlns => "http://ns.adobe.com/xfdf/", :"xml:space" => "preserve") do
    xfdf.fields do
      @data.each do |key, value|
        if Hash === value
          value.each do |sub_key, sub_value|
            xfdf.field(:name => "#{key}_#{sub_key}") do
              xfdf.value sub_value
            end
          end
        else
          xfdf.field(:name => key) do
            xfdf.value value
          end
        end
      end
    end
  end
  xfdf
end