How to Easily Create a PDF File with Python (in 3 Steps)

How to create PDF files in a few minutes using Python and HTML templates

The PyCoach
Towards Data Science

--

Image via Shutterstock under license to Frank Andrade

PDF is probably one of the most common file types that we can find on our computers.

We use PDFs for our resumes, reports, invoices, you name it!

A common way to create a PDF file is by saving a Word file as .pdf, but we can also create a PDF file using Python.

The pros? If you plan to create multiple PDFs using the same template, you could do it with Python! But first, we’ll see how to create a PDF file using Python.

In this tutorial, we’ll first create a simple PDF file and then we’ll create a more advanced one that looks like the one below.

Image by author

How to Easily Create a PDF File with Python

There are different ways to create a PDF file with Python, but I found using HTML templates the easiest one.

With this option, we can create basic and advanced PDFs in a few minutes!

Install the libraries

To create a PDF file, we need to install the following libraries:

pip install pdfkit
pip install jinja2

Before we use thepdfkit library, we need to install wkhtmltopdf. The steps to install it is up to your operating system:

# macOS (you need to install brew first)
brew install homebrew/cask/wkhtmltopdf
# Ubuntu
sudo apt-get install wkhtmltopdf

In case you’re on Windows, you can download the installer here.

Now let’s create a simple PDF with Python.

Step 1: Create an HTML template with placeholders

First, we need to create an HTML document that we’ll use as a template later.

To create this HTML document, we’ll use a website called HTML Editor. On this website, we can type text in the visual editor on the left and generate its HTML code in the source editor on the right.

Here’s the text I introduced in the editor (this file can be found on my GitHub)

Note the placeholders I created using the {{}}. Later we’ll introduce values inside these curly braces with Python.

Here’s the HTML code that was generated by the HTML editor.

<hr />
<p style="text-align: center;"><strong>Contract</strong></p>
<hr />
<p style="text-align: left;">I {{my_name}} am signing this contract ...</p>
<p style="text-align: left;">The house has the following items:</p>
<ol>
<li style="text-align: left;">{{item1}}</li>
<li style="text-align: left;">{{item2}}</li>
<li style="text-align: left;">{{item3}}</li>
</ol>
<p style="text-align: left;">Date: {{today_date}}</p>
<p style="text-align: left;">{{my_name}}</p>

We need to copy this HTML code, create an HTML file in our working directory, and paste the code on it.

Step 2: Create a Python variable for each placeholder

First, we import jinja2 and pdfkit. I have a placeholder named {{today_date}}, so I also import datetime to get today’s date.

Then we create a Python variable for each placeholder in our HTML document and we create a dictionary that pairs the placeholders with the Python variables.

import jinja2
import pdfkit
from datetime import datetime

my_name = "Frank Andrade"
item1 = "TV"
item2 = "Couch"
item3 = "Washing Machine"
today_date = datetime.today().strftime("%d %b, %Y")

context = {'my_name': my_name, 'item1': item1, 'item2': item2, 'item3': item3,
'today_date': today_date}

Note that I named each Python variable as the placeholders, but they could have different names.

Step 3: Create an environment for our template and export the PDF

Now it’s time to use jinja2 to create an environment for our template.

template_loader = jinja2.FileSystemLoader(‘./’)
template_env = jinja2.Environment(loader=template_loader)

Then we specify which template we’re using. In my case, I named the HTML file as basic-template.hmtl. After this, we render the dictionary we created in step 2.

template = template_env.get_template(‘basic-template.html’)
output_text = template.render(context)

Next, we add wkhtmltopdf to the pdfkit configuration. To do so, we need to specify the path where wkhtmltopdf was installed.

Here’s the command you need to run to get this path.

# macOS/Linux
>>> which wkhtmltopdf
'/usr/local/bin/wkhtmltopdf'
# Windows
>>> where wkhtmltopdf
'C:\Program Files\wkhtmltopdf'

Finally, we export the PDF file using the .from_string() method.


template_loader = jinja2.FileSystemLoader('./')
template_env = jinja2.Environment(loader=template_loader)

template = template_env.get_template('basic-template.html')
output_text = template.render(context)

config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_string(output_text, 'pdf_generated.pdf', configuration=config, css='style.css')

Note that I added a CSS parameter to the .from_string() method. This is optional, but I found it necessary to make the font size of the paragraphs in our PDF bigger.

I created a style.css file in my working directory and wrote the code below.

p { font-size: 24px; }
li { font-size: 24px; }

That’s it. Here’s the PDF we generated with Python.

The code we wrote and the files used in this tutorial can be found on my GitHub.

Now to generate a more advanced PDF like the invoice below, we need to download an HTML template and create some placeholder. In this video tutorial, I show how to do it.

Automate your life with Python! Get my FREE Automation Cheat Sheet by joining my email list with 10k+ people.

If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to thousands of Python guides and Data science articles. If you sign up using my link, I’ll earn a small commission with no extra cost to you.

--

--