邪恶的PDF gem生成的PDF视图正在被浏览器缓存| Rails 5.2

时间:2018-12-28 03:24:49

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

我正在一个项目中,该项目具有将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

任何建议将不胜感激。

1 个答案:

答案 0 :(得分: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