我是RoR的新手,我正在尝试开发一个幻灯片共享应用,用户可以通过该应用上传和共享包含幻灯片(每页一张幻灯片)的多页PDF。然后,观看者可以在“显示”页面上查看这些幻灯片。
在幕后我试图将多页pdf转换成几个.png文件(1 pdf页面 - > 1.png文件),然后以轮播/滑块方式显示它们。
这是我的上传者:
# 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
当我尝试上传多页pdf文档时,它会抛出此错误:
行'identify /public/uploads/tmp/1443619784-406-3861/doc.pdf' failed with error:
pdf.pages.each_with_index do |page, index|
我尝试了不同的解决方案来转换文档,但到目前为止,它们都没有对我有用。为什么磨机.pdf文件的运行会失败minimagick的验证?有什么办法可以避免遇到这个问题吗?任何建议将不胜感激!