Python 2 and 3 wrapper for wkhtmltoimage utility to convert HTML to IMG using Webkit.
Installation
Install imgkit:
pip install imgkit
Install wkhtmltopdf:
Debian/Ubuntu:
sudo apt-get install wkhtmltopdf
Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.
with open('file.html') as f:
imgkit.from_file(f, 'out.jpg')
If you wish to further process generated IMG, you can read it to a variable:
# Use False instead of output path to save pdf to a variable
img = imgkit.from_url('http://google.com', False)
You can find all wkhtmltoimage options by type wkhtmltoimage command or visit this Manual. You can drop '--' in option name. If option without value, use None, False or '' for dict value:. For repeatable options (incl. allow, cookie, custom-header, post, postfile, run-script, replace) you may use a list or a tuple. With option that need multiple values (e.g. --custom-header Authorization secret) we may use a 2-tuple (see example below).
Each API call takes an optional config paramater. This should be an instance of imgkit.config() API call. It takes the config options as initial paramaters. The available options are:
wkhtmltoimage - the location of the wkhtmltoimage binary. By default imgkit will attempt to locate this using which (on UNIX type systems) or where (on Windows).
xvfb - the location of the xvfb-run binary. By default imgkit will attempt to locate this using which (on UNIX type systems) or where (on Windows).
meta_tag_prefix - the prefix for imgkit specific meta tags - by default this is imgkit-
Example - for when wkhtmltopdf or xvfb is not in $PATH:
Make sure that you have wkhtmltoimage in your $PATH or set via custom configuration (see preceding section). where wkhtmltoimage in Windows or which wkhtmltoimage on Linux should return actual path to binary.
IOError: 'No xvfb executable found':
Make sure that you have xvfb-run in your $PATH or set via custom configuration (see preceding section). where xvfb in Windows or which xvfb-run or which Xvfb on Linux should return actual path to binary.
IOError: 'Command Failed':
This error means that IMGKit was unable to process an input. You can try to directly run a command from error message and see what error caused failure (on some wkhtmltoimage versions this can be cause by segmentation faults)