Skip to content

Instantly share code, notes, and snippets.

@richpeck
Forked from bastien/content.rb
Created November 15, 2015 22:02
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 richpeck/8b5d2b146f3056d78c1e to your computer and use it in GitHub Desktop.
Save richpeck/8b5d2b146f3056d78c1e 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment