Fernando Perez wrote:
> Hi,
> 
> Let's say I upload a pdf file. Imagemagick extracts all pages out of it
> and stores the png images on the hard-drive. How to easily handle all
> these generated files with Paperclip?
> 
> Has anyone done that before? Thanks for your advice

I've done precisely this just recently. It isn't as tricky as it seems, 
really. All you need are a few steps in your pdf processor that will 
take the extracted images and add them to a new record. So, if you have 
the following relationship:

class Document < ActiveRecord::Base
  has_many :images
  has_attached_file :file, :styles => { :original => {} }, :processors 
=> [:extract]
end

class Image < ActiveRecord::Base
  belongs_to :document
  has_attached_file :image
end

In your processor perform your extraction to a temporary folder, and 
after it is done do something like the following:

if @attachment.respond_to?(:instance) and 
@attachment.instance.respond_to?(:images)
  @attachment.instance.images.destroy_all

  Dir.glob("#...@temporary}/*.{jpg,png}").each do |path|
    File.open(path) { |file| @attachment.instance.images.create(:image 
=> file) }
  end
else
  raise PaperclipError, "Unable to save extracted pages. No valid 
attachment."
end

Afterwards make sure to remove the temporary folder and you should be 
good.
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to