Generating PDF letter heads for clients in mass! Report Lab Example.

James Lewis
5 min readJun 4, 2021

Hey there! Welcome to my blog where I post about my journey as a self-taught developer. You can find my GitHub by clicking HERE.

By Tiger LilyPexels

What are we building?

We will be building a small python script which will do the following.

  1. Query users and their personal data
  2. Populate a PDF letter head with the user data
  3. Append each PDF with user data to make one large PDF which we can then print in bulk to mail out.

What are we using to build this?

  1. Python
  2. Report Lab.

You will need Python and Report Lab installed. Use pip for report lab, example below.

pip install reportlab

If you are having difficulty, there are manual downloads available as well! Helpful download resource, HERE.

Open your favorite text / code editor and create app.py, sample.pdf, and a data.py file. File structure should look like this.

APP/
app.py
sample.pdf
data.py

In our data.py file, we have the following code. A users array of dictionaries. This will act as our seed data for this small script.

In our app.py we have the following code.

Let’s walk through each of the lines of code so you can retain the information and hopefully use this concept in the wild!

Screenshot by Author — James Lewis

We import our data (seed data), as users from our data.py file.

There are several imports from reportLab that we will need.

  • canvas — From the DOCS! ” The canvas should be thought of as a sheet of white paper with points on the sheet identified using Cartesian (X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page”.
  • pdfmetrics — Allows us to register and import fonts! “Acrobat Reader comes with 14 standard fonts built in. Therefore, the ReportLab PDF Library only needs to refer to these by name. If you want to use other fonts, they must be available to your code and will be embedded in the PDF document.”
  • TTFont — References a True Type Font. “A TrueType font is a font standard and is the major type of font found in both Mac and Microsoft Windows operating systems”. Definition taken from techopedia.

The most important of these imports is canvas.

Screenshot by Author — James Lewis

I wrapped our reportLab logic in a function, called run.

We can now use our imports! Let’s register a True Type Font, Tahoma and Tahoma Bold.

Screenshot by Author — James Lewis

Variable doc is the backbone of our script. We use the canvas import as you see above. It can take several different parameters, it can even take a buffer if that is what your application requires. In this case, we already have our blank PDF doc in the root directory. So that is what we pass in as a string with the PDF extension.

Screenshot by Author — James Lewis

Remember our data.py file? It contained an array of dictionaries. Let’s loop through it.

Screenshot by Author — James Lewis

Canvas has a setFont method to set font and font size. Since we’ve registered Tahoma we are able to now use it in our application. Let’s choose font size 8.

Screenshot by Author — James Lewis

Here is where the magic happens! Canvas has a very popular method called drawString. Remember when I provided the definition of canvas above? Well, the 25, 710 and 25, 700 represent an x and y axis. Starting point is 0, 0 on every PDF page, this is at the bottom left hand side of every page.

Therefore we are placing the users name at the top left, and the users address directly below, just as we would with a letter head to put in the mail.

showPage Method:

“The showPage method causes the canvas to stop drawing on the current page and any further operations will draw on a subsequent page (if there are any further operations — if not no new page is created). The save method must be called after the construction of the document is complete — it generates the PDF document, which is the whole purpose of the canvas object”

Okay! So what does this excerpt mean? Docs (found HERE). It means if we want to create multiple PDF pages we must call the showPage method as seen in the above code screenshot. This next part is very important! After we call showPage, we exit the loop and call the save method. This will create our PDF n times (n being the number of users we have in our dictionary).

Screenshot by Author — James Lewis

We exit the loop! We save the document and then we can now run the function in the script.

The entire script can be run from the root directory now.

python app.py

And your 3 page PDF will replace the empty sample.pdf in your directory.

Entire code from app.py below!

And code from data.py below as well!

I hope this post helped someone! Report Lab is a great tool to generate PDF’s. The documentation is long and can be a bit confusing, but this post went over how to generate basic PDF’s from existing data.

Happy Coding!

--

--

James Lewis

I am an obsessive autodidact, I love coffee, early mornings, guitar, camping, traveling with my wife and of course…software development!