Class: PdfForms::PdftkWrapper
- Inherits:
-
Object
- Object
- PdfForms::PdftkWrapper
- Includes:
- NormalizePath
- Defined in:
- lib/pdf_forms/pdftk_wrapper.rb
Overview
Wraps calls to PdfTk
Constant Summary collapse
- PDFTK =
'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
returns the commands output, check general execution success with $?.success?.
-
#cat(*args) ⇒ Object
concatenate documents.
-
#fill_form(template, destination, data = {}, fill_options = {}) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, :field1 => ‘value 1’.
-
#get_field_names(template) ⇒ Object
get field names for template.
-
#get_fields(template) ⇒ Object
Get field metadata for template.
-
#initialize(*args) ⇒ PdftkWrapper
constructor
Initializes a new wrapper instance.
-
#multistamp(primary_file, stamp_file, output) ⇒ Object
applies each page of the stamp PDF to the corresponding page of the input PDF args: primary_file, stamp_file, output.
-
#read(path) ⇒ Object
pdftk.read ‘/path/to/form.pdf’ returns an instance of PdfForms::Pdf representing the given template.
-
#stamp(primary_file, stamp_file, output) ⇒ Object
stamp one pdf with another.
Methods included from NormalizePath
Constructor Details
#initialize(*args) ⇒ PdftkWrapper
Initializes a new wrapper instance. Pdftk will be autodetected from PATH: PdftkWrapper.new(:flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’)
The pdftk binary may also be explecitly specified: PdftkWrapper.new(‘/usr/bin/pdftk’, :flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’)
25 26 27 28 29 30 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 25 def initialize(*args) pdftk, = normalize_args *args @pdftk = Cliver.detect! pdftk raise "pdftk executable #{@pdftk} not found" unless call_pdftk('-h') && $?.success? @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 16 def @options end |
#pdftk ⇒ Object (readonly)
Returns the value of attribute pdftk.
16 17 18 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 16 def pdftk @pdftk end |
Instance Method Details
#call_pdftk(*args) ⇒ Object
returns the commands output, check general execution success with $?.success?
84 85 86 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 84 def call_pdftk(*args) SafeShell.execute pdftk, *(args.flatten) end |
#cat(*args) ⇒ Object
concatenate documents
args: in_file1, in_file2, … , in_file_n, output
91 92 93 94 95 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 91 def cat(*args) arguments = args.flatten.compact.map{|path| normalize_path(path)} output = arguments.pop call_pdftk arguments, 'output', output end |
#fill_form(template, destination, data = {}, fill_options = {}) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, :field1 => ‘value 1’
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 33 def fill_form(template, destination, data = {}, = {}) q_template = normalize_path(template) q_destination = normalize_path(destination) fdf = data_format(data) tmp = Tempfile.new('pdf_forms-fdf') tmp.close fdf.save_to tmp.path = {:tmp_path => tmp.path}.merge() args = [ q_template, 'fill_form', normalize_path(tmp.path), 'output', q_destination ] args, result = call_pdftk *args 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#{pdftk} #{args.flatten.compact.join ' '}\ncommand output was:\n#{result}\nfailing form data has been saved to #{fdf_path}") end ensure tmp.unlink if tmp end |
#get_field_names(template) ⇒ Object
get field names for template
Initialize the object with utf8_fields: true to get utf8 encoded field names.
78 79 80 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 78 def get_field_names(template) read(template).fields.map{|f| f.name} end |
#get_fields(template) ⇒ Object
Get field metadata for template
Initialize the object with utf8_fields: true to get utf8 encoded field metadata.
70 71 72 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 70 def get_fields(template) read(template).fields end |
#multistamp(primary_file, stamp_file, output) ⇒ Object
applies each page of the stamp PDF to the corresponding page of the input PDF args: primary_file, stamp_file, output
106 107 108 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 106 def multistamp(primary_file, stamp_file, output) call_pdftk primary_file, 'multistamp', stamp_file, 'output', output end |
#read(path) ⇒ Object
pdftk.read ‘/path/to/form.pdf’ returns an instance of PdfForms::Pdf representing the given template
62 63 64 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 62 def read(path) Pdf.new path, self, end |
#stamp(primary_file, stamp_file, output) ⇒ Object
stamp one pdf with another
args: primary_file, stamp_file, output
100 101 102 |
# File 'lib/pdf_forms/pdftk_wrapper.rb', line 100 def stamp(primary_file, stamp_file, output) call_pdftk primary_file, 'stamp', stamp_file, 'output', output end |