如何使用 PDFKit 向浏览器呈现 PDF?

发布于 2024-10-18 05:49:45 字数 1000 浏览 1 评论 0原文

我有以下 link_to 路径:

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

我目前没有返回到浏览器。

编辑:这是我在 ruby​​ 日志中看到的内容:

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]

I have the following link_to path:

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

I have the following custom action in my letters controller:

 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

I currently get nothing returning to the browser.

EDIT: This is what I see in the ruby logs:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2024-10-25 05:49:45

这就是我在带有 PDFkit 的控制器中使用的内容。

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

希望这有帮助

This is what I use in my controller with PDFkit.

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

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文