Wicked_pdf and wkhtmltopdf-binary gem on Ubuntu 10.04 #53

Closed
brendon opened this Issue Sep 29, 2011 · 18 comments

Projects

None yet

10 participants

@brendon
Contributor
brendon commented Sep 29, 2011

Hi there, I have no problem using wkhtmltopdf-binary in development on my mac but when trying to use it on my production server (Ubuntu 10.04) where the app is deployed via capistrano and bundle is managing the gems (Rails 3.1) I get the following error:

RuntimeError (Failed to execute:
/home/deployer/apps/nzswarranty/shared/bundle/ruby/1.9.1/bin/wkhtmltopdf       -q - - 
Error: PDF could not be generated!):
  app/controllers/admin/products_controller.rb:52:in `certificate_example'

I'm not sure if the binary in the folder mentioned above is the one I want or whether I should be going for:

/apps/nzswarranty/shared/bundle/ruby/1.9.1/gems/wkhtmltopdf-binary-0.9.5.3/bin/wkhtmltopdf

Any help would be appreciated :D

Update

Thought it might be because the -binary gem binaries are 32 bit and this is a 64bit install of Ubuntu so I removed the gem and did apt-get install wkhtmltopdf and now the error message is still the same just using the path of the system wkhtmltopdf:

RuntimeError (Failed to execute:
/usr/bin/wkhtmltopdf       -q - - 
Error: PDF could not be generated!):
  app/controllers/admin/products_controller.rb:52:in `certificate_example'
@brendon
Contributor
brendon commented Sep 29, 2011

Ended up using the static binary from the wkhtmltopdf site and committing it with my project. Had to install the msttcorefonts package on Ubuntu to get Arial as a font (which seems to render better kerning-wise).

@brendon brendon closed this Sep 29, 2011
@wanchic
wanchic commented Oct 12, 2011

I confirm this problem too (Ubuntu 11.04). You will experience this with any version of Ubuntu, and probably with any distro flavor as well (including Macs). I wasn't too convinced that one wkhtmltopdf of the same version would be any different than google's binary compile, but there is, and here is why.

Working a little deeper, I tried running wkhtmltopdf at command prompt and it seemed fine. But, after running wkhtmltopdf with the -q - - attributes I received this error:
Cannot connect to X server

Well, of course, I'm not going to install "X Server" on a server machine that doesn't need it. Do I? I discovered this link (http://blog.structuralartistry.com/post/2327213260/installing-wkhtmltopdf-on-ubuntu-server) which explains more in depth on this issue. This also explains why this will work in development mode, but not production. In conclusion google's static compiled version of wkhtmltopdf has all the needed X Server libraries bundled in.

My suggestion when using production mode:

  1. First remove the wkhtmltopdf package from ubuntu and/or the wkhtmltopdf-binary gem.
  2. Download the static binary (i386/amd64) from google's site. (http://code.google.com/p/wkhtmltopdf/downloads/list)
  3. Install it under /usr/local/bin (eg. /usr/local/bin/wkhtmltopdf-amd64)
  4. Create a soft link in /usr/bin with the simple label "wkhtmltopdf" so future programs will execute with ease (eg. ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf)
  5. Restart apache2 and test your app.

Additional website ideas that maybe of help:
http://www.lucidity.ie/blog/33-generating-pdfs-using-wicked-pdf-in-ruby-on-rails
https://github.com/mileszs/wicked_pdf/wiki/WickedPdf-usage

@brendon
Contributor
brendon commented Oct 13, 2011

Thanks for that :) I found it was easier to just commit it to the project rather than put it into /usr/local/bin though the latter is probably more correct. If wkhtmltopdf-binary had a github repo that I could find, I'd be tempted to patch it to serve the static binaries and add the 64bit binary at the same time. :)

@wanchic
wanchic commented Jan 9, 2012

Installing the app on Ubuntu 11.10 server, now wicked_pdf is not working again. Except I get no errors when I manually run wkhtmltopdf. using wicked_pdf 0.7.2 and wkhtmltopdf 0.9.9

Error in production.log is:
Rendered reports/referrals.pdf.erb (1.1ms)
Completed 500 Internal Server Error in 223ms

RuntimeError (Failed to execute:
/usr/bin/wkhtmltopdf -q - -
Error: PDF could not be generated!):

@wanchic
wanchic commented Jan 9, 2012

WORKING AGAIN - Additional work for Ubuntu 11.x?

Step 1: Install wkhtmltopdf from apt-get
apt-get install wkhtmltopdf

You are not going to use this version, but you need to install it (for now) so that it loads the additional dependencies needed for "Ubuntu" and wkhtmltopdf.

Step 2: Disable wkhtmltopdf from /usr/bin
mv /usr/bin/wkhtmltopdf /usr/bin/wkhtmltopdf_tmp

Step 3: Now move up to my post on Oct 12, 2011, and continue as usual. (Downloading the static bin 0.9.9 from google, etc)

NOTE: I will try back again later to get those additional dependency packages needed for wkhtmltopdf under Ubuntu.

@Mr-Nizzle

I'm still getting the error while on server after following @snasir suggestion
`
RuntimeError (Failed to execute:
"/usr/local/bin/wkhtmltopdf" -q - -

`

@mihaj
mihaj commented Apr 2, 2012

Hi. I am also getting:

RuntimeError (Failed to execute:
"/usr/local/bin/wkhtmltopdf" -q - -
Error:PDF cound not be generated.

I have wkhtmltopdf installed under /usr/local/bin/wkhtmltopdf and rails app configured to point to this binary.

@Mr-Nizzle

@mihaj checkout my answer on stack overflow about this

http://stackoverflow.com/a/9687535/295228

'
Fixed removing system installed wkhtmltopdf and using the binary of wkhtmltopdf:

  1. Uninstall the wkhtmltopdf package: apt-get remove wkhtmltopdf --purge
  2. (in usr/local/bin) sudo curl -C - -O http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
  3. (in usr/local/bin) sudo tar -xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
  4. (in usr/local/bin) ln -s wkhtmltopdf-amd64 wkhtmltopdf
  5. In your initializer WickedPdf.config = { :exe_path => "/usr/local/bin/wkhtmltopdf" }
    '

I found it has something to do witht the X server because the newer versions of wkhtmltopdf uses the server to generate the pdf you need to use 0.9.9 from the binary to get it working right.

@mihaj
mihaj commented Apr 2, 2012

Hi there! This works! Like you said, 0.9.9 version is the way to go in this case.

Thanks!

@priyank-gupta

Hey, This works like a charm for me thanks.

@shyammohankanojia

Hi all,
I am using this for PDF
for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

every thing is worked fine
But after some time.. im getting RuntimeError (Failed to execute:

error when i click to generate PDF first time it works and on second time PDF generate it is giving
RuntimeError (Failed to execute:
error if i restarts server it gives a pdf on first click and again on second click it gives same error.

RuntimeError (Failed to execute:
"/usr/local/bin/wkhtmltopdf" --header-html "file:///tmp/wicked_header_pdf_8273_0.html" --footer-html "file:///tmp/wicked_footer_pdf_8273_0.html" --margin-top 30 --margin-bottom 20 --margin-left 30 --margin-right 30 -q - -
Error: PDF could not be generated!):

Please suggest what should i do ,
I am using Ubuntu 10.04 64 bit
Im also getting same error on local machine Fedora 15 64 bit

@wanchic
wanchic commented May 11, 2012

shyammohankanojia,
Have you considered making a virtual machine with Ubuntu 12.04 and testing your app under this? What version of Ruby/Rails are you using?

@shyammohankanojia

Thanks for reply,
No, I am using Ubuntu 10.04 64 bit,
and using ruby 1.8.7 and rails 2.3.5.
It was working some days before but now its behaving strange.

@wanchic
wanchic commented May 11, 2012

Just an update for all. I've migrated from apache/passenger to nginix/unicorn, with Ubuntu 12.04. Looking back at my past fix notes, everything still works. However, I noticed that user: snasir suggested installing

openssl build-essential xorg libssl-dev

If you are using rvm, you'll probably already have openssl, build-essential, libssl-dev installed in order to compile ruby. What's left is xorg. If you're running a server, IMHO it is really not necessary or desired for the entire xorg package to be installed. Maybe snasir can comment if it is. Installing just the wkhtmltopdf package only installs the necessary dependencies (some which I'm still confused as to why it's installed, like mysql-common for example). Here is a list of packages:

fontconfig fontconfig-config libaudio2 libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libfontconfig1 libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libice6 liblcms1 libmng1 libmysqlclient18 liborc-0.4-0 libqt4-dbus libqt4-declarative libqt4-network libqt4-script libqt4-sql libqt4-sql-mysql libqt4-xml libqt4-xmlpatterns libqtcore4 libqtgui4 libqtwebkit4 libsm6 libtiff4 libxi6 libxrender1 libxt6 mysql-common qdbus ttf-dejavu-core x11-common

This is the same instruction sequence that works with my Ubuntu 12.04 server, nginx & unicorn:

  1. apt-get install wkhtmltopdf or apt-get install all of those packages listed above. (Yes, those packages are necessary or your PDF won't compile)

  2. Next, remove the wkhtmltopdf-binary gem (if installed) and rename /usr/bin/wkhtmltopdf to wkhtmltopdf_old.

  3. Download the static binary (i386/amd64) from google's site. (http://code.google.com/p/wkhtmltopdf/downloads/list) Install it under /usr/local/bin (eg. /usr/local/bin/wkhtmltopdf-amd64)

  4. Create a soft link in /usr/bin with the simple label "wkhtmltopdf" so future programs will execute with ease:

    ln -s /usr/local/bin/wkhtmltopdf-i386 /usr/bin/wkhtmltopdf
       ...or...
    ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf
    
  5. Restart nginx (and unicorn)/apache2 and test your app.

@jzumbrun

@wanchic Your solution worked. Thank you.

@abachuk
abachuk commented Oct 12, 2013

Had exactly the same issue, thanks to @wanchic and @Mr-Nizzle, here is step by step how I solved it:

  • sudo apt-get remove wkhtmltopdf --purge
  • sudo apt-get install fontconfig fontconfig-config libaudio2 libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libfontconfig1 libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libice6 liblcms1 libmng1 libmysqlclient18 liborc-0.4-0 libqt4-dbus libqt4-declarative libqt4-network libqt4-script libqt4-sql libqt4-sql-mysql libqt4-xml libqt4-xmlpatterns libqtcore4 libqtgui4 libqtwebkit4 libsm6 libtiff4 libxi6 libxrender1 libxt6 mysql-common qdbus ttf-dejavu-core x11-common
  • sudo curl -C - -O http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
  • cd /usr/local/bin
  • mv sudo mv wkhtmltopdf wkhtmltopdf_old
  • sudo curl -C - -O http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
  • sudo tar -xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
  • ln -s wkhtmltopdf-amd64 wkhtmltopdf
  • open application.rb WickedPdf.config = { :exe_path => "/usr/local/bin/wkhtmltopdf" }
@sachinprasad

@abachuk Thanks for step by step process.

@buren-trialbee

@abachuk comment as a script (needs to run with sudo):

set -e
echo 'Removing old installation of wkhtmltopdf (if present)'
(apt-get remove wkhtmltopdf --purge) || true
echo 'Installing wkhtmltopdf dependencies'
(apt-get install -y fontconfig fontconfig-config libaudio2 libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libfontconfig1 libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libice6 liblcms1 libmng1 libmysqlclient18 liborc-0.4-0 libqt4-dbus libqt4-declarative libqt4-network libqt4-script libqt4-sql libqt4-sql-mysql libqt4-xml libqt4-xmlpatterns libqtcore4 libqtgui4 libqtwebkit4 libsm6 libtiff4 libxi6 libxrender1 libxt6 mysql-common qdbus ttf-dejavu-core x11-common ) || true
cd /usr/local/bin
echo 'Renaming old wkhtmltopdf to wkhtmltopdf_old (if present in /usr/local/bin/'
[[ -f /usr/local/bin/wkhtmltopdf ]] && mv wkhtmltopdf wkhtmltopdf_old
echo 'Downloading static wkhtmltopdf binary from Google Code'
curl -C - -O http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
tar -xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
rm wkhtmltopdf-0.9.9-static-amd64.tar.bz2
echo 'Creating symlink wkhtmltopdf-amd64 => wkhtmltopdf'
ln -s wkhtmltopdf-amd64 wkhtmltopdf

for example save it to ~/install_wkhtmltopdf.sh and run it with sudo: sudo bash ~/install_wkhtmltopdf.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment