title: Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?
body: <h2>Background</h2>
  
  <p>After reading around it seemed to me that <a href="https://github.com/prawnpdf/prawn" rel="noreferrer">Prawn</a> is out and <a href="http://code.google.com/p/wkhtmltopdf/" rel="noreferrer">wkhtmltopdf</a> is in. It also seems like the <a href="https://github.com/pdfkit/PDFKit" rel="noreferrer">PDFKit</a> and <a href="https://github.com/mileszs/wicked_pdf" rel="noreferrer">wicked_pdf</a> gems for Rails are the new cool. So I found a screencast by <a href="http://railscasts.com/" rel="noreferrer">Ryan</a> on <a href="http://railscasts.com/episodes/220-pdfkit" rel="noreferrer">how to use PDFKit</a>. 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).</p>
  
  <h2>Question</h2>
  
  <p><em>When I request a <code>.pdf</code> 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?</em></p>
  
  <h2>What I'm Using</h2>
  
  <ul>
  <li>OS: OSX 10.8.1</li>
  <li>Rails: 3.2.8</li>
  <li>Ruby: 1.9.3</li>
  <li>wkhtmltopdf: 0.11.0_rc1 (although when I run <code>wkhtmltopdf -V</code> it says 0.10.0_rc2)</li>
  <li>qt: 4.8.2</li>
  </ul>
  
  <h2>What I've Done</h2>
  
  <ul>
  <li>used the PDFKit middleware by loading <code>config.middleware.use "PDFKit::Middleware"</code> in my <code>application.rb</code> file.</li>
  <li>included <code>gem 'pdfkit'</code> in my Gemfile and installed it with Bundler</li>
  <li>set the <code>.pdf</code> mime-type in my <code>mime_types.rb</code> initializer with <code>Mime::Type.register_alias "application/pdf", :pdf</code></li>
  <li>added <code>config.threadsafe!</code> to <code>config/environments/development.rb</code> for multiple threads so asset pipeline doesn't conflict with PDF engine</li>
  <li>tested <code>wkhtmltopdf http://www.google.com google.pdf</code> and it generated a PDF of the Google homepage as expected</li>
  <li>tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)</li>
  </ul>
  
  <h2>What it Looks Like</h2>
  
  <p>This is the regular html page rendered by Rails (I've blurred the client details):
  <img src="https://i.stack.imgur.com/XotJR.jpg" alt="enter image description here"></p>
  
  <p>This is the CLI output by Rails when I try to navigate to <code>localhost:3000/some/path.pdf</code>. (the app hangs while waiting for a response):
  <img src="https://i.stack.imgur.com/Etc2j.jpg" alt="enter image description here"></p>
  
  <p>When I finally kill the Rails process with <code>ctrl-c</code> 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):
  <img src="https://i.stack.imgur.com/aiOGz.jpg" alt="enter image description here"></p>
  
  <h2>Conclusions So Far</h2>
  
  <p>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?</p>
  
  <h2>Plea for Help</h2>
  
  <p>How do I determine <em>what</em> exactly the problem is and how do I fix it?</p>
  
  <p>I'll love you if you can help me &lt;3</p>
  
  <h2>Update</h2>
  
  <p>I've also tried using an alternative method of rendering the PDF (with <code>.to_pdf</code>) without the middleware option as follows (doing this I commented out <code>config.middleware.use "PDFKit::Middleware"</code> from my <code>application.rb</code> file):</p>
  
  <pre><code>respond_to do |format|
      format.html
      format.pdf do
          html = '&lt;html&gt;&lt;body&gt;This is a test.&lt;/body&gt;&lt;/html&gt;'
          @pdf = PDFKit.new(html)
  
          send_data @pdf.to_pdf, 
              :filename =&gt; 'whatever.pdf', 
              :type =&gt; 'application/pdf', 
              :disposition =&gt; 'attachment'
      end
  end
  </code></pre>