module Pdfunite

Constants

VERSION

Public Class Methods

binary() click to toggle source
# File lib/pdfunite.rb, line 32
def binary
  @@binary
end
binary=(binary) click to toggle source

You can set a custom pdfunite binary here

# File lib/pdfunite.rb, line 28
def binary=(binary)
  @@binary = binary
end
join(*args) { |arg| ... } click to toggle source

Join PDF files or PDF data

Pdfunite.join(filenames) Pdfunite.join(objects) { |obj| obj.to_pdf }

# File lib/pdfunite.rb, line 42
def join(*args)
  output = nil
  Dir.mktmpdir do |dir|
    tmpdir = Pathname.new(dir).realpath
    files = args.flatten(1).collect.with_index do |arg, idx|
      realpath = if block_given?
        data = yield(arg)
        file = tmpdir.join("#{idx}.pdf")
        file.open('wb') { |f| f << data }
        file.realpath
      else
        Pathname.new(arg).realpath
      end
      realpath.to_s
    end
    outfile = tmpdir.join('output.pdf')
    cmdline = [binary.to_s, *files, outfile.to_s]
    output = outfile.binread if run_command(cmdline.shelljoin)
  end
  output
end
logger() click to toggle source
# File lib/pdfunite.rb, line 21
def logger
  @@logger
end
logger=(logger) click to toggle source

You can set a custom logger here

# File lib/pdfunite.rb, line 17
def logger=(logger)
  @@logger = logger
end

Private Class Methods

run_command(cmdline) click to toggle source
# File lib/pdfunite.rb, line 66
def run_command(cmdline)
  logger = Pdfunite.logger || Logger.new(STDOUT)
  stdout, stderr, status = Open3.capture3(cmdline)
  logger.info "Pdfunite: #{cmdline}"
  raise unless status.success?
  true
rescue Errno::ENOENT
  raise BinaryNotFound, "The pdfunite executable could not be found at #{binary.inspect}. Check the pdfunite README for more info."
rescue => e
  logger.error "Pdfunite error: #{e}\ncommand: #{cmdline}\nerror: #{stderr}"
  false
end