wicked_pdf 错误:无法生成 PDF

发布于 2024-12-22 18:58:47 字数 1332 浏览 2 评论 0原文

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

Gemfile

gem "wicked_pdf"
gem "wkhtmltopdf-binary"

the error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

烦人精 2024-12-29 18:58:47

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

gem "wicked_pdf"
gem "wkhtmltopdf-binary"

I had the same problem. The solution was to add wkhtmltopdf-binary to the gem file and run bundle install.

gem "wicked_pdf"
gem "wkhtmltopdf-binary"

我在 gemfile 中已经有 wkhtmltopdf-binary,但由于这是在我的本地计算机上而不是在服务器上运行,因此我将此错误留给服务器支持团队来处理。他们检查了 wkhtmltopdf 的路径,他们尝试将简单的 html 转换为 pdf 并且它有效。因此他们尝试运行 bundle update 命令,此后 pdf 转换在服务器上也运行良好。我更改了宝石路径,我想这就是问题所在。我发布了我的解决方案,以防其他人也遇到这个问题。

I had wkhtmltopdf-binary already in gemfile, but as this was working on my local computer and not on server, I left this error for the server support team to care off.. they have checked the path to wkhtmltopdf, they tried to convert a simple html to pdf and it worked.. so they tried to run a bundle update command and after this the pdf converting worked fine on server too. I had a gem path changed and I guess this was the problem. I posted my solution in case someone else will have this problem too.

万人眼中万个我 2024-12-29 18:58:47

对于 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 包的基本字体,但是直到此时我发现这是一个有用的信息来源和/或有利于添加多阶段Docker 文件。

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

注意:

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

For Alpine 3.9+ the wkhtmltopdf binary is available, however I was getting either a blank PDF or "Failed to load document" error - despite working fine locally on MacOSX. It turns out that you need to include fonts explicitly for alpine builds (at least)

Controller action

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

The above worked locally on MacOSX machine but on a server based on ruby alpine image, as below, it failed with failed to load document

Dockerfile

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

even a more basic example failed with a blank 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

Solution

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
...

Ideally Alpine would include a basic font with the wkhtmltopdf package, however until such time I found this to be a useful source of info and/or good for adding a mutistage Docker file.

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

NOTE:

lack of an explicit font package in alpine may also cause PDF conversion using libreoffice to fail too. We found corrupt PDFs when converted from docx files in particular.

匿名的好友 2024-12-29 18:58:47

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

I had the same problem. I had wkhtmltopdf-binary installed and bundle update didn't help neither. Here is what helped me:

The important thing is that I run this on Alpine Linux and it does not seem to be supported by gem wkhtmltopdf_binary_gem https://github.com/zakird/wkhtmltopdf_binary_gem/issues/53

I installed separately wkhtmltopdf into the system: apk add wkhtmltopdf

And then edited the initializer to include the binary path:

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

WickedPdf.config = {
  exe_path: ENV["WKHTMLTOPDF_BIN"]
}
森林散布 2024-12-29 18:58:47

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

I'm facing the same issue, it works fine on local machine but when deployed on the server it raises an error:
Error: PDF could not be generated!.
In my case, there are some dependencies missing on the server. Use the below command on the server to install the dependencies.
sudo apt-get install libfontconfig1 libxrender1

慕烟庭风 2024-12-29 18:58:47

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

我通过使用 wkhtmltopdf-binary 版本 0.12.6.1 解决了该问题。

I have the same problem when using Ubuntu 20.04.

I solve the problem by using wkhtmltopdf-binary version 0.12.6.1.

友谊不毕业 2024-12-29 18:58:47

如果您在 docker 容器中遇到此问题,很可能您正在使用 Alpine Linux。在这种情况下,wkhtmltopdf-binary 与 Alpine Linux 不兼容。因此,请添加这些 gem:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
gem 'wkhtmltopdf-binary-edge-alpine', '~> 0.12.5.0'

wkhtmltopdf-binary-edge-alpine 适用于 Alpine Linux。一切都应该很好,希望如此

If you are facing this in the docker container, most probably, you are using the Alpine Linux. In that case, wkhtmltopdf-binary is not compatible with the Alpine Linux. So, add these gems instead:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
gem 'wkhtmltopdf-binary-edge-alpine', '~> 0.12.5.0'

wkhtmltopdf-binary-edge-alpine is for the Alpine Linux. Everything should work just fine, hopefully????

情深缘浅 2024-12-29 18:58:47

对我来说,上述方法都不起作用,因为我在 arm64 macOS 架构上使用 Docker,并且我不想使用模拟容器。相反,有效的方法是获取我的架构的包并将其安装在容器上,然后使用它而不是 gem。
这是我在安装所有 gems 后在 Dockerfile 中所做的事情:

RUN if [ $(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) = "arm64" ]; then \
    wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
    dpkg -i wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
    mv /usr/local/bin/wkhtmltopdf /usr/local/bundle/bin/wkhtmltopdf; \
    rm wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
fi

希望这有帮助

For me none of the above worked, since I am using Docker on an arm64 macOS architecture and i don't want to use emulated containers. What worked instead, was to get the package for my architecture and install it on the container and then use it instead of the gem.
Here is what i did in Dockerfile after installing all the gems:

RUN if [ $(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) = "arm64" ]; then \
    wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
    dpkg -i wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
    mv /usr/local/bin/wkhtmltopdf /usr/local/bundle/bin/wkhtmltopdf; \
    rm wkhtmltox_0.12.6.1-2.bullseye_arm64.deb; \
fi

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文