11

宝石文件

gem "wicked_pdf"
gem "wkhtmltopdf-binary"

错误:

RuntimeError in CarsController#show

Failed to execute:
/usr/bin/wkhtmltopdf     --print-media-type    -q - - 
Error: PDF could not be generated!
Rails.root: /u/apps/zeepauto/autozeep_update

汽车控制器

def show
    @class_showcar = true
    @class_admin = true
    @car = Car.find(params[:id])
    @search = Car.search(params[:search])
    @cars_see_special = Car.where(:special => "1").order('rand()').limit(3)

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @car }
      format.pdf do
        render :pdf => "#{@car.carname.name}",
               :print_media_type => true
      end
    end
  end

显示.html.erb

<p class="show_links"><%= link_to  url_for(request.params.merge(:format => :pdf)) do %>
  <%= image_tag('/images/printversion.png', :alt => 'Download') %>
</p>

wicked_pdf.erb

# config/initializers/wicked_pdf.rb
WickedPdf.config = {
#  :exe_path => '/var/lib/gems/1.8/bin/wkhtmltopdf'
  :exe_path => '/usr/bin/wkhtmltopdf'
}
4

6 回答 6

22

我有同样的问题。解决方案是添加wkhtmltopdf-binary到 gem 文件并运行bundle install.

gem "wicked_pdf"
gem "wkhtmltopdf-binary"
于 2012-01-18T18:52:48.837 回答
13

wkhtmltopdf-binary已经进去了gemfile,但是因为这是在我的本地计算机上而不是在服务器上工作,所以我把这个错误留给了服务器支持团队来关心..他们检查了 wkhtmltopdf 的路径,他们试图将一个简单的 html 转换为 pdf并且它起作用了..所以他们尝试运行一个bundle update命令,之后pdf转换在服务器上也可以正常工作。我更改了 gem 路径,我想这就是问题所在。我发布了我的解决方案,以防其他人也有这个问题。

于 2011-12-26T15:17:00.537 回答
4

对于 Alpine 3.9+,wkhtmltopdf 二进制文件可用,但是我收到空白 PDF 或“无法加载文档”错误 - 尽管在 MacOSX 上本地工作正常。事实证明,您需要为 alpine 构建明确包含字体(至少)

控制器动作

def show
    respond_to do |format|
      format.html do
        render 'pdfs/templates/my_template.html.erb'
      end

      format.pdf do
        render(
          pdf: "file_name",
          template: 'pdfs/templates/my_template.html.erb',
          disposition: 'inline'
        )
      end
    end
end

以上在 MacOSX 机器上本地工作,但在基于 ruby​​ alpine 映像的服务器上工作,如下所示,它失败并加载文档失败

Dockerfile

FROM ruby:2.6.3-alpine3.10
....
# add wkhtmltopdf for use with wicked_pdf gem
RUN apk --no-cache add wkhtmltopdf
...

即使是更基本的示例也因空白 pdf 而失败

respond_to do |format|
  format.pdf do
    pdf = WickedPdf.new.pdf_from_string('TESTING 123')
    send_data(
      pdf,
      filename: "file_name.pdf",
      type: 'application/pdf',
      disposition: 'inline'
    )
  end
end

解决方案

Dockerfile

FROM ruby:2.6.3-alpine3.10
....
# add wkhtmltopdf for use with wicked_pdf gem
RUN apk --no-cache add \
                  ttf-ubuntu-font-family \
                  wkhtmltopdf
...

理想情况下,Alpine 会在包中包含一个基本字体wkhtmltopdf,但是直到那时我发现这是一个有用的信息来源和/或有利于添加一个 mutistage Docker 文件。

https://github.com/madnight/docker-alpine-wkhtmltopdf/blob/master/Dockerfile

笔记:

alpine 中缺少明确的字体包也可能导致 PDF 转换使用libreoffice失败。特别是从 docx 文件转换时,我们发现了损坏的 PDF。

于 2019-09-25T09:14:18.827 回答
2

我有同样的问题。我已经wkhtmltopdf-binary安装了,bundle update也没有帮助。这对我有帮助:

重要的是我在 Alpine Linux 上运行它,gem wkhtmltopdf_binary_gem 似乎不支持它https://github.com/zakird/wkhtmltopdf_binary_gem/issues/53

我将 wkhtmltopdf 单独安装到系统中:apk add wkhtmltopdf

然后编辑初始化程序以包含二进制路径:

# config/initializers/wicked_pdf.rb
require "wicked_pdf"

WickedPdf.config = {
  exe_path: ENV["WKHTMLTOPDF_BIN"]
}
于 2019-07-10T13:04:21.673 回答
2

我面临同样的问题,它在本地机器上运行良好,但在服务器上部署时会引发错误:
错误:无法生成 PDF!.
就我而言,服务器上缺少一些依赖项。在服务器上使用以下命令安装依赖项。
sudo apt-get install libfontconfig1 libxrender1

于 2020-03-20T12:04:28.800 回答
0

我在使用Ubuntu 20.04时遇到了同样的问题。

我通过使用 wkhtmltopdf-binary version 解决了这个问题0.12.6.1

于 2021-11-08T04:16:13.030 回答