Skip to content

Instantly share code, notes, and snippets.

@bastien
Created July 6, 2012 09:58
Show Gist options
  • Save bastien/3059321 to your computer and use it in GitHub Desktop.
Save bastien/3059321 to your computer and use it in GitHub Desktop.
Paperclip processor to convert PDF to JPG to go around problems with the old versions of imagemagick and ghostscript available on Heroku. This file is located in [APP_ROOT]/lib/paperclip_processors/ghostscript.rb
# Model using the ghostscript processor
class Content < ActiveRecord::Base
has_attached_file :resource,
:styles => { :preview => ["725x1200>", :jpg], :thumb => ["100x140>", :jpg] },
:processors => [:ghostscript, :thumbnail],
:convert_options => { :all => '-colorspace RGB -flatten -density 300 -quality 100' },
:path => ":page_path/:class/:id/:resource_token/:style/:filename"
end
module Paperclip
class Ghostscript < Processor
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :source_file_options
def initialize file, options = {}, attachment = nil
super
@file = file
@format = options[:format]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode
begin
parameters = []
parameters << "-dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -dUseCIEColor -dFirstPage=1 -dLastPage=1"
parameters << "-sOutputFile=:dest"
parameters << ":source"
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
success = Paperclip.run("gs", parameters, :source => "#{File.expand_path(src.path)}", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
end
dst
end
end
end
@momolog
Copy link

momolog commented Jan 21, 2013

@DiOssTech: Please repair the markup of your comment. Try explicit code highlighting using

```ruby

where necessary.
Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment