wicked_pdf 错误:无法生成 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'
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我也有同样的问题。解决方案是将
wkhtmltopdf-binary
添加到 gem 文件并运行bundle install
。I had the same problem. The solution was to add
wkhtmltopdf-binary
to the gem file and runbundle install
.我在
gemfile
中已经有wkhtmltopdf-binary
,但由于这是在我的本地计算机上而不是在服务器上运行,因此我将此错误留给服务器支持团队来处理。他们检查了 wkhtmltopdf 的路径,他们尝试将简单的 html 转换为 pdf 并且它有效。因此他们尝试运行bundle update
命令,此后 pdf 转换在服务器上也运行良好。我更改了宝石路径,我想这就是问题所在。我发布了我的解决方案,以防其他人也遇到这个问题。I had
wkhtmltopdf-binary
already ingemfile
, 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 abundle 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.对于 Alpine 3.9+,wkhtmltopdf 二进制文件可用,但是我收到空白 PDF 或“无法加载文档”错误 - 尽管在 MacOSX 上本地工作正常。事实证明,您需要为 alpine 构建明确包含字体(至少)
控制器操作
上面的内容在 MacOSX 机器上本地工作,但在基于 ruby alpine 图像的服务器上,如下所示,它失败了,无法加载文档
Dockerfile
甚至更多基本示例因空白 pdf
解决方案
Dockerfile
而失败理想情况下,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
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
even a more basic example failed with a blank pdf
Solution
Dockerfile
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.我也有同样的问题。我安装了
wkhtmltopdf-binary
并且bundle update
也没有帮助。这对我有帮助:重要的是我在 Alpine Linux 上运行它,并且 gem wkhtmltopdf_binary_gem https://github.com/zakird/wkhtmltopdf_binary_gem/issues/53
我是单独安装的wkhtmltopdf 进入系统:
apk add wkhtmltopdf
然后编辑初始化程序以包含二进制路径:
I had the same problem. I had
wkhtmltopdf-binary
installed andbundle 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:
我面临同样的问题,它在本地计算机上工作正常,但部署在服务器上时会引发错误:
错误:无法生成 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
我在使用 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
.如果您在 docker 容器中遇到此问题,很可能您正在使用 Alpine Linux。在这种情况下,
wkhtmltopdf-binary
与 Alpine Linux 不兼容。因此,请添加这些 gem: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:wkhtmltopdf-binary-edge-alpine
is for the Alpine Linux. Everything should work just fine, hopefully????对我来说,上述方法都不起作用,因为我在 arm64 macOS 架构上使用 Docker,并且我不想使用模拟容器。相反,有效的方法是获取我的架构的包并将其安装在容器上,然后使用它而不是 gem。
这是我在安装所有 gems 后在 Dockerfile 中所做的事情:
希望这有帮助
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:
Hope this helps