Sha256: 9b5d9446fe0e6d9013ccd2b52f288e44061fec70ef720e2baf60c8dffae66013

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'tempfile'
module PdftkForms
  # Wraps calls to PdfTk
  class Wrapper
    
    attr_reader :path, :options
    
    # PdftkWrapper.new('/usr/bin/pdftk', :encrypt => true, :encrypt_options => 'allow Printing')
    # Or
    # PdftkWrapper.new  #assumes 'pdftk' is in the users path
    def initialize(pdftk_path = nil, options = {})
      @path = pdftk_path || "pdftk"
      @options = options
    end
    
    # 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', {:field1 => 'value 1'}, false)
    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
    
    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
    
    protected
    
    def encrypt_options(pwd)
      if options[:encrypt]
        ['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
      end
    end
    
    def call_pdftk(*args)
      %x{#{path} #{args.flatten.compact.join ' '}}
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pdftk_forms-0.1.0 lib/pdftk_forms/wrapper.rb