无法使用 wicked_pdf 在生产环境中生成 PDF

问题描述 投票:0回答:1

我正在尝试生成 pdf 并将其呈现在生产服务器中。在本地主机中,一切对我来说都很好。我生成 PDF 时没有错误。但在生产中出现此错误。

RuntimeError (Failed to execute: ["/home/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-binary-0.12.6.6/bin/wkhtmltopdf", "--page-size", "A4", "file:////tmp/wicked_pdf20240308-1479705-1jl9481.html", "/tmp/wicked_pdf_generated_file20240308-1479705-112vd26.pdf"] Error: PDF could not be generated!  Command Error: pid 1483292 exit 1 /home/app/.rbenv/versions/2.7.6/lib/ruby/2.7.0/bundler/spec_set.rb:86:in'block in materialize': Could not find rake-13.0.6 in any of the sources (Bundler::GemNotFound)

在Gemfile中我用过

 gem 'rails', '~> 5.1.4'

 gem 'wicked_pdf', '~> 2.8'
 gem 'wkhtmltopdf-binary', '~> 0.12.6.6'

在控制器中

 respond_to do |format|
        format.pdf do
          render pdf: 'certificate',
                 template: 'payments/ripicle_receipt.html.haml',
                 layout: 'payments/accounting_pdf.html.haml',
                 delete_temporary_files: true,
                 page_size: 'A4'

config/initializers/wicked_pdf.rb 文件。我在这个文件中没有设置

在服务器上: ruby 2.7.6p219,Bundler 版本 2.1.4,ubuntu 22.4

我使用

Rails c
,我尝试生成pdf文件。没关系。但是我在浏览器上请求,有错误

更新

我使用“rails s”来启动服务器。这行得通。但是我通过 nginx-passenger 启动服务器,它有这个错误

ruby-on-rails wicked-pdf
1个回答
0
投票

这里是 WickedPDF 和 wkhtmltopdf-binary-gem 的当前维护者。

/home/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-binary-0.12.6.6/bin/wkhtmltopdf
this Ruby 脚本。它调用顶行
!/usr/bin/env ruby
来确定属于
wkhtmltopdf_binary_gem
一部分的哪个确切二进制文件要在您的生产平台上执行。

因此,当您的 Rails 应用程序 shell 调用

wkhtmltopdf
时,它经常运行的用户并不具有与应用程序相同的 Ruby 环境(您的应用程序在 Bundler 管理的环境中运行,但 shell 脚本不需要额外的设置,例如为该 Linux 用户安装和配置
rbenv
,并将其配置为在非交互式 shell 中运行。

解决此问题的一种方法是确保您的服务器也安装了

wkhtmltopdf_binary
作为系统 gem,但可能很难让系统上安装的版本与您的
Gemfile.lock
中的版本同步。

我建议您在所需的实际二进制文件(不是 Ruby binstub)的路径中编写代码,或者将正确的二进制文件解压并复制到应用程序的

bin/
目录中,然后更改 WickedPDF 配置,如下所示:

config/initializers/wicked_pdf.rb

WickedPdf.configure do |config|
  if Rails.env.production?
    # Path to where the actual gem binary is extracted:
    config.exe_path: "#{ENV['GEM_HOME']}/gems/wkhtmltopdf-binary-#{Gem.loaded_specs['wkhtmltopdf-binary'].version}/bin/wkhtmltopdf_debian_10_amd64"

    # Or a copied binary in the /bin/ path:
    config.exe_path = Rails.root.join('bin/wkhtmltopdf_debian_10_amd64').to_s
  end
end

或者,您可以安装

wkhtmltopdf
的系统软件包(如 apt-get install wkhtmltopdf),然后将
exe_path
指向那里,而不是使用 gem。

© www.soinside.com 2019 - 2024. All rights reserved.