下载pdf而不是在浏览器中显示它

时间:2011-12-16 17:34:23

标签: ruby-on-rails pdf

我在我的rails应用程序上使用pdfkit(根据Ryan Bates railscast我这样做了) 目前它正在浏览器中显示pdf。

如何下​​载pdf?

谢谢

编辑:

感谢您的回复!

但是如何在控制器中创建pdf的数据?

当网址为.pdf时,pdf会自动显示。例如,当转到此链接时:

http://localhost:3000/home/index.pdf

它在浏览器上自动打开页面的pdf版本(这是一个rails中间件设置)。

2 个答案:

答案 0 :(得分:3)

尝试使用所有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

答案 1 :(得分:0)

假设它是一个简单的机架端点:

class PdfEndpoint
  def call(env)
    [200, {"Content-Type" => "application/pdf", "Content-Disposition" => "attachment", "filename" => "filename.pdf"}, pdf_data]
  end
end

以下Railscast也可能有所帮助:

http://railscasts.com/episodes/151-rack-middleware

相关问题