Skip to content

Instantly share code, notes, and snippets.

@bastien
Created July 6, 2012 09:58
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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
@DiOssTech
Copy link

Hello

I am trying to use your script to generate jpgs for pdfs without success.

I have an older development environment:
shajizaidi@Spock:/Softdev/e-mangue/script$ rails -v
Rails 2.3.8
shajizaidi@Spock:
/Softdev/e-mangue/script$ ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [i686-linux]

And relevant gems:
aasm (3.0.1)
actionmailer (2.3.8, 2.2.2)
actionpack (2.3.8, 2.2.2)
activemodel (3.1.3)
activerecord (2.3.8, 2.2.2)
activeresource (2.3.8, 2.2.2)
activesupport (3.1.3, 2.3.8, 2.2.2)
arel (2.2.1)
git (1.2.5)
hpricot (0.8.5)
httpclient (2.2.4)
mysql (2.8.1)
passenger (3.0.11)
rack (1.4.0, 1.1.3)
rails (2.3.8, 2.2.2)
rake (0.9.2)
ruby-blockcache (0.2)
rubygems-update (1.5.3)
rubyist-aasm (2.1.1)
soap4r (1.5.8)

The excerpt from my Model is below:
class Issue < ActiveRecord::Base
include AASM

belongs_to :publication
has_many :readings
has_many :readers, :through => :readings, :class_name => "User"

has_many :issue_metadatas, :dependent => :destroy
has_many :table_of_contents_entries, :dependent => :destroy

has_many :issue_ratings

has_one :issue_status, :dependent => :destroy

has_attached_file :pdf,

:url => "/assets/:id/",

:path => ":rails_root/assets/docs/:id/:style/:basename.:extension",

                :styles => {

:icon => ["60X80#", :jpg],

                            :thumb => ["360x480#", :jpg]

:large => ["X800", :jpg]

                            },
                :processors => [:ghostscript, :thumbnail],
                :convert_options => {:all => '-colorspace RGB -flatten -density 300 -quality 100'},
                :path => ":page_path/:class/:id/:resource_token/:style/:filename"

validates_presence_of :issue_number
validates_presence_of :title
validates_presence_of :date

When I try to upload a pdf I get the following error:
RuntimeError (cannot generate tempfile '): /home/shajizaidi/.rvm/rubies/ruby-1.8.7-p357/lib/ruby/1.8/tempfile.rb:52:ininitialize'
lib/paperclip_processors/ghostscript.rb:17:in new' lib/paperclip_processors/ghostscript.rb:17:inmake'
app/controllers/issues_controller.rb:79:in new' app/controllers/issues_controller.rb:79:increate'

In ghostscript.rb

To test if I change the way the temp file is being generated by not using an array for example:
dst = Tempfile.new(“#@basename})

instead of:
dst = Tempfile.new([@basename, @Format ? ".#{@Format}" : ''])

I don't get the error; but then of course the file name of the temp file is not correctly generated.

I have tried to research this quite a bit and cant seem to find a solution.

Any help would be greatly appreciated.

Thanks

Shaji

@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