I’m using PDFKit (which uses wkhtmltopdf) in an attempt to render a view as pdf within a Rails 3 app.
PDFKit renders with Errno::EPIPE (Broken pipe) pointing to send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf') in my controller show action:
# Controller
def show
respond_to do |format|
format.html { render }
format.pdf do
html = render_to_string(:layout => false , :action => "show.html.haml")
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "invoice.pdf", :type => 'application/pdf')
return # to avoid double render call
end
end
end
# Gemfile
...
gem 'pdfkit'
gem 'wkhtmltopdf'
...
I know wkhtmltopdf is not the source of this error as wkhtmltopdf public/404.html tmp/404.pdf from within Rails.root works as expected.
I’m using an example from jonathanspies.com after using the middleware failed in the same manner.
# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware
After trying it in a fresh Rails 3 app, I get the following error:
command failed: "~/.rvm/gems/ree-1.8.7-2011.01@blog/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-right" "0.75in" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-"
Running the command manually and usage screen is displayed, looking at the –quiet option it is easy to see its supposed to be –quit
Change /lib/pdfkit/pdfkit.rb:35 to the following and everything works as expected (with middleware too).
args << '--quit'
So, once again, I’ve solved my problem in the act of writing the question to get help (always pays to include detail). I submitted a pull request which corrects the spelling mistake (always a silly mistake which gets unnoticed). Hope nobody minds me posting anyways.
Regarding changing the quiet argument to quit.
This change is only valid if you are using the wkhtmltopdf gem which uses a very old version of the wkhtmltopdf binary.
With the wkhtmltopdf gem
With the latest binary I have installed
The spelling mistake exists in the old binary that comes with the wkhtmltopdf gem. I would suggest you monkey patch pdfkit with an initialize or something based on if you included the wkhtmltopdf gem or not.
I would also accept a pull request that made pdfkit aware of the version of wkthtmltopdf it was running against and conditionally switched that argument.