【问题标题】:How to save PDF preview images in Rails Active Storage?如何在 Rails Active Storage 中保存 PDF 预览图像?
【发布时间】:2020-05-14 14:13:48
【问题描述】:

所以我使用 Active Storage 将 PDF 附加到我的模型中,这一切都很好。作为我项目的一部分,我被要求在一个特定资源页面上显示 PDF 预览,该页面列出了网站上所有可用的 PDF 以供下载 (25+)。我正在使用标准配置 - PopplerPDFPreviewer & MuPDFPreviewer。

irb(main):001:0> Rails.application.config.active_storage.previewers
=> [ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer]

代码:

- @products.each do |product|
  - if product.installation_guide.attached?
    - if product.installation_guide.previewable?
      = image_tag product.installation_guide.preview(resize: '200x200')

这也有效,但是,由于它不断地重新生成图像,我有时会收到超时和过多的请求消息。

@net_http_res   #<Net::HTTPTooManyRequests 429 Too Many Requests readbody=true>

这不好!

现在api.rubyonrails.org 建议使用 .processed.service_url 生成一个稳定的 URL(不是视图),该 URL 将重定向到将由 .service_url返回的短期 URL >。

blob.preview(resize_to_limit: [100, 100]).processed.service_url

好的,但同样,这意味着我必须不断重新生成预览...有没有办法在上传 PDF 时生成预览,并保存该预览直到它被删除或替换?

【问题讨论】:

    标签: ruby-on-rails rails-activestorage


    【解决方案1】:

    这只发生在开发环境中。在生产(Ubuntu 18.04)上,它就像一个魅力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2020-12-01
      • 2020-09-03
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多