Class: PdfForms::PdftkWrapper
- Inherits:
-
Object
- Object
- PdfForms::PdftkWrapper
- Includes:
- SafePath
- Defined in:
- lib/pdf_forms/pdftk_wrapper.rb
Overview
Wraps calls to PdfTk
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pdftk ⇒ Object
readonly
Returns the value of attribute pdftk.
Instance Method Summary collapse
- #call_pdftk(*args) ⇒ Object
-
#cat(*args) ⇒ Object
concatenate documents.
-
#fill_form(template, destination, data = {}) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, :field1 => ‘value 1’.
- #get_field_names(template) ⇒ Object
- #get_fields(template) ⇒ Object
-
#initialize(pdftk_path, options = {}) ⇒ PdftkWrapper
constructor
PdftkWrapper.new(‘/usr/bin/pdftk’, :flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’).
-
#read(path) ⇒ Object
pdftk.read ‘/path/to/form.pdf’ returns an instance of PdfForms::Pdf representing the given template.
Methods included from SafePath
#file_path, #normalize_path, #quote_path
Constructor Details
#initialize(pdftk_path, options = {}) ⇒ PdftkWrapper
PdftkWrapper.new(‘/usr/bin/pdftk’, :flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’)
16 17 18 19 20 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 16 def initialize(pdftk_path, = {}) @pdftk = file_path(pdftk_path) raise "pdftk executable #{@pdftk} not found" unless call_pdftk('-h') =~ /pdftk\s+\d/i @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 13 def @options end |
#pdftk ⇒ Object (readonly)
Returns the value of attribute pdftk.
13 14 15 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 13 def pdftk @pdftk end |
Instance Method Details
#call_pdftk(*args) ⇒ Object
60 61 62 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 60 def call_pdftk(*args) %x{#{pdftk_command args}} end |
#cat(*args) ⇒ Object
concatenate documents
args: in_file1, in_file2, … , in_file_n, output
67 68 69 70 71 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 67 def cat(*args) arguments = args.flatten.compact.map{|path| safe_path(path)} output = arguments.pop call_pdftk(*([arguments, 'output', output].flatten)) end |
#fill_form(template, destination, data = {}) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, :field1 => ‘value 1’
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 23 def fill_form(template, destination, data = {}) q_template = safe_path(template) q_destination = safe_path(destination) fdf = data_format(data) tmp = Tempfile.new('pdf_forms-fdf') tmp.close fdf.save_to tmp.path command = pdftk_command q_template, 'fill_form', safe_path(tmp.path), 'output', q_destination, (tmp.path) output = %x{#{command}} unless File.readable?(destination) && File.size(destination) > 0 fdf_path = nil begin fdf_path = File.join(File.dirname(tmp.path), "#{Time.now.strftime '%Y%m%d%H%M%S'}.fdf") fdf.save_to fdf_path rescue Exception fdf_path = "#{$!}\n#{$!.backtrace.join("\n")}" end raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}\nfailing form data has been saved to #{fdf_path}") end ensure tmp.unlink if tmp end |
#get_field_names(template) ⇒ Object
56 57 58 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 56 def get_field_names(template) read(template).fields.map{|f| f.name} end |
#get_fields(template) ⇒ Object
52 53 54 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 52 def get_fields(template) read(template).fields end |