Skip to content

Instantly share code, notes, and snippets.

@jessieay
Created June 21, 2013 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessieay/5832466 to your computer and use it in GitHub Desktop.
Save jessieay/5832466 to your computer and use it in GitHub Desktop.
custom paperclip processor - lives in lib/paperclip_processors
module Paperclip
class Merge < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
@files = attachment.instance.files
end
def make
src = @file
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)}"
)
dst
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment