Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Possible to convert a PDF to Images?

I have a Rails 3 app with PaperClip/S3...

Is it possible to allow a user to upload a PDF, convert the PDF to images, and then upload?

Thanks!

like image 756
AnApprentice Avatar asked Nov 05 '10 22:11

AnApprentice


1 Answers

Take a look at imagemagick and rmagick plugin for ruby. This allows you to do all kinds of image conversions, including PDF to jpeg.

http://rmagick.rubyforge.org/

EDIT:

untested sample code:

require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
pdf.write("myimage.jpg")

if doc.pdf has 3 pages, this should output 3 images:
myimage.jpg.0
myimage.jpg.1
myimage.jpg.2

take a look at the end of the documentation on this page, which shows a similar example with a multi-frame gif converted to multiple PNGs using imagelist: http://www.imagemagick.org/RMagick/doc/ilist.html#write

like image 64
johnmcaliley Avatar answered Oct 20 '22 11:10

johnmcaliley