304 North Cardinal St.
Dorchester Center, MA 02124
We got this error on a fresh installation of CentOS when trying to convert webpages to pdf’s using wkhtmltopdf:
wkhtmltopdf: cannot connect to X server
We’re getting this error because we do not have any sort of GUI environment installed (e.g. GNOME, KDE).
To fix this we could go one of two ways, the first would be to install a full blown GUI environment but that would take up too many resources, we only need to take screenshots of webpages from this server. The second route is to install Xvfb.
Xvfb (short for X virtual framebuffer) is an in-memory display server for UNIX-like operating system (e.g., Linux). It enables you to run graphical applications without a display.
To install Xvfb, issue the following command at the shell terminal:
yum install xorg-x11-server-Xvfb
The command to run wkhtmltopdf would change to take into account Xvfb. It’ll look something like the following:
xvfb-run -a --server-args="-screen 0, 1366x768x24" /usr/bin/wkhtmltopdf https://www.google.com/ google.pdf
That’s it, you’re done!