Class: Pludoni::Pdfutils::ConvertDocumentToPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/pludoni/pdfutils/convert_document_to_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(blob) ⇒ ConvertDocumentToPdf

Returns a new instance of ConvertDocumentToPdf.



5
6
7
# File 'lib/pludoni/pdfutils/convert_document_to_pdf.rb', line 5

def initialize(blob)
  @blob = FileWrapper.make(blob)
end

Instance Method Details

#run(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pludoni/pdfutils/convert_document_to_pdf.rb', line 9

def run(&block)
  fname = File.basename(@blob.filename.to_s, File.extname(@blob.filename.to_s))
  @blob.open do |source|
    # convert image to pdf
    tf = Tempfile.new([fname, '.pdf'])
    tf.binmode

    command = ['soffice']
    command << '--headless'
    command << '--convert-to'
    command << 'pdf'
    command << '--outdir'
    command << File.dirname(source.path)
    command << source.path
    stdout, stderr, status = Open3.capture3(*command)
    unless status.success?
      raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
    end
    pdf_path = source.path.sub(/\.[a-z0-9]+$/i, '.pdf')
    unless File.exist?(pdf_path)
      raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
    end

    FileUtils.move(pdf_path, tf.path)

    tf.open

    FileWrapper.make(tf, filename: "#{fname}.pdf")
  end
end