首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用神殿生成PDF预览

用神殿生成PDF预览
EN

Stack Overflow用户
提问于 2019-04-25 11:45:16
回答 1查看 531关注 0票数 1

我正在转换我们的后端文件上传到与神社工作。我成功地获得了图像上传和大拇指钉起来,运行相当容易,但我一直在努力做同样的PDF文件。

上传本身工作,但是,我不能设法为文件生成一个缩略图/预览。我用的是神殿和ImageProcessing和vipslib。

我尝试使用vips提供的缩略图方法,但这似乎只适用于图像文件,我也尝试遵循这个SO,但没有成功。

现在让我给大家介绍一些情况:

这是神殿的初始化器

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

如前所述,上传器,目前是什么破坏,而不是生成预览。该文件的前一个版本如下所示:

代码语言:javascript
运行
复制
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系统上。

谢谢你的许可信息-也会调查的!

EN

Stack Overflow用户

发布于 2019-04-26 04:02:37

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

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

代码语言:javascript
运行
复制
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
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55848570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档