1

我正在将我们的后端文件上传转换为与 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 系统上运行。

感谢有关许可证信息 - 也会对此进行调查!

4

1 回答 1

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
于 2019-04-26T04:02:37.203 回答