首页 > 解决方案 > wicked_pdf 在生产服务器中不起作用

问题描述

在本地它可以正常工作,但是当我尝试在服务器上生成 PDF 时,它会抛出:

 RuntimeError (Failed to execute:
    ["/usr/local/bin/wkhtmltopdf", "-q", "file:////tmp/wicked_pdf20180531-994-1x8fbfn.html", "/tmp/wicked_pdf_generated_file20180531-994-1vidmtk.pdf"]
    Error: PDF could not be generated!
Command Error: /usr/bin/env: ruby: No such file or directory

这是my_controller上的内容。

pdf=WickedPdf.new.pdf_from_string(render_to_string('disc_bill',:layout=>false))
  save_file = Rails.root.join("public","bill.pdf")
  File.open(save_file, 'wb') do |file|
    file << pdf
  end

在我的Gemfile

gem 'wicked_pdf', '~> 1.1'
gem 'wkhtmltopdf-binary', '~> 0.12.3.1'

标签: ruby-on-railsruby-on-rails-3wkhtmltopdfwicked-pdfwkhtmltopdf-binary

解决方案


看起来 wkhtmltopdf 命令不在 PATH 中,您可以通过将 WickedPdf.config 更改为来解决此问题

WickedPdf.config = {
  exe_path: Rails.env.production? ? '/path/to/bin/wkhtmltopdf' : '/usr/local/bin/wkhtmltopdf'
}

推荐阅读