问题
I am new to RoR and I'm trying to develop a slide sharing app through which users can upload and share a multipage PDFs containing a deck of slides (one slide per page). Viewers can then view these slides on a "show" page.
Behind the scenes I am trying to convert the multipage pdf into several .png files (1 pdf page -> 1.png file), and then display them in a carousel/slider fashion.
Here is my uploader:
# encoding: utf-8
class DocUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# 'public/doc_uploads/'
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :filename
process :generate_png
def generate_png
pdf = MiniMagick::Image.new(self.file.path)
pdf.pages.each_with_index do |page, index|
page.write("page#{index}.png")
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(pdf)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
'doc.pdf' if original_filename
end
end
When I try to upload a multipage pdf document, it throws me this error:
'identify /public/uploads/tmp/1443619784-406-3861/doc.pdf' failed with error:
at the line pdf.pages.each_with_index do |page, index|
I have tried different solutions to converting the document, but none of them have worked for me so far. Why would a run of the mill .pdf file fail minimagick's validation? Is there anything I can do to avoid running into this issue? Any advice will be greatly appreciated!
来源:https://stackoverflow.com/questions/32868326/minimagick-gem-fails-to-convert-multipage-pdf-to-several-png-images