这个想法是,我想将 PDF 转换为每页的图像。我尝试了 Stackoverflow 上的其他方法,但无济于事。下面是我的上传文件。
上传者
class PdfUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
#//TODO - Remove converted images when deleting an entry, maybe? if needed?
storage :file
#//TODO - Add images into the same folder. model.id can't be accessed in a custom process.
def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/tmp"
end
def extension_white_list
%w(pdf)
end
def filename
super.chomp(File.extname(super)) + '.png'
end
version :pages do
process :create_pages
process convert: 'png'
end
process convert: 'png'
version :thumb do
process :cover
process :resize_to_fill => [200, 200, Magick::CenterGravity]
process convert: 'png'
end
def cover
manipulate! do |frame, index|
frame if index.zero?
end
end
def create_pages
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
end
PDF 将被转换为每一页的图像。但它不会识别 model.id 并将其存储在该文件夹中。相反,它将存储在上面的目录中。我已经尝试添加以下内容来强制制作文件夹,但通过一点测试似乎不知道model.id?
试图制作目录
def create_pages
Dir.mkdir(store_dir) unless File.exists?(store_dir)
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
非常感谢您的帮助,我已经坚持了一段时间,并且因为沮丧而不得不离开它几天。先感谢您。