Class: Common::ConvertToPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/common/convert_to_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ ConvertToPdf

Returns a new instance of ConvertToPdf.



7
8
9
# File 'lib/common/convert_to_pdf.rb', line 7

def initialize(file)
  @file = file
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/common/convert_to_pdf.rb', line 11

def run
  in_file = Common::FileHelpers.generate_temp_file(@file.read)
  return in_file if @file.content_type == Mime[:pdf].to_s

  unless @file.content_type.starts_with?('image/')
    File.delete(in_file)
    raise IOError, "PDF conversion failed, unsupported file type: #{@file.content_type}"
  end

  out_file = "#{Common::FileHelpers.random_file_path}.pdf"

  begin
    MiniMagick::Tool::Convert.new do |convert|
      convert << '-units' << 'pixelsperinch' << '-density' << '72' << '-page' << 'letter'
      convert << in_file
      convert << out_file
    end
  ensure
    File.delete(in_file)
  end

  out_file
end