65

How to run wkhtmltopdf headless?!

Installation on Debian Whezzy

apt-get install wkhtmltopdf

Command

wkhtmltopdf --title "$SUBJECT" -q $SOURCEFILE $OUTPUTFILE

Error

QXcbConnection: Could not connect to display
3
58

There is a more easy way to make John WH Smith solution.

Just install xvfb from apt and then, you can run:

xvfb-run wkhtmltopdf

(just put xvfb-run before any wkhtmltopdf command.)

7
  • 1
    For posterity, this works on a Raspberry Pi, Raspbian Jessie as well. Thanks! Mar 18 '16 at 4:25
  • Also if you get xvfb-run: error: xauth command not found then just sudo apt-get install xauth and try again!
    – MediaVince
    Aug 18 '16 at 14:49
  • worked for me as well
    – Stefan
    Oct 31 '16 at 3:00
  • if you don't have the xvfb-run command in CentOS: yum install xorg-x11-server-Xvfb
    – kasimir
    Aug 8 '17 at 14:39
  • xvfb-run: error: Xvfb failed to start
    – tokam
    Feb 24 '19 at 18:40
56

This is a bug, and the fix hasn't been brought to the Debian repositories. Quoting ashkulz (who closed the bug report) :

You're using the version of wkhtmltopdf in the debian repositories, which does not support running headless.

So you can either...

8
  • 1
    If you do not want to build wkhtmltopdf from source, you might want to try phantomjs with: phantomjs /usr/share/doc/phantomjs/examples/rasterize.js $url $pdf.
    – ysdx
    Mar 26 '15 at 13:12
  • Have now downloaded the source to /var/bin/wkhtmltopdf.. cd /var/bin/wkhtmltopdf && scripts/build.py wheezy-amd64 => -bash: scripts/build.py: Permission denied
    – clarkk
    Mar 26 '15 at 13:37
  • @clarkk You shouldn't download the source to /var/bin. Take it to your home directory and follow the instructions in the INSTALL.md file, for your distribution. Mar 26 '15 at 13:39
  • I still get the error
    – clarkk
    Mar 26 '15 at 13:41
  • 2
    Or just grab the latest linux generic binary from the website: wkhtmltopdf.org/downloads.html Sep 27 '16 at 7:46
13

on your command line type like this

export DISPLAY=:0 

then run your qt applications

6
  • 1
    this may not be the right answer for the question at large, but I came here from google for a different similar problem that this solved.
    – plod
    Sep 9 '16 at 8:21
  • Solved my problem on raspberry pi3, qt5.7.0, gui error: "QXcbConnection: Could not connect to display Aborted" .
    – mohammads
    Jan 30 '17 at 3:27
  • Doesn't work for me on Raspberry Pi3 I get an error: "QXcbConnection: Could not connect to display Aborted" Nov 1 '17 at 10:04
  • My raspberry Pi always boots in command line mode. Nov 1 '17 at 10:05
  • Solved my problem while trying to run OBS Studio from terminal. Thank you :-)
    – Mohamed
    Mar 16 '19 at 21:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.