我正在转换我们的后端文件上传到与神社工作。我成功地获得了图像上传和大拇指钉起来,运行相当容易,但我一直在努力做同样的PDF文件。
上传本身工作,但是,我不能设法为文件生成一个缩略图/预览。我用的是神殿和ImageProcessing和vipslib。
我尝试使用vips提供的缩略图方法,但这似乎只适用于图像文件,我也尝试遵循这个SO,但没有成功。
现在让我给大家介绍一些情况:
这是神殿的初始化器
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/google_cloud_storage"
Shrine.storages = {
cache: Shrine::Storage::GoogleCloudStorage.new(bucket: ENV['CACHE_BUCKET']),
store: Shrine::Storage::GoogleCloudStorage.new(bucket: ENV['STORE_BUCKET'])
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :determine_mime_type这就是Uploader
class DocumentUploader < Shrine
require 'vips'
def generate_location(io, context)
"documents/#{Time.now.to_i}/#{super}"
end
plugin :processing
# plugin :processing # allows hooking into promoting
# plugin :versions # enable Shrine to handle a hash of files
# plugin :delete_raw # delete processed files after uploading
# plugin :determine_mime_type
#
process(:store) do |io, context|
preview = Tempfile.new(["shrine-pdf-preview", ".pdf"], binmode: true)
begin
IO.popen *%W[mutool draw -F png -o - #{io.path} 1], "rb" do |command|
IO.copy_stream(command, preview)
end
rescue Errno::ENOENT
fail "mutool is not installed"
end
preview.open # flush & rewind
versions = { original: io }
versions[:preview] = preview if preview && preview.size > 0
versions
end
end如前所述,上传器,目前是什么破坏,而不是生成预览。该文件的前一个版本如下所示:
class DocumentUploader < Shrine
require 'vips'
def generate_location(io, context)
"documents/#{Time.now.to_i}/#{super}"
end
plugin :processing
# plugin :processing # allows hooking into promoting
# plugin :versions # enable Shrine to handle a hash of files
# plugin :delete_raw # delete processed files after uploading
# plugin :determine_mime_type
#
process(:store) do |io, context|
thumb = Vips::Image.thumbnail(io.metadata["filename"], 300)
thumb
end
end我在网上很少看到关于这个主题的文档。
更新:回答问题
命令vips pdfload发出使用信息,它确实表示将使用libpoppler加载PDF。
我直接从下载页面安装了tar文件,版本是8.7.0,运行在Debian系统上。
谢谢你的许可信息-也会调查的!
发布于 2019-04-26 04:02:37
经过几个小时的努力,昨天我终于把事情做好了。
最后的解决方案非常简单。我使用了神社提供的版本控制插件,并将原始版本保存在那里。
class DocumentUploader < Shrine
include ImageProcessing::Vips
def generate_location(io, context)
"documents/#{Time.now.to_i}/#{super}"
end
plugin :processing # allows hooking into promoting
plugin :versions # enable Shrine to handle a hash of files
plugin :delete_raw # delete processed files after uploading
process(:store) do |io, context|
versions = { original: io } # retain original
io.download do |original|
pipeline = ImageProcessing::Vips.source(original)
pipeline = pipeline.convert("jpeg").saver(interlace: true)
versions[:large] = pipeline.resize_to_limit!(800, 800)
versions[:medium] = pipeline.resize_to_limit!(500, 500)
versions[:small] = pipeline.resize_to_limit!(300, 300)
end
versions # return the hash of processed files
end
endhttps://stackoverflow.com/questions/55848570
复制相似问题