我最近将Rails应用程序从6.0升级到6.1,在升级后,每当我试图生成PDF时,我都会收到以下错误:
RuntimeError: Failed to execute: ["/opt/rubies/ruby-2.6.6/bin/wkhtmltopdf", "--orientation", "Landscape", "--margin-top", "25", "--margin-bottom", "10", "--header-spacing", "3", "--header-html", "file:////tmp/wicked_header_pdf20210506-22237-1tx5wdw.html", "--footer-html", "file:////tmp/wicked_footer_pdf20210506-22237-1fo4wp6.html", "file:////tmp/wicked_pdf20210506-22237-1c7ctor.html", "/tmp/wicked_pdf_generated_file20210506-22237-1c2hpwj.pdf"] Error: PDF could not be generated! Command Error: /opt/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf_centos_7_amd64: error while loading shared libraries: libpng15.so.15: cannot open shared object file: No such file or directory
我尝试通过以下链接执行以下步骤:https://majestic.cloud/how-to-install-wkhtmltopdf-on-amazon-linux/手动安装libpng,但问题仍然存在
# wkhtmltopdf https://majestic.cloud majestic.pdf /opt/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf_centos_7_amd64: error while loading shared libraries: libpng15.so.15: cannot open shared object file: No such file or directory
我的Gemfile是这样的:
ruby '2.6.6'
gem 'rails', '~> 6.1', '>= 6.1.3.1'
gem 'wicked_pdf', '~> 2.0', '>= 2.0.1'
gem 'wkhtmltopdf-binary'
这是我的config/initializers/wicked_pdf.rb
if Rails.env.production?
WickedPdf.config = {
exe_path: '/opt/rubies/ruby-2.6.6/bin/wkhtmltopdf'
}
end
由于该应用程序托管在ElasticBeanstalk上,因此权限已通过ebextensions 设置
container_commands:
03_set_wkhtmltopdf_binary_permissions:
command: "chmod -R 777 /opt/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6.5/bin/"
由于版本不匹配而出现问题。点击此处了解更多信息:https://github.com/zakird/wkhtmltopdf_binary_gem/issues/110
以下对我有效:
Gemfile
gem 'wicked_pdf', '~> 2.0', '>= 2.0.1'
gem 'wkhtmltopdf-binary', '0.12.6'
Gemfile.lock
wicked_pdf (2.1.0)
activesupport
wkhtmltopdf-binary (0.12.6)
wicked_pdf (~> 2.0, >= 2.0.1)
wkhtmltopdf-binary (= 0.12.6)
.ebextensions/commands.config
03_set_wkhtmltopdf_binary_permissions:
command: "chmod -R 777 /opt/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/wkhtmltopdf-binary-0.12.6/bin/"