DEV Community

Mintu Agarwal
Mintu Agarwal

Posted on

Installation Guide for wkhtmltopdf for pdfkit in Python

Hi all, recently I created a project which required transforming an HTML output into a pdf file using python3. I faced several nuances while running the pdfkit util - from_file in python which internally requires wkhtmltopdf

You might face an error like the one below when executing pdfkit utilities.

OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
Enter fullscreen mode Exit fullscreen mode

As stated above, we must install the package from the above source. Installing using apt-get install or pip3 install may not necessarily work for you.

For me it threw the following error

> sudo apt-get install wkhtmltopdf
> pdfkit.from_file('x.html', 'y.pdf')
OSError: wkhtmltopdf exited with non-zero code 1. error:
qt.qpa.screen: QXcbConnection: Could not connect to display localhost:0.0
Could not connect to any X display.
Enter fullscreen mode Exit fullscreen mode

So the preferred way to get this is:

sudo wget -p ./ https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.deb
sudo dpkg -i  wkhtmltox-0.12.1_linux-wheezy-amd64.deb
Enter fullscreen mode Exit fullscreen mode

If after executing the second command you see the following error:

dpkg: dependency problems prevent configuration of wkhtmltox:
 wkhtmltox depends on libpng12-0; however:
  Package libpng12-0 is not installed.
Enter fullscreen mode Exit fullscreen mode

Install the libpng12-0 not using apt-get install but again the package itself

sudo wget http://se.archive.ubuntu.com/ubuntu/pool/main/libp/libpng/libpng12-0_1.2.54-1ubuntu1_amd64.deb
sudo dpkg -i libpng12-0_1.2.54-1ubuntu1_amd64.deb
sudo dpkg -i  wkhtmltox-0.12.1_linux-wheezy-amd64.deb
Enter fullscreen mode Exit fullscreen mode

Now your problem must get resolved. Upvote if this worked for you!
Also, the link to my project where I used pdfkit, which creates HTML and pdf of your Resume with the user providing the contents in a yaml.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay