Class: Pdftk

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

Constant Summary collapse

EXE_NAME =
"pdftk"
@@config =
{}

Instance Method Summary collapse

Constructor Details

#initialize(execute_path = nil) ⇒ Pdftk

Returns a new instance of Pdftk.



18
19
20
21
22
23
# File 'lib/pdftk.rb', line 18

def initialize(execute_path = nil)
  @exe_path = execute_path || find_binary_path
  raise "Location of #{EXE_NAME} unknow" if @exe_path.empty?
  raise "Bad location of #{EXE_NAME}'s path" unless File.exists?(@exe_path)
  raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
end

Instance Method Details

#merge(source_array, destination_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pdftk.rb', line 25

def merge(source_array, destination_path)
  @source_files = source_array
  @merged_file_path =  destination_path

  command = "#{@exe_path} #{@source_files.join(' ')} cat output #{@merged_file_path}"
  err = Open3.popen3(command) do |stdin, stdout, stderr|
    stderr.read
  end
  pdf = @merged_file_path.read
  raise "PDF could not be generated!" if pdf and pdf.rstrip.length == 0
  pdf
rescue Exception => e
  raise "Failed to execute:\n#{command}\nError: #{e}"
end