Class: Nguyen::PdftkWrapper
- Inherits:
-
Object
- Object
- Nguyen::PdftkWrapper
- Defined in:
- lib/nguyen/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
invokes pdftk with the given arguments and returns its output (stdout and stderr combined); arguments are passed to the process verbatim, without any shell involved, so paths need no quoting or escaping.
- #cat(*files, output) ⇒ Object
-
#fill_form(template, destination, form_data) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, xfdf_or_fdf_object or xfdf_or_fdf_string.
- #get_field_names(template) ⇒ Object
-
#initialize(pdftk_path, options = {}) ⇒ PdftkWrapper
constructor
PdftkWrapper.new(‘/usr/bin/pdftk’, :flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’).
-
#multistamp(template, stamp_file, destination) ⇒ Object
pdftk.multistamp ‘/path/to/source.pdf’, ‘/path/to/stamp.pdf’, ‘/path/to/destination.pdf’ overlays the stamp PDF onto the source PDF page by page.
-
#read(path) ⇒ Object
pdftk.read ‘/path/to/form.pdf’ returns an instance of Nguyen::Pdf representing the given template.
-
#stamp(template, stamp_file, destination) ⇒ Object
pdftk.stamp ‘/path/to/source.pdf’, ‘/path/to/stamp.pdf’, ‘/path/to/destination.pdf’ overlays the stamp PDF on top of every page of the source PDF.
Constructor Details
#initialize(pdftk_path, options = {}) ⇒ PdftkWrapper
PdftkWrapper.new(‘/usr/bin/pdftk’, :flatten => true, :encrypt => true, :encrypt_options => ‘allow Printing’)
14 15 16 17 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 14 def initialize(pdftk_path, = {}) @pdftk = pdftk_path @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 11 def @options end |
#pdftk ⇒ Object (readonly)
Returns the value of attribute pdftk.
11 12 13 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 11 def pdftk @pdftk end |
Instance Method Details
#call_pdftk(*args) ⇒ Object
invokes pdftk with the given arguments and returns its output (stdout and stderr combined); arguments are passed to the process verbatim, without any shell involved, so paths need no quoting or escaping
58 59 60 61 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 58 def call_pdftk(*args) output, _status = Open3.capture2e(pdftk, *normalize_args(args)) output end |
#cat(*files, output) ⇒ Object
63 64 65 66 67 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 63 def cat(*files, output) files = files.flatten input = files.flat_map { |f| f.to_s.include?('*') ? Dir.glob(f) : [f] } call_pdftk(*input, 'output', output) end |
#fill_form(template, destination, form_data) ⇒ Object
pdftk.fill_form ‘/path/to/form.pdf’, ‘/path/to/destination.pdf’, xfdf_or_fdf_object or xfdf_or_fdf_string
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 20 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) args = [template, 'fill_form', tmp.path, 'output', destination, (tmp.path)] output = call_pdftk(*args) unless File.readable?(destination) && File.size(destination) > 0 raise PdftkError.new("failed to fill form with command\n#{describe_command(args)}\ncommand output was:\n#{output}") end ensure tmp.unlink if tmp end |
#get_field_names(template) ⇒ Object
51 52 53 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 51 def get_field_names(template) read(template).fields end |
#multistamp(template, stamp_file, destination) ⇒ Object
pdftk.multistamp ‘/path/to/source.pdf’, ‘/path/to/stamp.pdf’, ‘/path/to/destination.pdf’ overlays the stamp PDF onto the source PDF page by page
41 42 43 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 41 def multistamp(template, stamp_file, destination) stamp_operation 'multistamp', template, stamp_file, destination end |
#read(path) ⇒ Object
pdftk.read ‘/path/to/form.pdf’ returns an instance of Nguyen::Pdf representing the given template
47 48 49 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 47 def read(path) Pdf.new path, self end |
#stamp(template, stamp_file, destination) ⇒ Object
pdftk.stamp ‘/path/to/source.pdf’, ‘/path/to/stamp.pdf’, ‘/path/to/destination.pdf’ overlays the stamp PDF on top of every page of the source PDF
35 36 37 |
# File 'lib/nguyen/pdftk_wrapper.rb', line 35 def stamp(template, stamp_file, destination) stamp_operation 'stamp', template, stamp_file, destination end |