# Cleanly handling sub-generatede files with Paperclip

**URL:** <https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674>\
**Category:** Rails\
**Created:** [August 21, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674 "2010-08-21T19:38:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 21, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674/1 "2010-08-21T19:38:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 21, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674/2 "2010-08-21T19:56:47Z")

</div>

Fernando P. 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.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 21, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674/3 "2010-08-21T21:06:48Z")

</div>

> Parker S. wrote:  
> Interesting approach. In particular problem you ran into in practice?  
> Too many files for the fs? Database blowing up? Other?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 21, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/cleanly-handling-sub-generatede-files-with-paperclip/193674/4 "2010-08-21T21:29:35Z")

</div>

Fernando P. wrote:

> Interesting approach. In particular problem you ran into in practice?  
> Too many files for the fs? Database blowing up? Other?

It has worked really well in practice. The failing point was always  
ImageMagick, really. We ended up using pdf2image instead, which yielded  
much better output, much faster. We’ve processed 120+ page documents, so  
the file issue wasn’t a problem. With the time it takes to process the  
images (assuming you are resizing / thumbnailing) you’ll certainly want  
to process with a background processor though–Delayed Job, Resque or  
the like.
