使用Shrine生成PDF预览

时间:2019-04-25 11:45:17

标签: vips shrine

我正在将后端文件上传转换为可与Shrine一起使用。我设法使图像上传和缩略图创建起来非常容易,并且很容易运行,但是我一直在努力对PDF文件进行同样的操作。

上传本身有效,但是,我无法为文件生成缩略图/预览。我正在使用Shrine和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

这是上载器

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文件,并且版本是在Debian系统上运行的8.7.0。

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

1 个答案:

答案 0 :(得分:1)

经过数小时的奋斗,昨天我终于把事情做好了。

最后的解决方案非常简单。我使用了shrine提供的版本控制插件,并将原始版本保存在其中。

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