class OfficeConverter::Converter

Attributes

soffice_command[RW]

Public Class Methods

new(source, target_dir, soffice_command = nil, convert_to = nil) click to toggle source
# File lib/office_converter.rb, line 19
def initialize(source, target_dir, soffice_command = nil, convert_to = nil)
  @source = source
  @target_dir = target_dir
  @soffice_command = soffice_command
  @convert_to = convert_to || "pdf"
  determine_soffice_command
  check_source_type

  unless @soffice_command && File.exists?(@soffice_command)
    raise IOError, "Can't find Libreoffice or Openoffice executable."
  end
end

Public Instance Methods

convert() click to toggle source
# File lib/office_converter.rb, line 32
def convert
  orig_stdout = $stdout.clone
  $stdout.reopen File.new('/dev/null', 'w')
  pid = system(["export HOME=/tmp && ",@soffice_command, "--headless", "--convert-to", @convert_to, @source, "--outdir", @target_dir].join(" "))
  $stdout.reopen orig_stdout
end

Private Instance Methods

check_source_type() click to toggle source
# File lib/office_converter.rb, line 60
def check_source_type
  return if File.exists?(@source) && !File.directory?(@source) #file
  raise IOError, "Source (#{@source}) is neither a file nor an URL."
end
determine_soffice_command() click to toggle source
# File lib/office_converter.rb, line 41
def determine_soffice_command
  unless @soffice_command
    @soffice_command ||= which("soffice")
    @soffice_command ||= which("soffice.bin")
  end
end
which(cmd) click to toggle source
# File lib/office_converter.rb, line 48
def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    end
  end

  return nil
end