【问题标题】:Wicked PDF gem generated PDF views are being cached by browsers | Rails 5.2浏览器正在缓存 Wicked PDF gem 生成的 PDF 视图 |导轨 5.2
【发布时间】:2018-12-28 03:24:49
【问题描述】:

我正在开发一个具有将index 视图导出为 PDF 的功能的项目。这一切都与Wicked PDF gem 完美配合,但是,在生产中,生成的 PDF 会被浏览器缓存,因此可能会过期。刷新 PDF 视图确实会获取最新数据,但简单地导航到 PDF 视图似乎只使用浏览器缓存的文档。有没有办法防止 PDF 视图被浏览器缓存?

控制器

class InvoicesController < ApplicationController
  # GET /invoices
  def index
    @invoices = Invoice.all

    respond_to do |format|
      format.html
      format.pdf do
        render pdf: "Invoices_#{Time.current.strftime("%Y_%m_%d_at_%H_%M")}",
               template:       'invoices/index',
               show_as_html:   params.key?('debug'),
               title:          "Invoices_#{Time.current.strftime("%Y_%m_%d_at_%H_%M")}", # otherwise first page title is used
               orientation:    :landscape,
               margin:         { top:    15,                     # default 10 (mm)
                                 bottom: 15,
                                 left:   15,
                                 right:  45 },
               footer:         { left: "Extracted: #{Time.current.to_formatted_s(:date_at_time)}",
                                 right: "Page [page] of [topage]" }
      end
    end
  end
end

任何建议将不胜感激。

【问题讨论】:

    标签: ruby-on-rails pdf browser-cache wicked-pdf


    【解决方案1】:

    您只需清除该页面的缓存即可完成

     class InvoicesController < ApplicationController
        before_action :set_cache_headers
    
          private
    
          def set_cache_headers
            response.headers["Cache-Control"] = "no-cache, no-store"
            response.headers["Pragma"] = "no-cache"
            response.headers["Expires"] = Time.now
          end
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2016-12-03
      • 2014-06-10
      • 1970-01-01
      • 2013-10-14
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多