Class: DocPDF::Adapters::Converters::Soffice

Inherits:
Base
  • Object
show all
Defined in:
lib/docpdf/adapters/converters/soffice.rb

Constant Summary collapse

MIME_TYPES =
%w[
  application/msword
  application/vnd.openxmlformats-officedocument.wordprocessingml.document
  application/vnd.ms-excel
  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  application/vnd.ms-powerpoint
  application/vnd.openxmlformats-officedocument.presentationml.presentation
  application/vnd.oasis.opendocument.text
  application/vnd.oasis.opendocument.spreadsheet
  application/vnd.oasis.opendocument.presentation
  text/csv
  text/html
  text/rtf
  application/rtf
].freeze

Class Method Summary collapse

Class Method Details

.convert(data, source_filename) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
# File 'lib/docpdf/adapters/converters/soffice.rb', line 25

def convert(data, source_filename)
  Dir.mktmpdir do |output_dir|
    Tempfile.create(["docpdf", File.extname(source_filename || ".tmp")]) do |tempfile|
      tempfile.binmode
      tempfile.write(data)
      tempfile.rewind

      soffice = DocPDF.configuration.soffice_path
      profile_dir = File.join(output_dir, "profile")
      stderr_path = File.join(output_dir, "stderr.log")
      success = system(soffice, "--headless",
                       "-env:UserInstallation=file://#{profile_dir}",
                       "--convert-to", "pdf",
                       "--outdir", output_dir, tempfile.path,
                       out: File::NULL, err: stderr_path)

      pdf_path = Dir.glob(File.join(output_dir, "*.pdf")).first

      if pdf_path
        File.binread(pdf_path)
      else
        stderr_output = File.read(stderr_path).strip
        detail = stderr_output.empty? ? "" : " (#{stderr_output})"

        if success.nil?
          raise SofficeNotFoundError, "LibreOffice (soffice) not found on PATH. Install LibreOffice or set DocPDF.configuration.soffice_path."
        elsif success
          raise ConversionError, "LibreOffice completed but produced no PDF for #{source_filename || 'document'}#{detail}"
        else
          raise ConversionError, "LibreOffice failed to convert #{source_filename || 'document'} to PDF#{detail}"
        end
      end
    end
  end
end