How to convert a PDF into an array of images, with Carrierwave and MiniMagick (Ruby on Rails) -
i'm converting uploaded pdfs images, 1 image per page. have figured out how generate images using minimagick::tool::convert
, don't know how write version
block uploader, can access array of image urls.
here's uploader far:
class documentuploader < carrierwave::uploader::base include carrierwave::minimagick storage :file # storage :fog def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end version :jpg process :convert_to_images process :set_content_type_jpg def convert_to_images(*args) image = minimagick::image.open(current_path) image.pages.each_with_index |page, index| minimagick::tool::convert.new |convert| convert.background 'white' convert.flatten convert.density 300 convert.quality 95 convert << page.path convert << "#{carrierwave.root}/#{store_dir}/image-#{index}.jpg" end end end end def set_content_type_jpg(*args) self.file.instance_variable_set(:@content_type, "image/jpg") end # add white list of extensions allowed uploaded. def extension_white_list %w(jpg jpeg gif png doc docx pdf) end end
this generates image-0.jpg
, image-1.jpg
, etc. in correct directory. have no way of referencing images in views, or knowing how many there are. not work when need upload images s3. how can carrierwave handle file storage collection of images, instead of single image?
it looks need add new database column store number of pages. there way make uploader return array of image urls, based on count?
i'm willing switch gem. easier paperclip, shrine, or refile?
with shrine can make each page different version:
class imageuploader < shrine plugin :versions plugin :processing process(:store) |io, context| pdf = io.download versions = {} image = minimagick::image.new(pdf.path) image.pages.each_with_index |page, index| page_image = tempfile.new("version-#{index}", binmode: true) minimagick::tool::convert.new |convert| convert.background 'white' convert.flatten convert.density 300 convert.quality 95 convert << page.path convert << page_image.path end page_image.open # refresh updated file versions[:"page_#{index + 1}"] = page_image end versions end end
assuming have document
model , attached pdf file
attachment field, can retrieve array of pages using hash#values
:
pages = document.file.values pages #=> [...array of pages...] pages.count #=> number of pages
Comments
Post a Comment