Creating an image per page from a PDF

938 views
Skip to first unread message

Ibrahim Awwal

unread,
May 2, 2012, 8:24:03 AM5/2/12
to carrierwave
Hi,

I'm trying to do something where I upload a PDF file to my Rails
application, and the uploader splits the PDF into N files, one image
file for each page. I can do this trivially with Rmagick by itself by
just making an ImageList with the PDF and then saving the individual
pages, but now I'm wondering what the "right" way to do this using
CarrierWave would be. Should I be using manipulate! somehow? This bug
report seems to indicate that it should be possible, but it doesn't
seem to do anything for me. https://github.com/jnicklas/carrierwave/pull/691

I tried doing this in my uploader:

process :split

def split
manipulate! do |img, index|
img = yield(img) if block_given?
img
end
end

not really knowing what I'm doing, but this didn't really do anything
for me. Should I just call img.write(whateverfilename) in manipulate,
or is there a better way? I thought it would be convenient if each
page was saved as a version but I can't call version inside the
manipulate!'s block. Apologies for severely abusing programming
paradigms I'm sure,

-Ibrahim

Lee Jarvis

unread,
Jun 5, 2012, 7:16:44 AM6/5/12
to carri...@googlegroups.com
Hi,

Unfortunately I'm going through the same issue. Did you manage to solve this? There doesn't appear to be much support for PDF manipulation.

Jacob Tjørnholm

unread,
Jun 5, 2012, 9:06:23 AM6/5/12
to carri...@googlegroups.com
Hi, 

Have a look at the source for manipulate!: 

As you can see, if RMagick reports size > 1 for the "image", your block is yielded for each frame. 
So yes, I guess you should be able to simply save each image from your block. 

I don't know about storing individual versions, though...

/Jacob



--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.


Simon Bailey

unread,
Jun 11, 2012, 5:52:52 PM6/11/12
to carri...@googlegroups.com
I have now tried quite a few approaches to achieve the required result with no dice. If any experienced heads could help me out here it would be most appreciated:

http://stackoverflow.com/questions/10988023/multipage-pdf-processing-to-png-using-carrierwave

Simon


On Wednesday, 2 May 2012 13:24:03 UTC+1, Ibrahim Awwal wrote:

Simon Bailey

unread,
Jun 12, 2012, 6:02:55 PM6/12/12
to carri...@googlegroups.com
Ok seems I have a solution to writing the images from the pdf conversion, it doesn't feel clean but suggestions on refining would be appreciated:

def extract(format)
    cache_stored_file! if !cached?
    # Manually create upload dir as its not created at this point
    FileUtils.mkdir_p "#{Rails.root}/#{store_dir}" 
    images = Magick::ImageList.new(current_path)
    images.write "#{Rails.root}/#{store_dir}/#{filename}"
end

Simon Bailey

unread,
Jun 13, 2012, 12:56:59 PM6/13/12
to carri...@googlegroups.com
Damn that is a wrong solution, when I use #{store_dir} it still fails to write the files to the same directory Carrierwave is uploading to i.e. public/uploads/.. so when I transitioned to S3 it just wrote them to the Rails application root /uploads/.. :(

Ben Ridgway

unread,
Jul 19, 2013, 3:33:20 AM7/19/13
to carri...@googlegroups.com, newt...@gmail.com
Hi Gents. Has anyone managed to resolve this issue a year on? I'm trying to do something similar by taking a multipage PDF in and converting each page to PNG images. 

Thanks 
Ben

On Friday, March 15, 2013 3:03:30 AM UTC+9, James Summerill wrote:
Did you ever find out why this happening and is there a fix for it?
I'm having the same issue of Carrierwave not saving the the specified store_dir
Reply all
Reply to author
Forward
0 new messages