Rails wikedPDF 错误:无法生成 PDF

gFo*_*iva 2 ruby-on-rails

我尝试在 Rails 6 上使用 WickedPdf 但出现此错误

RuntimeError (Failed to execute:
["/home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf", "file:////tmp/wicked_pdf20200531-14069-p9pvre.html", "/tmp/wicked_pdf_generated_file20200531-14069-8mk29k.pdf"]
Error: PDF could not be generated!
 Command Error: /home/guilherme/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf:45:in `<top (required)>': Invalid platform, must be running on Ubuntu 14.04/16.04/18.04 CentOS 6/7/8, Debian 8/9/10, or intel-based macOS (missing binary: /home/guilherme/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf_ubuntu_20.04_amd64). (RuntimeError)
    from /home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf:23:in `load'
    from /home/guilherme/.rbenv/versions/2.7.1/bin/wkhtmltopdf:23:in `<main>'
):
Run Code Online (Sandbox Code Playgroud)

在我的 gemfile 中我添加

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Run Code Online (Sandbox Code Playgroud)

我渲染 pdf 的控制器方法是

  def gera_contrato
    cliente = Cliente.find_by_id(params[:cliente])
    @cnpj = cliente.local.cnpj
    @razao = cliente.local.razao
    @fantasia = cliente.local.fantasia
    @nome = cliente.nome
    @cpf = cliente.cpf
    @rg = cliente.rg
    render pdf: 'contrato', handlers: [:erb], formats: [:html]
  end
Run Code Online (Sandbox Code Playgroud)

我的 html 文件到 pdf 是

<!doctype html>
<html>
  <head>
    <meta charset='utf-8' />
    <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
  </head>
  <body>
    <h1><%= @cnpj %></h1>
    <h1><%= @razao %></h1>
    <h1><%= @fantasia %></h1>
    <h1><%= @nome %></h1>
    <h1><%= @cpf %></h1>
    <h1><%= @rg %></h1>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我已经更改了路径,更改了 gem 版本,并且做了很多事情但没有任何效果,我应该做什么来解决这个错误?

小智 6

嗯,这就是你的问题。错误消息指出:Invalid platform, must be running on Ubuntu 14.04/16.04/18.04。您必须从https://wkhtmltopdf.org/downloads.html安装 Ubuntu 20.04 的版本,并手动将其指向此版本,如下所示

/config/initializers/wicked_pdf.rb

WickedPdf.config = {
  exe_path: '/path/to/correct/version'
}
Run Code Online (Sandbox Code Playgroud)