下载pdf而不是在浏览器中显示它
我在我的rails应用程序上使用pdfkit(根据Ryan Bates railscast我这样做)目前它在浏览器中显示pdf。
如何下载pdf?
谢谢
编辑:
谢谢你的回复!
但是如何在控制器中创建pdf的数据呢?
当url为.pdf
时,pdf会自动显示。 例如,当转到此链接时:
http://localhost:3000/home/index.pdf
它会在浏览器上自动打开页面的pdf版本(这是一个rails中间件设置)。
尝试使用所有rails控制器都具有的send_data方法。
像这样的东西:
# 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
假设它是一个简单的机架端点:
class PdfEndpoint def call(env) [200, {"Content-Type" => "application/pdf", "Content-Disposition" => "attachment", "filename" => "filename.pdf"}, pdf_data] end end
以下Railscast也可能有所帮助: