Jag Älskar
Jag Älskar

Reputation: 317

Carrierwave: Split multipage PDF into several images

I'm trying to split multipage PDFs for my Rails application and then convert the pages to png. In the best case I would end up with one entry per page in my image Model.

This is what I've got so far:

def generate_png
  manipulate! do |image, index|
    image.format = 'png'
    image.write("#{Rails.root}/public/#{store_dir}/image-#{index}.png")
  end
end

Unfortunately it only gives me image-0.png for the first frame and nothing for the other frames. Where am I going wrong?

How would I generate a new entry in my image model for each frame? Something like

model.images.new :image => image

?

Thank you all!

Upvotes: 1

Views: 1428

Answers (1)

abo-elleef
abo-elleef

Reputation: 2198

when you split pdf file into images if the the file name is file.pdf the results will be multi images with the same name, with attached number like file-0.png ,file-1.png ....... so you need to load all converted images from pdf and and convert them in any format you want

it gives you image-0.png because the return of spliting pdf file is only the first image which is the first page in the pdf file

you can upload the file normally and do after_create methods

def generate_image_form_pdf 
  file = @magazine.file
  file_name=file.path.sub(".pdf",".jpg")
  system("convert #{file.path}  #{file_name}")
end

the above method will create jpg images from the pdf file and store them in the same directory and the same name of the original pdf file

Upvotes: 1

Related Questions