How to create pdf files in Python using Pdfkit

Reading Time : ~ .

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 http://wkhtmltopdf.org/.

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('http://micropyramid.com', '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('http://micropyramid.com', 'micro.pdf', options=options)

Please go through http://wkhtmltopdf.org/usage/wkhtmltopdf.txt, 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(pdf.read(), content_type='application/pdf')  # Generates the response as pdf response.
 response['Content-Disposition'] = 'attachment; filename=output.pdf'
 pdf.close()
 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.

wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
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
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
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 using yield and generators. Dinesh Deshmukh

Generators are memory efficient. They allow us to code with minimum intermediate arguments, less data structures.
Generators are of two types, generator expressions and generator ...

Continue Reading...
Debugging in Python Ramya Ambati

When something goes wrong with your code instead of using standard debugging techniques such as print statements use debugging tools. I found two great tools ...

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