为什么在Rails应用被杀死时,PDFKit/wkhtmltopdf会挂起,但是却按预期呈现PDF? [英] Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?
问题描述
在阅读之后,在我看来大虾消失了,而 PDFKit 和 wicked_pdf 都是Rails的新宠.因此,我在
After reading around it seemed to me that Prawn is out and wkhtmltopdf is in. It also seems like the PDFKit and wicked_pdf gems for Rails are the new cool. So I found a screencast by Ryan on how to use PDFKit. I installed everything, tested wkhtmltopdf on the CLI with no problems, fiddled around with Rails settingsto run multiple processes so the asset pipeline works, and all seemed good, except I'm still stuck at the very end of the process (actually getting the PDF response from the server).
当我请求视图的.pdf
版本(我使用PDFKit中间件选项)时,我的浏览器就坐在那儿等待响应,但是当我终止Rails处理PDF时,我就期望了要获取,然后才在我的浏览器窗口中弹出.有什么作用?
When I request a .pdf
version of my view (I'm using the PDFKit Middleware option) my browser just sits there waiting for a response, but as soon as I kill the Rails process the PDF I expected to get only then pops up in my browser window. What gives?
- OS:OSX 10.8.1
- 路轨:3.2.8
- Ruby:1.9.3
- wkhtmltopdf:0.11.0_rc1(尽管当我运行
wkhtmltopdf -V
时它显示为0.10.0_rc2) - qt:4.8.2
- OS: OSX 10.8.1
- Rails: 3.2.8
- Ruby: 1.9.3
- wkhtmltopdf: 0.11.0_rc1 (although when I run
wkhtmltopdf -V
it says 0.10.0_rc2) - qt: 4.8.2
-
通过在我的
- 使用PDFKit中间件.
- 将
gem 'pdfkit'
包含在我的Gemfile中,并与Bundler一起安装 - 使用
Mime::Type.register_alias "application/pdf", :pdf
在我的mime_types.rb
初始值设定项中设置.pdf
MIME类型 - 为多个线程将
config.threadsafe!
添加到了config/environments/development.rb
,因此资产管道不会与PDF引擎冲突 - 测试了
wkhtmltopdf http://www.google.com google.pdf
,并按预期生成了Google主页的PDF - 尝试将PDFKit替换为wicked_pdf,并遇到了相同的问题(挂起,但是当Rails进程被终止时,PDF呈现了预期的效果)
application.rb
文件中加载config.middleware.use "PDFKit::Middleware"
来- used the PDFKit middleware by loading
config.middleware.use "PDFKit::Middleware"
in myapplication.rb
file. - included
gem 'pdfkit'
in my Gemfile and installed it with Bundler - set the
.pdf
mime-type in mymime_types.rb
initializer withMime::Type.register_alias "application/pdf", :pdf
- added
config.threadsafe!
toconfig/environments/development.rb
for multiple threads so asset pipeline doesn't conflict with PDF engine - tested
wkhtmltopdf http://www.google.com google.pdf
and it generated a PDF of the Google homepage as expected - tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)
这是Rails渲染的常规html页面(我已经模糊了客户端的详细信息):
This is the regular html page rendered by Rails (I've blurred the client details):
这是我尝试导航到localhost:3000/some/path.pdf
时Rails的CLI输出. (应用程序在等待响应时挂起):
This is the CLI output by Rails when I try to navigate to localhost:3000/some/path.pdf
. (the app hangs while waiting for a response):
当我最终用ctrl-c
杀死Rails进程时,PDF终于在浏览器中显示出来,正如我所期望的那样(CSS和HTML正确呈现,因此资源似乎可以正常加载):
When I finally kill the Rails process with ctrl-c
the PDF finally shows up in the browser as I expected to see it (CSS and HTML rendered properly, so assets seem to load fine):
将wicked_pdf替换为PDFKit并获得相同的结果似乎使我认为问题不在于那些库,而是与我的开发环境有关.但是wkhtmltopdf在命令行中运行良好,因此让我认为它和QT都在做自己的工作.问题必须出在Rails中.也许我没有正确配置某些东西?
Swapping PDFKit for wicked_pdf and getting the same results seems to make me think the problem isn't with those libraries, but something to do with my development environment. But wkhtmltopdf runs fine off the command line, so that makes me think that it and QT are doing their job. The problem must be in Rails. Maybe I'm not configuring something properly?
如何确定确切的问题是什么?如何解决?
How do I determine what exactly the problem is and how do I fix it?
如果您能帮助我< 3
I'll love you if you can help me <3
我还尝试过使用一种不带中间件选项的替代方法来呈现PDF(使用.to_pdf
),如下所示(为此,我从application.rb
文件中注释了config.middleware.use "PDFKit::Middleware"
):
I've also tried using an alternative method of rendering the PDF (with .to_pdf
) without the middleware option as follows (doing this I commented out config.middleware.use "PDFKit::Middleware"
from my application.rb
file):
respond_to do |format|
format.html
format.pdf do
html = '<html><body>This is a test.</body></html>'
@pdf = PDFKit.new(html)
send_data @pdf.to_pdf,
:filename => 'whatever.pdf',
:type => 'application/pdf',
:disposition => 'attachment'
end
end
推荐答案
问题出在wkhtmltopdf本身,特别是0.9.9之后的任何版本.直接从命令行运行时,wkhtmltopdf会挂起.
The problem is with wkhtmltopdf itself, specifically, any version after 0.9.9. wkhtmltopdf hangs when run directly from the command-line.
纠正步骤:
brew uninstall wkhtmltopdf
cd /usr/local/Library/Formula/
git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb
brew install wkhtmltopdf
然后验证是否安装了正确的版本wkhtmltopdf --version
,应该会产生wkhtmltopdf 0.9.9
Then verify the correct version is installed wkhtmltopdf --version
which should yield wkhtmltopdf 0.9.9
引文:
- https://github.com/mileszs/wicked_pdf/issues/110
- http://wearepandr.com/blog/article/homebrew-and -installing-old-package-versions#blog_nav
- https://github.com/mileszs/wicked_pdf/issues/110
- http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav
这篇关于为什么在Rails应用被杀死时,PDFKit/wkhtmltopdf会挂起,但是却按预期呈现PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!