Class: Nguyen::PdftkWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/nguyen/pdftk_wrapper.rb

Overview

Wraps calls to PdfTk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdftk_path, options = {}) ⇒ PdftkWrapper

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


12
13
14
15
# File 'lib/nguyen/pdftk_wrapper.rb', line 12

def initialize(pdftk_path, options = {})
  @pdftk = pdftk_path
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.


9
10
11
# File 'lib/nguyen/pdftk_wrapper.rb', line 9

def options
  @options
end

#pdftkObject (readonly)

Returns the value of attribute pdftk.


9
10
11
# File 'lib/nguyen/pdftk_wrapper.rb', line 9

def pdftk
  @pdftk
end

Instance Method Details

#call_pdftk(*args) ⇒ Object


41
42
43
# File 'lib/nguyen/pdftk_wrapper.rb', line 41

def call_pdftk(*args)
  %x{#{pdftk_command args}}
end

#cat(*files, output) ⇒ Object


45
46
47
48
49
# 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_format) ⇒ Object

pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, xfdf_or_fdf_object


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nguyen/pdftk_wrapper.rb', line 18

def fill_form(template, destination, form_data_format)
  tmp = Tempfile.new('pdf_forms-fdf')
  tmp.close
  form_data_format.save_to tmp.path
  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) ⇒ Object


37
38
39
# File 'lib/nguyen/pdftk_wrapper.rb', line 37

def get_field_names(template)
  read(template).fields
end

#read(path) ⇒ Object

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


33
34
35
# File 'lib/nguyen/pdftk_wrapper.rb', line 33

def read(path)
  Pdf.new path, self
end