Download pdf with PDFKit

I have built-in pdfkit with my rails 3 application, adding it to middleware, and it works great. On each page with .pdf , the pdf version of the page is displayed in the browser.

I want the pdf file to load and not display in the browser. How can i do this?

thanks

+4
source share
2 answers

I really ran into this problem. Seeing this question, I went deeper into the problem. its a bit complicated, but pdfkit actually lets you know at least one of the ways that .pdf is requested. this can probably use some cleanup, but this is what we are currently doing:

 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 
+2
source

It's easy

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

with a built-in option (you are viewing a pdf file in a browser) or attachments (a pdf file is loaded)

0
source

Source: https://habr.com/ru/post/1386706/


All Articles