为什么捆绑器无法在服务器上找到 wkhtmltopdf

why cant bundler find wkhtmltopdf on server

我正在使用 pdfkit gem 并且它正在使用 wkhtmltopdf-binary gem.

中的 wkhtmltopdf 进行开发

在服务器上 pdfkit 失败并出现错误

undefined method `chomp' for nil:NilClass

shared/bundle/ruby/2.1.0/gems/pdfkit-0.8.2/lib/pdfkit/configuration.rb:22:in `wkhtmltopdf'

失败的方法是

def wkhtmltopdf
  @wkhtmltopdf ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
end

经过一些调试后,当从该方法调用时,'bundle exec whkhtmltopdf' 出现 returns 空白。

当从应用程序根目录中的命令行调用时,它给出

/var/www/<app>/shared/bundle/ruby/2.1.0/bin/wkhtmltopdf

我已经尝试从初始化程序中初始化@wkhtmltopdf 的值,如下所示

PDFKit.configure do |config|
  if Rails.env.production?
    config.wkhtmltopdf = "/var/www/<app>/shared/bundle/ruby/2.1.0/bin/wkhtmltopdf"
  end 
  config.default_options = {
    :page_size => 'A4',
  }
end

但我仍然得到同样的错误。即它仍在尝试 运行 'which' 命令并以空白失败。

bundle exec which wkhtmltopdf returns nil,这是导致此错误的原因。

有一个快速修复方法是在 initializer

中重新定义方法
require 'pdfkit'
class PDFKit
  class Configuration
    def wkhtmltopdf
      @wkhtmltopdf ||= `which wkhtmltopdf`.chomp
    end
  end
end

你可以看看表格here