Oded Harth
Oded Harth

Reputation: 4406

Download pdf using PDFKit

I have integrated pdfkit with my rails 3 application, by adding it to the middleware, and it works great. On every page which has .pdf it shows a pdf version of the page in the browser.

I want the pdf to be downloaded and not shown in the browser. How can I do this?

Thanks

Upvotes: 4

Views: 1764

Answers (2)

chinh
chinh

Reputation: 291

That's easy,

send_data @pdf, :filename => "whatever.pdf",
            :type => "application/pdf",
            :disposition  => "inline" # either "inline" or "attachment"

with inline option ( you view pdf file in browser) or attachment option( pdf file is downloaded)

Upvotes: 0

context
context

Reputation: 21

I actually ran into this issue as well. seeing this question made me dig into the problem even more. its a little tricky but pdfkit does in fact let you know in at least one way the request is for .pdf. this could probably use some clean up but this is what we are doing currently:

respond_with @object do |wants|
  wants.html {
    if request.env["Rack-Middleware-PDFKit"]
      pdf_page = render_to_string :layout => "print_out"
      send_data pdf_page, :filename => "file.pdf"
    else
      render :layout => "print_out"
    end
  }
end

Upvotes: 2

Related Questions