wicked_pdf does not run on Ubuntu server : wkhtmltopdf: cannot connect to X server
Asked Answered
K

3

14

I'm trying to use wicked_pdf on my prod server but it keeps failling :

RuntimeError (Failed to execute:
"/usr/bin/wkhtmltopdf" -q        "file:////tmp/wicked_pdf20130709-23109-1adqx5g.html" "/tmp/wicked_pdf_generated_file20130709-23109-1ic5dbe.pdf"
Error: PDF could not be generated!
 Command Error: wkhtmltopdf: cannot connect to X server
):
  app/controllers/contrats_controller.rb:15:in `block (2 levels) in show'
  app/controllers/contrats_controller.rb:11:in `show'

I tried to follow this answer : wkhtmltopdf: cannot connect to X server but it still does not work.

Kampong answered 9/7, 2013 at 19:57 Comment(0)
K
15

This post helped me to resolve my problem : http://www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/

I'm reproducing here the step from this post that helped me to install it :

# first, installing dependencies
sudo aptitude install openssl build-essential xorg libssl-dev

# 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

He also create an initializer to tell to pdfKit where it is, so this method is for wicked PDF and PDF Kit.

Kampong answered 12/7, 2013 at 8:37 Comment(3)
This worked for me. I had to add a the following to a initializers/wicked_pdf.rb file as for to get it to work: WickedPdf.config = { :exe_path => '/path/to/wkhtmltopdf' }Amatol
So that was my issue, it had to be in 64bitTreillage
This answer works, as the comment says, for 64 bit only.Lop
O
12

Resolved this problem in this tread https://mcmap.net/q/187481/-wkhtmltopdf-cannot-connect-to-x-server

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 https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-i386.deb
$ sudo dpkg -i wkhtmltox-0.12.2.1_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 https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
$ sudo dpkg -i wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
$ wkhtmltopdf http://www.google.com test.pdf
Ootid answered 22/1, 2016 at 13:14 Comment(4)
This answer worked for me only in an Ubuntu 32 bit version. In 64 bit found many problems when issuing command sudo dpkg -i wkhtmltox-0.12.2_linux-trusty-i386.debLop
@Francisco you will need to use the 64 bit package http://download.gna.org/wkhtmltopdf/0.12/0.12.2/wkhtmltox-0.12.2_linux-trusty-amd64.debGhana
Thanks @Ghana still have to fix staging server, so I'm giving it a shot.Lop
gna.org is shut downAscogonium
P
8
  1. Verify you have xvfb installed, or install it using apt-get install xvfb.

  2. Create a file called wkhtmltopdf.sh and add the following:

    xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf $*
    

    Change the dimensions (640x480x16) to match whatever virtual screen parameters you want it to emulate.

  3. Move the shell script to /usr/bin, and set permissions:

    sudo chmod a+x /usr/bin/wkhtmltopdf.sh
    
  4. Optionally, you can add a symbolic link in your project directory:

    ln -s /usr/bin/wkhtmltopdf.sh wkhtmltopdf
    

See this and this for reference.

Piggish answered 11/7, 2013 at 21:16 Comment(1)
It seems you can do it without xvfbKampong

© 2022 - 2024 — McMap. All rights reserved.