How to generate PDF Files from HTML In Python using PDFKIT

Generating PDF files using pdfkit:

There are many approaches for generating PDF in python. pdfkit is one of the better approaches as, it renders HTML into PDF with various image formats, HTML forms, and other complex printable documents.

Install pdfkit: You can install it with pip using the following command.

pip install pdfkit

To make pdfkit work perfectly we have to install wkhtmltopdf which deals with images and other complex things, To know more about wkhtmltopdf please go through

Basic Usage of pdfkit:

We can create a PDF document with pdfkit in 3 ways. They are 1. from URL, 2. from a html file and 3. from the string.

1. Generate PDF from URL: The following script gives us the pdf file from a website URL.

import pdfkit
pdfkit.from_url('', 'micro.pdf')

2. Generate PDF from file: The following script gives us the pdf file from a HTML file.

import pdfkit
pdfkit.from_file('micropyramid.html', 'micro.pdf')

3. Generate PDF from the string: The following script gives us the pdf file from string.

import pdfkit
pdfkit.from_string('MicroPyramid', 'micro.pdf')

Advanced Usage of pdfkit:

We can pass some options to pdfkit pdf file generation function that controls the page size, margins, and many others. Here is an example of how we pass the options.

options = {
    'page-size': 'A4',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
pdfkit.from_url('', 'micro.pdf', options=options)

Please go through, to know of many other options.

Django's way of using pdfkit: We can use the pdfkit to generate the pdf files in Django also. The following is the sample script to generate the pdf file from HTML in Django.

 from django.template.loader import get_template 
 from django.template import Context
 import pdfkit

 template = get_template("output_pdf.html")
 context = Context({"data": data})  # data is the context data that is sent to the html file to render the output. 
 html = template.render(context)  # Renders the template with the context data.
 pdfkit.from_string(html, 'out.pdf')
 pdf = open("out.pdf")
 response = HttpResponse(, content_type='application/pdf')  # Generates the response as pdf response.
 response['Content-Disposition'] = 'attachment; filename=output.pdf'
 os.remove("out.pdf")  # remove the locally created pdf file.
 return response  # returns the response.

Some time on the server we might get an issue wkhtmltopdf: cannot connect to X server. This is because of the standard installation of wkhtmltopdf, requires a xserver. You should download a precompiled version. If you execute the following lines on your shell it will be fixed.

tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
sudo mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
sudo chmod +x /usr/local/bin/wkhtmltopdf
    By Posted On

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
WSGI explanation with simple APP Ravi Kumar Gadila

The main goal of WSGI is to facilitate easy interconnection of servers and web frameworks/applications. WSGI defines a standerd API for web servers(uWSGI, Twisted, Gunicorn) ...

Continue Reading...
Generating CSV, Excel files Using Python Shirisha Gaddi

In most of the cases, you need to export the data from your database to different formats. In this post I will show you how ...

Continue Reading...
Python to Debian package: Simple, Easy and Fast Dinesh Deshmukh

Packaging python script to debian follows strict instructions, using the following instructions, most of the steps can be skipped hence making it easy and fast.
Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
*We don't provide your email contact details to any third parties