Skip to content

Instantly share code, notes, and snippets.

@bastien
Created July 6, 2012 09:58
Show Gist options
  • Select an option

  • Save bastien/3059321 to your computer and use it in GitHub Desktop.

Select an option

Save bastien/3059321 to your computer and use it in GitHub Desktop.

Revisions

  1. bastien revised this gist Jul 6, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ghostscript.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    module Paperclip
    # Created to answer a single need, converting pdf to rgb before resizing them, because it at once with ImageMagick 6.6.6 wasn't working
    # Since heroku isn't using 6.6.6 yet the call to this proessor is commented out in the content.rb class
    class Ghostscript < Processor

    attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :source_file_options
  2. bastien created this gist Jul 6, 2012.
    9 changes: 9 additions & 0 deletions content.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # 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
    37 changes: 37 additions & 0 deletions ghostscript.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    module Paperclip
    # Created to answer a single need, converting pdf to rgb before resizing them, because it at once with ImageMagick 6.6.6 wasn't working
    # Since heroku isn't using 6.6.6 yet the call to this proessor is commented out in the content.rb class
    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