ruby-on-rails – 运行时错误(无法执行)无法使用wkhtmltopdf-bin
我看起来在stackoverflow上寻找其他解决方案的高低,并且它们似乎都没有合适的答案.所以我会尝试尽可能具体.
我们在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' 以下是应用程序中的相关代码: 的Gemfile 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) controller.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. 任何意见都将不胜感激. 解决方法
问题是wkhtmltopdf正在执行一个shell脚本来确定要使用的二进制文件的版本失败,因为apache没有shell(并且有充分的理由).解决方案是明确设置二进制文件.
由于我们的dev,staging和prod环境各不相同,我们创建了一个在rails app中执行的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 或者,正如我们所做的那样,设置我们的主厨配置,以使用适当的二进制文件设置所有环境.它还让我们确保我们需要的所有字体都已安装用于生产. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |