为什么在Rails应用被杀死时,PDFKit/wkhtmltopdf会挂起,但是却按预期呈现PDF? [英] Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

查看:102
本文介绍了为什么在Rails应用被杀死时,PDFKit/wkhtmltopdf会挂起,但是却按预期呈现PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读之后,在我看来大虾消失了,而 PDFKit wicked_pdf 都是Rails的新宠.因此,我在 Ryan 的截屏视频"rel =" noreferrer>如何使用PDFKit .我安装了所有内容,在CLI上没有问题地测试了wkhtmltopdf,摆弄了Rails设置以运行多个进程,因此资产管道可以正常工作,并且一切似乎都很好,除了我仍然停留在进程的最后(实际上是获取PDF)服务器的响应).

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
    通过在我的application.rb文件中加载config.middleware.use "PDFKit::Middleware"
  • 使用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呈现了预期的效果)
  • used the PDFKit middleware by loading config.middleware.use "PDFKit::Middleware" in my application.rb file.
  • included gem 'pdfkit' in my Gemfile and installed it with Bundler
  • set the .pdf mime-type in my mime_types.rb initializer with Mime::Type.register_alias "application/pdf", :pdf
  • added config.threadsafe! to config/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

引文:

  1. https://github.com/mileszs/wicked_pdf/issues/110
  2. http://wearepandr.com/blog/article/homebrew-and -installing-old-package-versions#blog_nav
  1. https://github.com/mileszs/wicked_pdf/issues/110
  2. http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav

这篇关于为什么在Rails应用被杀死时,PDFKit/wkhtmltopdf会挂起,但是却按预期呈现PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆