2011-02-18 107 views
3

我有以下的link_to路徑:如何使用PDFKit向瀏覽器呈現PDF?

http://localhost:3000/letters/showpdf/3

我有我的信控制器的以下自定義操作:

def showpdf 

@letter = Letter.find(params[:id]) 
@redcloth_letter = " <style type='text/css'> </style>" 

@redcloth_letter = @redcloth_letter << RedCloth.new(@letter.body).to_html 

pdf = PDFKit.new(@redcloth_letter) 
pdf.stylesheets << "#{RAILS_ROOT}/public/stylesheets/compiled/pdf.css" 

send_data(pdf.to_pdf, :filename => "pdf.pdf") 

end 

我現在什麼也沒有返回給瀏覽器。

編輯:這是我在紅寶石日誌中看到:

Processing LettersController#showpdf (for 127.0.0.1 at 2011-02-18 15:10:31) [GET] 
    Parameters: {"action"=>"showpdf", "id"=>"3", "controller"=>"letters"} 
    Letter Load (1.5ms) SELECT * FROM "letters" WHERE ("letters"."id" = 3) 
Sending data pdf.pdf 
Completed in 1917ms (View: 6, DB: 2) | 200 OK [http://localhost/letters/showpdf/3] 
+0

你有默:: Type.register 「應用程序/ PDF格式」:在您的environment.rb PDF?我寫了相同的代碼,一切正常。你什麼意思是什麼都不回到瀏覽器?彈出一個空的pdf嗎?或者沒有這樣的東西? – 2011-02-18 19:04:15

+0

@andrew謝謝你回覆我的問題。嗯,我曾經有一個MIME類型,但刪除它。我得到零響應,並沒有彈出。我將添加Mime類型行,並參閱... – Angela 2011-02-18 22:43:57

回答

9

這是我在與PDFkit控制器使用。

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

希望這有助於