Class: Pludoni::Pdfutils::Joiner

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

Constant Summary collapse

MAX_LENGTH =
200

Instance Method Summary collapse

Constructor Details

#initialize(blobs) ⇒ Joiner

Returns a new instance of Joiner.



6
7
8
# File 'lib/pludoni/pdfutils/joiner.rb', line 6

def initialize(blobs)
  @blobs = blobs.map { |i| FileWrapper.make(i) }
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pludoni/pdfutils/joiner.rb', line 10

def run
  fname = @blobs.map(&:filename).map { |i| i.split('.').first[0..20] }.join('-')
  if fname.length > MAX_LENGTH
    fname = fname[0..MAX_LENGTH]
  end

  tf = Tempfile.new([fname, '.pdf'])
  tf.binmode
  tfs = @blobs.map { |i| i.to_tf }
  cli = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=#{tf.path} #{tfs.map(&:path).join(' ')}"
  stdout, stderr, status = Open3.capture3(cli)
  unless status.success?
    raise JoiningFailedError, "PDF Joining failed: \nStdout: #{stdout}\nStderr: #{stderr}"
  end

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