下载pdf而不是在浏览器中显示 [英] Download pdf instead of showing it in the browser
问题描述
我正在Rails应用程序上使用pdfkit(我根据Ryan Bates railscast 进行了此操作) 目前,它正在浏览器中显示pdf.
I am using pdfkit on my rails application (I did this according to Ryan Bates railscast) Currently it's showing the pdf inside the browser.
如何下载pdf呢?
How can the pdf be downloaded instead ?
谢谢
感谢您的回复!
但是如何在控制器中创建pdf数据?
But how can I create the data of the pdf in the controller?
URL为.pdf
时,会自动显示pdf.例如,当转到此链接时:
The pdf shows up automatically when the url has .pdf
. for example when going to this link:
http://localhost:3000/home/index.pdf
它会在浏览器上自动打开页面的pdf版本(这是rails中间件设置).
It opens a pdf version of the the page automatically on the browser (it's a rails middleware setup).
推荐答案
尝试使用所有rails控制器都具有的 send_data 方法.
Try using the send_data method that all rails controllers have.
类似这样的东西:
# Generate a PDF document with information on the client and return it.
# The user will get the PDF as a file download.
def download_pdf
send_data(generate_pdf, :filename => "my_file.pdf", :type => "application/pdf")
end
这篇关于下载pdf而不是在浏览器中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!