Class: Pludoni::Pdfutils::Compressor

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

Instance Method Summary collapse

Constructor Details

#initialize(blob) ⇒ Compressor

Returns a new instance of Compressor.



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

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

Instance Method Details

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pludoni/pdfutils/compressor.rb', line 9

def run
  fname = File.basename(@blob.filename.to_s, '.*')
  tf = Tempfile.new([fname, '.pdf'])
  tf.binmode
  input = @blob.to_tf
  cli = "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -r150 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=#{Shellwords.escape tf.path} #{Shellwords.escape input.path}"

  stdout, stderr, status = Open3.capture3(cli)
  unless status.success?
    raise CompressionFailed, "PDF Compression failed: \nStdout: #{stdout}\nStderr: #{stderr}"
  end

  FileWrapper.make(tf, filename: fname + ".pdf")
end