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 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 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 |
@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
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/Softdev/e-mangue/script$ ruby -vRails 2.3.8
shajizaidi@Spock:
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",
:icon => ["60X80#", :jpg],
:large => ["X800", :jpg]
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:in
initialize'lib/paperclip_processors/ghostscript.rb:17:in
new' lib/paperclip_processors/ghostscript.rb:17:in
make'app/controllers/issues_controller.rb:79:in
new' app/controllers/issues_controller.rb:79:in
create'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