Created
July 6, 2012 09:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DiOssTech: Please repair the markup of your comment. Try explicit code highlighting using
where necessary.
Cheers.