ruby-on-rails PDF错误:无法生成PDF

nsc4cvqm  于 2022-12-24  发布在  Ruby
关注(0)|答案(3)|浏览(166)

我尝试在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>'
):

在我的gemfile我添加

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

渲染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

我的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>

我已经改变了路径,改变了gem版本,做了很多事情,但都没有工作,我应该怎么做来解决这个错误?

9cbw7uwe

9cbw7uwe1#

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

WickedPdf.config = {
  exe_path: '/path/to/correct/version'
}
olhwl3o2

olhwl3o22#

备选案文1:

gem 'wkhtmltopdf-binary', '~> 0.12.6.5'

备选案文2:

sudo apt-get install zlib1g fontconfig libxrender1 libfreetype6 libxext6 libx11-6 

sudo apt-get install wkhtmltopdf
piv4azn7

piv4azn73#

我遇到了同样的错误,在做了一些研究后,我发现这个问题是由于wkhtmltopdf-binary,它与Ubuntu 21.04不兼容(在我的情况下)。

相关问题