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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay