
Upload pdf and convert all pages to images one by one and store against one db record.
Here is code.
require 'RMagick'
class PdfUploader < Shrine
include ImageProcessing::MiniMagick
plugin :activerecord
plugin :determine_mime_type
plugin :logging, logger: Rails.logger
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :versions, names: [:original,:pngs]
plugin :add_metadata
plugin :processing
add_metadata :pages do |io, context|
PDF::Reader.new(io.path).page_count
end
Attacher.validate do
validate_max_size 50.megabytes, message: 'is too large (max is 2 MB)'
validate_mime_type_inclusion ['application/pdf']
end
def process(io, context)
case context[:phase]
when :store
pngs = if io.mime_type == 'application/pdf'
tempPdf = io.download
pdf = Magick::ImageList.new(tempPdf.path)
pdf.each_with_index do |page_img, i|
path = Rails.root.join('tmp',"#{i}_pdf_page.png").to_s
image = page_img.write path
end
end
# raise pngs
{ original: io,pngs: pngs}
end
end
end
Please help me whats wrong?
Thanks