Skip to content

Using wkhtmltopdf without X server

MukulG27 edited this page Jul 12, 2023 · 13 revisions

wkhtmltopdf needs a X server. If you're running your application on a VPS, you probably don't have one installed. The solution is install a "virtual" X server.

Linux

Debian/Ubuntu

To install wkhtmltopdf in a Debian/Ubuntu server, follow this steps(as root):

apt-get install wkhtmltopdf
apt-get install xvfb
printf '#!/bin/bash\nxvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*' > /usr/bin/wkhtmltopdf.sh
chmod a+x /usr/bin/wkhtmltopdf.sh
ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf
wkhtmltopdf http://www.google.com output.pdf

If you cannot acquire the root shell (e.g. on an Azure/AWS Devops Agent) change the third line to:

printf '#!/bin/bash\nxvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*' | sudo tee /usr/bin/wkhtmltopdf.sh

Reference: Installing wkhtmltopdf on Ubuntu Server 12.04

Amazon Linux 2

To install 'wkhtmltopdf' in a Amazon Linux 2 server, follow these steps(as root):

yum install xorg-x11-server-Xvfb
ln -s /usr/bin/Xvfb /usr/bin/xvfb
printf '#!/bin/bash\nxvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*' > /usr/bin/wkhtmltopdf.sh
chmod a+x /usr/bin/wkhtmltopdf.sh
ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf
wkhtmltopdf http://www.google.com output.pdf```