class Nguyen::PdftkWrapper

Wraps calls to PdfTk

Attributes

options[R]
pdftk[R]

Public Class Methods

new(pdftk_path, options = {}) click to toggle source

PdftkWrapper.new('/usr/bin/pdftk', :flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')

# File lib/nguyen/pdftk_wrapper.rb, line 12
def initialize(pdftk_path, options = {})
  @pdftk = pdftk_path
  @options = options
end

Public Instance Methods

call_pdftk(*args) click to toggle source
# File lib/nguyen/pdftk_wrapper.rb, line 41
def call_pdftk(*args)
  %x{#{pdftk_command args}}
end
cat(*files,output) click to toggle source
# File lib/nguyen/pdftk_wrapper.rb, line 45
def cat(*files,output)
  files = files[0] if files[0].class == Array
  input = files.map{|f| %Q(#{f})}
  call_pdftk(*input,'output',output)
end
fill_form(template, destination, form_data) click to toggle source

pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', xfdf_or_fdf_object or xfdf_or_fdf_string

# File lib/nguyen/pdftk_wrapper.rb, line 18
def fill_form(template, destination, form_data)
  tmp = Tempfile.new('pdf_forms-fdf')
  tmp.close
  form_data.respond_to?(:save_to) ? form_data.save_to(tmp.path) : File.write(tmp.path, form_data)
  command = pdftk_command %Q("#{template}"), 'fill_form', %Q("#{tmp.path}"), 'output', destination, add_options(tmp.path)
  output = %x{#{command}}
  unless File.readable?(destination) && File.size(destination) > 0
    raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}")
  end
ensure
  tmp.unlink if tmp
end
get_field_names(template) click to toggle source
# File lib/nguyen/pdftk_wrapper.rb, line 37
def get_field_names(template)
  read(template).fields
end
read(path) click to toggle source

pdftk.read '/path/to/form.pdf' returns an instance of Nguyen::Pdf representing the given template

# File lib/nguyen/pdftk_wrapper.rb, line 33
def read(path)
  Pdf.new path, self
end

Protected Instance Methods

add_options(pwd) click to toggle source
# File lib/nguyen/pdftk_wrapper.rb, line 57
def add_options(pwd)
  return if options.empty?
  opt_args = []
  if options[:flatten]
    opt_args << 'flatten'
  end
  if options[:encrypt]
    opt_args.concat ['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
  end
  opt_args
end
pdftk_command(*args) click to toggle source
# File lib/nguyen/pdftk_wrapper.rb, line 53
def pdftk_command(*args)
  "#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
end