Class: Libis::Format::Tool::OfficeToPdf

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger
Defined in:
lib/libis/format/tool/office_to_pdf.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(source, target, options = {}) ⇒ Object



16
17
18
# File 'lib/libis/format/tool/office_to_pdf.rb', line 16

def self.run(source, target, options = {})
  self.new.run source, target, options
end

Instance Method Details

#run(source, target, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/libis/format/tool/office_to_pdf.rb', line 20

def run(source, target, options = {})
  workdir = '/...'
  workdir = Dir.tmpdir unless Dir.exist? workdir

  workdir = File.join(workdir, rand(1000000).to_s)
  FileUtils.mkpath(workdir)

  src_file = File.join(workdir, File.basename(source))
  FileUtils.symlink source, src_file

  tgt_file = File.join(workdir, File.basename(source, '.*') + '.pdf')

  export_filter = options[:export_filter] || 'pdf'

  result = Libis::Tools::Command.run(
      Libis::Format::Config[:soffice_path], '--headless',
      '--convert-to', export_filter,
      '--outdir', workdir, src_file
  )

  unless result[:status] == 0
    warn "PdfConvert errors: #{(result[:err] + result[:out]).join("\n")}"
    return false
  end

  FileUtils.copy tgt_file, target, preserve: true
  FileUtils.rmtree workdir

  result[:out]
end