运行时错误(无法执行)无法使用 wkhtmltopdf-binary wicked_pdf生成 PDF



我在堆栈溢出搜索其他解决方案时上下寻找,似乎都没有合适的答案。所以我会尽量具体。

我们在Ruby 2.0和Rails 4上的Ruby on Rails应用程序中使用wicked_pdf(0.9.7)和wkhtmltopdf-binary(0.9.9.1)。

当我们克隆存储库并启动服务器时,我们的代码在本地运行,但是当我们推送到生产环境(4 个节点)时,它偶尔会起作用。

这是我们得到的错误:

INFO: ***************WICKED***************
Rendered application/index.pdf.haml within layouts/pdf (209.2ms)
Rendered application/_header.pdf.haml within layouts/pdf (1.5ms)
Completed 500 Internal Server Error in 3406ms
FATAL: RuntimeError(Failed to execute:n"/opt/application/releases/5405db831e02eb2987cc06b243333776ce9c34b8ab6db3e58e93f39c3f933621/vendor/bundle/ruby/2.0.0/bin/wkhtmltopdf" -q  --header-html "file:////tmp/wicked_header_pdf20131031-15836-12qkok6.html" --footer-right "[page] of [topage]" --footer-font-size 9    --margin-top 50  --encoding "UTF-8"   "file:////tmp/wicked_pdf20131031-15836-b874ab.html" "/tmp/wicked_pdf_generated_file20131031-15836-1ibji4k.pdf" nError: PDF could not be generated!n Command Error: /usr/bin/env: ruby: No such file or directoryn)
 vendor/bundle/ruby/2.0.0/gems/wicked_pdf-0.9.7/lib/wicked_pdf.rb:69:in `rescue in pdf_from_string'
 vendor/bundle/ruby/2.0.0/gems/wicked_pdf-0.9.7/lib/wicked_pdf.rb:73:in `pdf_from_string'
 vendor/bundle/ruby/2.0.0/gems/wicked_pdf-0.9.7/lib/pdf_helper.rb:63:in `make_pdf'
 vendor/bundle/ruby/2.0.0/gems/wicked_pdf-0.9.7/lib/pdf_helper.rb:76:in `make_and_send_pdf'
 vendor/bundle/ruby/2.0.0/gems/wicked_pdf-0.9.7/lib/pdf_helper.rb:21:in `render_with_wicked_pdf'

以下是应用程序中的相关代码:

宝石文件

gem 'wicked_pdf', '~> 0.9.7'
gem 'wkhtmltopdf-binary', '~> 0.9.9'

Gemfile.lock

wicked_pdf (0.9.7)
  rails
wkhtmltopdf-binary (0.9.9.1)

控制器.rb

def show
    render pdf: file_name,
          layout: 'pdf',
          margin: {
            top: 30
          },
          header: {
            html: {
              template: '_header.pdf.haml'
            }
          },
          footer: {
            right: I18n.t('pdf.page_description'),
            font_size: 9
          },
          encoding: 'UTF-8'
end
  • 我们已经直接在服务器上使用bundle exec(成功)运行了wkhtmltopdf,但是当我们点击页面时,它会在大约3/4的时间内出错500。
  • 我们也在 rails 控制台中成功运行了它。
  • 我们使用基本的 wkhtmltopdf 从网页生成 pdf。
  • 我们目前正在从本地文件生成一个pdf,wicked_pdf将在其中写入一个临时文件并尝试从那里生成文件。

任何意见将不胜感激。

问题是wkhtmltopdf正在执行一个shell脚本来确定要使用的二进制文件版本,但由于apache没有shell(并且有充分的理由),因此失败了。解决方案是显式设置二进制文件。

由于我们的开发、暂存和生产环境各不相同,我们创建了一个在 rails 应用程序中执行的 RBConfig 脚本(无需 shell 访问):

require 'rbconfig'
if RbConfig::CONFIG['host_os'] =~ /linux/
  arch = RbConfig::CONFIG['host_cpu'] == 'x86_64' ? 'wkhtmltopdf_linux_x64' : 'wkhtmltopdf_linux_386'
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
  arch = 'wkhtmltopdf_darwin_386'
else
  raise "Invalid platform. Must be running Intel-based Linux or OSX."
end
WickedPdf.config = {
  exe_path: "#{ENV['GEM_HOME']}/gems/wkhtmltopdf-binary-#{Gem.loaded_specs['wkhtmltopdf-binary'].version}/bin/#{arch}"
}

还有其他几种方法可以做到这一点。例如,在本地开发时,您的用户应该具有 shell 访问权限,在这种情况下,您实际上只需要为生产环境显式设置二进制文件:

if Rails.env.production?
    WickedPdf.config = { ... }
end

或者,正如我们所做的那样,设置我们的 chef 配置,以使用适当的二进制文件设置我们所有的环境。它还让我们确保为生产安装了所需的所有字体。

对于其他

任何遇到所选答案不起作用的人。

我在 Nginx 上运行乘客时遇到了这个问题。Nginx和Passenger 在其路径中运行的用户是红宝石,这引起了一些混乱。

问题原来是/etc/init.d/nginx文件指定了nginx运行时将具有的PATH,结果不包括ruby。

将 ruby 添加到 /etc/init.d/nginx 中的 PATH 变量解决了这个问题。