PDFKit,TypeError“无法将哈希值转换为字符串”

时间:2013-02-01 12:20:13

标签: ruby-on-rails ruby typeerror pdfkit

我正在使用PDFKit和它的Middlware将HTML呈现为PDF但当我尝试转到localhost时它仍然存在TypeError:3000 / booklets / 1.pdf

can't convert Hash into String

它说错误发生在BookletsController#show中。这是我的 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

摘自 application.rb

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

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

1 个答案:

答案 0 :(得分:0)

问题是您指定的exec路径。我刚刚在我自己的机器上复制了这个问题(ubuntu 12.04),并且我通过不使用wkhtmltopdf的gem安装来修复它,它添加了一些不起作用的wkhtmltopdf版本。

1。)在这里查看如何安装wkhtmltopdf(非宝石版) https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF

2。)从你的宝石文件中删除'wkhtmltopdf'

3。)通过执行'which wkhtmltopdf'找到wkhtmltopdf可执行文件。使用exe_path声明中返回的路径。

4。)不知道下一步是否重要,但我感动了:

PDFKit.configure do | config |     config.wkhtmltopdf = {:exe_path => “#{路径}” 端

进入config / initializers / pdfkit.rb

  • 也可以通过'gem uninstall wkhtmltopdf'
  • 删除gem
相关问题