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'
}
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
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.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.I had the same problem. The solution was to add
wkhtmltopdf-binary
to the gem file and runbundle install
.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: