2011-12-16 189 views
1

我在我的rails应用程序上使用pdfkit(我根据Ryan Bates railscast执行了此操作) 当前它在浏览器中显示pdf。下载pdf,而不是在浏览器中显示它

如何下载pdf?

感谢

编辑:

感谢您的答复!

但是我怎样才能在控制器中创建pdf的数据?

当url有.pdf时,pdf自动显示。例如去这个链接时:

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

它打开了页面的PDF版本会自动在浏览器上(这是一个轨道中间件设置)。

回答

3

尝试使用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 
+0

心中已经编辑这个问题。 – 2011-12-16 18:14:07

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

+0

我编辑了这个问题。 – 2011-12-16 18:14:21

相关问题