Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf: cannot connect to X server

I have been using wkthmltopdf to convert html to pdf documents on-the-fly on my linux web server. The program originally needed X11 or similar X server to run correctly, but through many requests by developers to have this run on servers without GUI, I am pretty sure it runs a virtual X server in the static version. I have been using the static (stand-alone) version of the program and it works great! I would put the executable file in a folder, and run:

./wkhtmltopdf file1.html file2.pdf

However I would like to install this program system-wide. I used the apt-get install wkhtmltopdf (just installed yesterday) and since I am running on a 64 bit system, I also needed apt-get install ia32-libs. After installation I can find the version like this:

wkhtmltopdf --version

output:

Name:
  wkhtmltopdf 0.9.9

License:
  Copyright (C) 2008,2009 Wkhtmltopdf Authors.



  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  This is free software: you are free to change and redistribute it. There is NO
  WARRANTY, to the extent permitted by law.

Authors:
  Written by Jakob Truelsen. Patches by Mário Silva, Benoit Garret and Emmanuel
  Bouthenot.

Now when I try to run the program installed via aptitude, I get the following error:

wkhtmltopdf: cannot connect to X server

Does anyone know how I can fix this? I guess this version is missing a virtual X server or something.

like image 434
jeffery_the_wind Avatar asked Mar 07 '12 15:03

jeffery_the_wind


5 Answers

or try this (from http://drupal.org/node/870058)

  1. Download wkhtmltopdf. Or better install it with a package manager:

    sudo apt-get install wkhtmltopdf
    
  2. Extract it and move it to /usr/local/bin/

  3. Rename it to wkhtmltopdf so that now you have an executable at /usr/local/bin/wkhtmltopdf
  4. Set permissions: sudo chmod a+x /usr/local/bin/wkhtmltopdf
  5. Install required support packages.

    sudo apt-get install openssl build-essential xorg libssl-dev
    
  6. Check to see if it works: run

    /usr/local/bin/wkhtmltopdf http://www.google.com test.pdf
    

    If it works, then you are done. If you get the error "Cannot connect to X server" then continue to number 7.

  7. We need to run it headless on a 'virtual' x server. We will do this with a package called xvfb.

    sudo apt-get install xvfb
    
  8. We need to write a little shell script to wrap wkhtmltopdf in xvfb. Make a file called wkhtmltopdf.sh and add the following:

    xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"
    
  9. Move this shell script to /usr/local/bin, and set permissions:

    sudo chmod a+x /usr/local/bin/wkhtmltopdf.sh
    
  10. Check to see if it works once again: run

    /usr/local/bin/wkhtmltopdf.sh http://www.google.com test.pdf
    

Note that http://www.google.com may throw an error like "A finished ResourceObject received a loading finished signal. This might be an indication of an iframe taking to long to load." You may want to test with a simpler page like http://www.example.com.

like image 140
TimoSolo Avatar answered Oct 23 '22 19:10

TimoSolo


This solved the issue for me:

sudo apt-get install xvfb
xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf file1.html file2.pdf
like image 37
syzspectroom Avatar answered Oct 23 '22 17:10

syzspectroom


I tried to do sudo apt-get install wkhtmltopdf but without any success. Instead I recommend you try:

  1. Download the latest executable (.11 rc1) :

    wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2

  2. uncompress it :

    tar -vxf wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2

  3. rename it :

    mv wkhtmltopdf-i386 wkhtmltopdf

  4. chmod it to executable :

    chmod a+x wkhtmltopdf

  5. place it into /usr/bin :

    sudo mv wkhtmltopdf /usr/bin

like image 28
Yakob Ubaidi Avatar answered Oct 23 '22 19:10

Yakob Ubaidi


Just made it:

1- To download wkhtmltopdf dependencies

# apt-get install wkhtmltopdf

2- Download from source

# wget http://downloads.sourceforge.net/project/wkhtmltopdf/xxx.deb

# dpkg -i xxx.deb

3- Try

# wkhtmltopdf http://google.com google.pdf

Its working fine

It works!

like image 27
Sfblaauw Avatar answered Oct 23 '22 17:10

Sfblaauw


I found method to resolve this problem without fake X server. In newest version of wkhtmltopdf dont need X server for work, but it no into official linux repositories.

Solution for Ubuntu 14.04.4 LTS (trusty) i386

$ sudo apt-get install xfonts-75dpi
$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2/wkhtmltox-0.12.2_linux-trusty-i386.deb
$ sudo dpkg -i wkhtmltox-0.12.2_linux-trusty-i386.deb
$ wkhtmltopdf http://www.google.com test.pdf

Solution for Ubuntu 14.04.4 LTS (trusty) amd64

$ sudo apt-get install xfonts-75dpi
$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2/wkhtmltox-0.12.2_linux-trusty-amd64.deb
$ sudo dpkg -i wkhtmltox-0.12.2_linux-trusty-amd64.deb
$ wkhtmltopdf http://www.google.com test.pdf

User felixhummel got very good solution, but repository with utilite has changed.

like image 16
PRIHLOP Avatar answered Oct 23 '22 17:10

PRIHLOP