Sha256: 921a5b5b8e859d3688c49ea2e4eaaa3dcd214770d767858963d2bd46da1f52e2

Contents?: true

Size: 1.58 KB

Versions: 27

Compression:

Stored size: 1.58 KB

Contents

module Bizside
  module FileConverter
    EXT_IMAGE = ['.gif', '.jpg', '.jpeg', '.png']
    EXT_OFFICE = ['.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx']
    EXT_PDF = ['.pdf']

    def self.convert_to_pdf(file)
      dest = file.path + '.pdf'

      case File.extname(file.path)
      when *EXT_OFFICE
        unless system("java -Xmx512m -jar /opt/jodconverter/lib/jodconverter-core.jar #{file.path} #{dest}")
          raise "オフィス文書からPDFに変換できませんでした。file=#{file.path}"
        end
      when *EXT_PDF
        unless system("cp #{file.path} #{dest}")
          raise "PDFをコピーできませんでした。file=#{file.path}"
        end
      else
        raise "サポートしていない拡張子です。file=#{file.path}"
      end

      File.new(dest)
    end

    def self.convert_to_image(file)
      require 'rmagick'

      case File.extname(file.path)
      when *EXT_IMAGE
        dest = file.path + File.extname(file.path)
        unless system("cp #{file.path} #{dest}")
          raise "画像をコピーできませんでした。file=#{file.path}"
        end
        ret = File.new(dest)
      when *EXT_OFFICE
        pdf = convert_to_pdf(file)
        ret = convert_to_image(pdf)
      when *EXT_PDF
        dest = file.path + '.png'
        images = Magick::ImageList.new(file.path) do
          self.quality = 100
          self.density = 96
        end
        images.first.write(dest)
        ret = File.new(dest)
      else
        raise "サポートしていない拡張子です。file=#{file.path}"
      end

      ret
    end

  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
bizside-2.3.3 lib/bizside/file_converter.rb
bizside-2.3.2 lib/bizside/file_converter.rb
bizside-2.3.1 lib/bizside/file_converter.rb
bizside-2.3.0 lib/bizside/file_converter.rb
bizside-2.2.3 lib/bizside/file_converter.rb
bizside-2.2.2 lib/bizside/file_converter.rb
bizside-2.2.1 lib/bizside/file_converter.rb
bizside-2.2.0 lib/bizside/file_converter.rb
bizside-2.1.12 lib/bizside/file_converter.rb
bizside-2.1.11 lib/bizside/file_converter.rb
bizside-2.1.10 lib/bizside/file_converter.rb
bizside-2.1.9 lib/bizside/file_converter.rb
bizside-2.1.8 lib/bizside/file_converter.rb
bizside-2.1.7 lib/bizside/file_converter.rb
bizside-2.1.6 lib/bizside/file_converter.rb
bizside-2.1.5 lib/bizside/file_converter.rb
bizside-2.1.4 lib/bizside/file_converter.rb
bizside-2.1.3 lib/bizside/file_converter.rb
bizside-2.1.2 lib/bizside/file_converter.rb
bizside-2.1.1 lib/bizside/file_converter.rb