[Ruby on Rails] PDFKit doesn't work with NoMethodError (undefined method `chomp' for nil:NilClass): error

Tadashi Shigeoka ·  Thu, March 28, 2013

I was using PDFKit in Ruby on Rails and ran into a situation where it wouldn’t work on CentOS.

NoMethodError (undefined method `chomp' for nil:NilClass):
  pdfkit (0.5.3) lib/pdfkit/configuration.rb:20:in `wkhtmltopdf'
  pdfkit (0.5.3) lib/pdfkit/pdfkit.rb:31:in `initialize'
  pdfkit (0.5.3) lib/pdfkit/middleware.rb:21:in `new'
  pdfkit (0.5.3) lib/pdfkit/middleware.rb:21:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `catch'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'

Even after trying to fix it by referring to the link below, I got a RuntimeError pdfkit (0.5.3) lib/pdfkit/pdfkit.rb:73:in `to_pdf’ error.

NoMethodError · Issue #82 · pdfkit/pdfkit

There were two causes. The first was that Basic authentication was set up on Apache.

The second was resolved by adding a path to /usr/local/bin.

The method to add the path is as follows:

First, create and save the following content in /usr/local/bin/ruby_wrapper:

#!/bin/sh
export PATH="$PATH:/usr/local/bin" 
exec "/usr/local/bin/ruby" "$@" 

Next, change the Ruby path used by Passenger to ruby_wrapper in Apache’s httpd.conf:

PassengerRuby /usr/local/bin/ruby_wrapper

That’s all from the Gemba.