wicked_pdf Error: PDF could not be generated
Asked Answered
E

8

14

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'
}
Equal answered 24/12, 2011 at 20:0 Comment(3)
Do you actually have the wkhtmltopdf binary installed in /usr/bin? Can you drop to the shell and execute "wkhtmltopdf google.com google.pdf"?Gilbertine
yes. thank you, we solved it by just running a bundle update command. I had a gem in gemfile with path changed and we think that was the problem.Equal
I still have the same problem in OSX, adding 'wkhtmltopdf-binary' to the gemfile didn't work for me. The generation of pdfs works, just not in Rails. It does work on the production server though.Chervonets
E
14

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.

Equal answered 26/12, 2011 at 15:17 Comment(0)
E
24

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"
Endoparasite answered 18/1, 2012 at 18:52 Comment(0)
E
14

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.

Equal answered 26/12, 2011 at 15:17 Comment(0)
U
4

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.

Ut answered 25/9, 2019 at 9:14 Comment(1)
Could you share a bit more on what kind of error you are having? I just posted this question, #60335067, which might be answered with the solution you posted here.Hone
A
3

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"]
}
Ankle answered 10/7, 2019 at 13:4 Comment(0)
I
2

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

Isabeau answered 20/3, 2020 at 12:4 Comment(0)
W
0

I have the same problem when using Ubuntu 20.04.

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

Windward answered 8/11, 2021 at 4:16 Comment(0)
B
0

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😂

Berrios answered 2/8, 2022 at 15:13 Comment(0)
C
0

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

Canard answered 14/6, 2023 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.