Class: Paperclip::PdfMerge

Inherits:
Processor
  • Object
show all
Defined in:
lib/paperclip_processors/pdf_merge.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ PdfMerge

Returns a new instance of PdfMerge.



29
30
31
32
33
34
# File 'lib/paperclip_processors/pdf_merge.rb', line 29

def initialize file, options = {}, attachment = nil
  super
  @format = File.extname(@file.path)
  @basename = File.basename(@file.path, @format)
  @files = attachment.instance.pdf_files
end

Instance Method Details

#makeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/paperclip_processors/pdf_merge.rb', line 36

def make
  src = @file
  if @current_format && @current_format.downcase == '.pdf'
    dst = Tempfile.new([@basename, @format])
    dst.binmode

    file_paths = @files.map {|file| "#{Rails.root}/tmp/#{file.original_filename}"}

    all_page_file_paths = ""

    file_paths.each do |file_path|
      pdf = PDF::Reader.new(file_path)
      count = pdf.page_count
      counter = 0

      count.times do
        all_page_file_paths += "#{file_path}[#{counter}] "
        counter += 1
      end
    end

     Paperclip.run(
       'convert',
       "-density 150 #{all_page_file_paths} #{File.expand_path(dst.path)}"
      )

    return dst
  else
    return src
  end
end