Sha256: 8934e1302147019bef97250c22316d90d3e58d35f493a5c5fdbdb1ab76a5aed0

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

require 'os'

require 'libis/tools/extend/string'
require 'libis/tools/logger'
require 'libis/tools/command'

require 'libis/format/config'

module Libis
  module Format
    module Tool

      class PdfOptimizer
        include ::Libis::Tools::Logger

        def self.run(source, target, quality)
          self.new.run source, target, quality
        end

        def run(source, target, quality)

          timeout = Libis::Format::Config[:timeouts][:pdf_optimizer]
          result = Libis::Tools::Command.run(
              'gs',
              '-sDEVICE=pdfwrite',
              '-dCompatibilityLevel=1.4',
              "-dPDFSETTINGS=/#{quality}",
              '-dNOPAUSE',
              '-dBATCH',
              "-sOutputFile=#{target}",
              "#{source}",
              timeout: timeout,
              kill_after: timeout * 2
          )

          raise RuntimeError, "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
          raise RuntimeError, "#{self.class} errors: #{result[:err].join("\n")}" unless result[:status] == 0 && result[:err].empty?

          result
        end
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
libis-format-1.0.0 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.74 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.72 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.70 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.69 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.68 lib/libis/format/tool/pdf_optimizer.rb
libis-format-0.9.67 lib/libis/format/tool/pdf_optimizer.rb