wicked_pdf错误:无法生成PDF

时间:2011-12-24 20:00:39

标签: ruby-on-rails wicked-pdf

的Gemfile

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

cars_controller

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

show.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'
}

5 个答案:

答案 0 :(得分:19)

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

gem "wicked_pdf"
gem "wkhtmltopdf-binary"

答案 1 :(得分:8)

wkhtmltopdf-binary已经gemfile,但由于这是在我的本地计算机而不是在服务器上工作,我将此错误留给服务器支持团队来关注..他们检查了wkhtmltopdf的路径,他们试图将一个简单的html转换为pdf并且它工作..所以他们试图运行bundle update命令,之后pdf转换在服务器上工作正常。我改变了宝石路径,我想这就是问题所在。我发布了我的解决方案,万一其他人也会遇到这个问题。

答案 2 :(得分:0)

我有同样的问题。我已经安装了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"]
}

答案 3 :(得分:0)

对于Alpine 3.9 +,wkhtmltopdf二进制文件可用,但是我在MacOSX上可以正常工作,但出现空白PDF或“无法加载文档”错误。事实证明,您至少需要为高山建筑明确包含字体

控制器动作

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软件包中包含一种基本字体,但是直到那时,我发现这是有用的信息来源,并且/或者对于添加多阶段Docker文件很有用。

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

注意:

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

答案 4 :(得分:0)

我遇到了同样的问题,它在本地计算机上工作正常,但是当部署在服务器上时会引发错误:
错误:无法生成PDF!
就我而言,服务器上缺少一些依赖项。在服务器上使用以下命令安装依赖项。
sudo apt-get install libfontconfig1 libxrender1

相关问题