因此,我正在使用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时,是否可以生成预览并保存该预览,直到将其删除或替换?
仅在开发环境中发生。在生产中(Ubuntu 18.04),它就像一个魅力。