【问题标题】:Generate PDF preview with Shrine使用 Shrine 生成 PDF 预览
【发布时间】: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 作为后端。我将添加一些链接的答案。

标签: vips shrine


【解决方案1】:

经过几个小时的努力,昨天我终于把事情搞定了。

最后的解决方案非常简单。我使用了神社提供的版本控制插件,并保留了原始版本。

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
end

【讨论】:

  • 澄清:convert 只是将格式设置为写入,它不会转换图像github.com/janko/image_processing/blob/master/doc/… 您的管道将与ImageProcessing::Vips.source(original).resize_to_limit!(800, 800).saver(interlace: true).convert("jpeg") 一样好工作
  • 那很好,会试一试!非常感谢您抽出宝贵时间提供帮助!
  • 无需更改您的代码——我认为不会有任何区别。不过,您可能想更新这个答案,现在它似乎有点误导。
  • 我根据您的回答编辑了转换参考,希望没问题。
猜你喜欢
  • 2018-05-18
  • 2017-12-05
  • 2014-07-06
  • 2018-03-29
  • 2012-04-22
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 2019-03-30
相关资源
最近更新 更多