By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content .
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

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 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

To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code

    Posted On
  • 02 February 2013
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Understanding self and __init__ method in python Class.

Understand self and __init__ method in python Class?
Before understanding the "self" and "__init__" methods in python class, it's very helpful if we have the ...

Continue Reading...
Programming with python Descriptors (_get_, _set_, _delete_) - MicroPyramid

Python descriptors are object attributes that are only invoked for new style of classes. Python descriptors comes under the category of meta programming(code that manipulates ...

Continue Reading...
Sending SMS, MMS using Twilio.

A simple Tutorial on sending SMS and MMS in python using Twilio. In this tutorial you will learn how to send SMS, MMS and checking ...

Continue Reading...
open source packages

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