Rails - pdfkit/pdfkit GitHub Wiki

How to cache the PDF using Rails page caching and PDFKit

  1. Disable the PDFKit middleware so that your controller handles the generation of the PDF.

  2. Register PDF as a mime-type in environment.rb Mime::Type.register "application/pdf", :pdf

  3. In your controller, cache pages just like normal with caches_page :show

  4. create a format.pdf call in your respond_to block and init a new PDFKit object, pass in the url to the current action and call to_pdf on it.

     respond_to do |format|
       format.html # show.html.erb
       format.pdf { render :text => PDFKit.new( post_url(@post) ).to_pdf }
     end
    

Now in your cache directory you'll create two cache files - one .html and one .pdf.