Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Python

How to Generate PDF Files from HTML In Python Using PDFKIT

2022-07-19

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