为什么 PDFKit/wkhtmltopdf 挂起但在 Rails 应用程序被终止时按预期呈现 PDF? [英] Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

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

问题描述

背景

在我看来,

  • http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav
  • Background

    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).

    Question

    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?

    What I'm Using

    • 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

    What I've Done

    • 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)

    What it Looks Like

    This is the regular html page rendered by Rails (I've blurred the client details):

    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):

    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):

    Conclusions So Far

    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?

    Plea for Help

    How do I determine what exactly the problem is and how do I fix it?

    I'll love you if you can help me <3

    Update

    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
    

    解决方案

    The problem is with wkhtmltopdf itself, specifically, any version after 0.9.9. wkhtmltopdf hangs when run directly from the command-line.

    Steps to correct:

    brew uninstall wkhtmltopdf
    cd /usr/local/Library/Formula/
    git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb
    brew install wkhtmltopdf
    

    Then verify the correct version is installed wkhtmltopdf --version which should yield wkhtmltopdf 0.9.9

    Citations:

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

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

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