【发布时间】:2019-09-14 20:15:15
【问题描述】:
我正在将我们的后端文件上传转换为与 Shrine 一起使用。我设法让图像上传和缩略图很容易启动和运行,但我一直在努力对 PDF 文件做同样的事情。
上传本身有效,但是,我无法为文件生成缩略图/预览。我将 Shrine 与 ImageProcessing 和 vipslib 一起使用。
我尝试使用 vips 提供的缩略图方法,但这似乎只适用于图像文件,我还尝试遵循此 SO 没有成功。
现在让我给你一些背景:
这是 Shrine 初始化器
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
这是上传者
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 系统上运行。
感谢有关许可证信息 - 也会对此进行调查!
【问题讨论】:
-
听起来您的 libvips 已配置为不支持 PDF。在命令行中尝试
vips pdfload,看看是否有帮助页面。你使用什么平台,什么版本的 libvips,你是怎么安装的,你从哪里得到二进制文件的? -
另外,libvips 使用 libpoppler 来加载 PDF,它是 GPL(libvips 中的所有其他内容都是 LGPL)。您可能需要考虑许可证合规性。
-
感谢您的帮助!我刚刚用模式详细信息更新了问题
-
对不起,我还没有真正阅读你的新上传器,呵呵。您正在使用 mutool,但这不是必需的,如果启用了 poppler,libvips 会透明地处理 PDF。您一直在使用的 SO 答案早于 Shrine 添加 libvips 作为后端。我将添加一些链接的答案。