Question

I'm using PDFKit with it's Middlware to render HTML as PDF but it keeps having a TypeError when I try to go to localhost:3000/booklets/1.pdf

can't convert Hash into String

It says the error is in BookletsController#show. This is an excerpt from my booklets_controller.rb

  def show
    @booklet = Booklet.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.pdf {
        html = render_to_string(:action => "show.html.erb", :formats => [:html])
        kit = PDFKit.new(html)
        send_data(kit.to_pdf, :filename => "booklet.pdf", :type => 'application/pdf')
      return
      }
    end
  end

environnment.rb

# Initialize the rails application
Ziin::Application.initialize!

Mime::Type.register "application/pdf", :pdf

excerpt from application.rb

require 'pdfkit'
config.middleware.use "PDFKit::Middleware"

PDFKit.configure do |config|
    config.wkhtmltopdf = { :exe_path => '/usr/local/bin/wkhtmltopdf' }
end
Was it helpful?

Solution

The problem is the exec path you specify. I just replicated the problem on my own machine (ubuntu 12.04), and I fixed it by NOT using the gem install of wkhtmltopdf, which adds some version of wkhtmltopdf that doesn't work.

1.) Look here for how to install wkhtmltopdf (non-gem version) https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF

2.) Remove 'wkhtmltopdf' from your gem file

3.) Find the wkhtmltopdf executable by doing 'which wkhtmltopdf'. Use the path that is returned in the exe_path declaratioin.

4.) Don't know if this next step matters, but I moved:

PDFKit.configure do |config| config.wkhtmltopdf = { :exe_path => "#{PATH}" end

into config/initializers/pdfkit.rb

  • also remove the gem by 'gem uninstall wkhtmltopdf'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top