Class: PdftkForms::Wrapper

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

Overview

Wraps calls to PdfTk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdftk_path = nil, options = {}) ⇒ Wrapper

PdftkWrapper.new(‘/usr/bin/pdftk’, :encrypt => true, :encrypt_options => ‘allow Printing’) Or PdftkWrapper.new #assumes ‘pdftk’ is in the users path



11
12
13
14
# File 'lib/pdftk_forms/wrapper.rb', line 11

def initialize(pdftk_path = nil, options = {})
  @path = pdftk_path || "pdftk"
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/pdftk_forms/wrapper.rb', line 6

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/pdftk_forms/wrapper.rb', line 6

def path
  @path
end

Instance Method Details

#fields(template_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pdftk_forms/wrapper.rb', line 28

def fields(template_path)
  unless @all_fields
    field_output = call_pdftk(template_path, 'dump_data_fields')
    raw_fields = field_output.split(/^---\n/).reject {|text| text.empty? }
    @all_fields = raw_fields.map do |field_text|
      attributes = {}
      field_text.scan(/^(\w+): (.*)$/) do |key, value|
        if key == "FieldStateOption"
          attributes[key] ||= []
          attributes[key] << value
        else
          attributes[key] = value
        end
      end
      Field.new(attributes)
    end
  end
  @all_fields
end

#fill_form(template, destination, data = {}, xfdf_input = true) ⇒ Object

pdftk.fill_form(‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, :field1 => ‘value 1’) if your version of pdftk does not support xfdf then call pdftk.fill_form(‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, => ‘value 1’, false)



19
20
21
22
23
24
25
26
# File 'lib/pdftk_forms/wrapper.rb', line 19

def fill_form(template, destination, data = {}, xfdf_input = true)
  input = xfdf_input ? Xfdf.new(data) : Fdf.new(data)
  tmp = Tempfile.new('pdf_forms_input')
  tmp.close
  input.save_to tmp.path
  call_pdftk template, 'fill_form', tmp.path, 'output', destination, 'flatten', encrypt_options(tmp.path)
  tmp.unlink
end