How to create pdf files in Python using Pdfkit

Reading Time : ~ .

Generating PDF files using pdfkit:

There are many approches for generating PDF in python. pdfkit is one of the better approache 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 thorough

Basic Usage of pdfkit:

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

1. Generate PDF from URL: The following script gives us the pdf file from an 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 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 server we might get an issue wkhtmltopdf: cannot connect to X server. This is because the standard installation of wkhtmltopdf, requires an 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
Generating PDF Files in Python using xhtml2pdf Siva Chittamuru

There are many ways for generating PDF in python. In this post I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a ...

Continue Reading...

Using Fabric, we can develop interactive script for ec2 region, ec2 flavour amazon web services. For this, you need aws account, security group, key pair, ...

Continue Reading...
Export Data with Scrapy python Jagadeesh V

In previous post we collected top 250 movies names and their links from imDb. In this blog post, lets look into exporting that data into ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties