This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Introduction
The Reportlab PDF Toolkit started life in the year 2000 by a companycalled Reportlab Inc.. Reportlab is now owned by ReportLab EuropeLtd. They produce the open source version of Reportlab. The Reportlabtoolkit is actually the foundation of their commercial product, ReportMarkup Language which is available in their Reportlab PLUSpackage. This book is focused on the open source version of Reportlab.The Reportlab PDF Toolkit allows you to create in Adobes PortableDocument Format (PDF) quickly and efficiently in the Python programminglanguage. Reportlab is the defacto method of generating PDFs in Python.You can also use Reportlab to create charts and graphics in bimap andvector formats in addition to PDF. Reportlab is known for its ability togenerate a PDF fast. In fact, Wikipedia chose Reportlab as their tool ofchoice for generating PDFs of their content. Anytime you click theDownload as PDF link on the left side of a Wikipedia page, it usesPython and Reportlab to create the PDF!
In this book, you will learn how to use Reportlab to create PDFs too.This book will be split into three sections. We will be covering thefollowing topics in the first section:
- The canvas
- Drawing
- Working with fonts
- PLATYPUS
- Paragraphs
- Tables
- Other Flowables
- Graphics
- and More!
In the second section, we will learn about data processing. The ideahere is to take in several different data formats and turn them intoPDFs. For example, it is quite common to receive data in XML or JSON.But learning how to take that information and turn it into a report issomething that isnt covered very often. You will learn how to do thathere. In the process we will discover how to make multipage documentswith paragraphs and tables that flow across the pages correctly.
The last section of the book will cover some of the other libraries youmight need when working with PDFs with Python. In this section we willlearn about the following:
About the Author
You may be wondering about who I am and why I might be knowledgeableenough about Python to write about it, so I thought Id give you alittle information about myself. I started programming in Python in theSpring of 2006 for a job. My first assignment was to port Windows loginscripts from Kixtart to Python. My second project was to port VBA code(basically a GUI on top of Microsoft Office products) to Python, whichis how I first got started in wxPython. Ive been using Python eversince, doing a variation of backend programming and desktop front enduser interfaces as well as automated tests.
I realized that one way for me to remember how to do certain things inPython was to write about them and thats how my Python blog came about:http://www.blog.pythonlibrary.org/. As I wrote, I would receivefeedback from my readers and I ended up expanding the blog to includetips, tutorials, Python news, and Python book reviews. I work regularlywith Packt Publishing as a technical reviewer, which means that I get totry to check for errors in the books before theyre published. I alsohave written for the Developer Zone (DZone) and i-programmer websites aswell as the Python Software Foundation. In November 2013, DZonepublished The Essential Core Python Cheat Sheet that I co-authored.I have also self-published the following books:
- Python 101 - June 2014
- Python 201: Intermediate Python - Sept. 2016
- wxPython Cookbook - Dec. 2016
Conventions
As with most technical books, this one includes a few conventions thatyou need to be aware of. New topics and terminology will be in bold.You will also see some examples that look like the following:
>>> myString = "Welcome to Python!"
The >>> is a Python prompt symbol. You will see this in thePython interpreter and in IDLE. Other code examples will beshown in a similar manner, but without the >>>. Most of thebook will be done creating examples in regular Python files, so youwont be seeing the Python prompt symbol all that often.
Setting up & Activating a Virtual Environment
If you dont want to add ReportLab into your systems Pythoninstallation, then you can use a virtual environment. In Python 2.x -3.2, you would need to install a package called virtualenv to createa virtual environment for Python. The idea is that it will create afolder with a copy of Python and pip. You activate the virtualenvironment, run the virtual pip and install whatever you need to.Python 3.3 added a module to Python called venv that does the samething as the virtualenv package, for the most part.
Here are some links on how all that works:
- https://docs.python.org/3/library/venv.html (Python 3 only)
- https://pypi.python.org/pypi/virtualenv (Python 2 and 3)
When you are using a Python Virtual Environment, you will need to firstactivate it. Activation of a virtual environment is like starting avirtual machine up in VirtualBox or VMWare, except that in this case,its just a Python Virtual Environment instead of an entire operatingsystem.
Creating a virtual sandbox with the virtualenv package is quite easy. OnMac and Linux, all you need to do is the following in your terminal orcommand prompt:
virtualenv FOLDER_NAME
To activate a virtual environment on Linux or Mac, you just need tochange directories to your newly created folder. Inside that foldershould be another folder called bin along with a few other foldersand a file or two. Now you can run the following command:
source bin/activate
On Windows, things are slightly different. To create a virtualenvironment, you will probably need to use the full path to virtualenv:
c:\Python27\Scripts\virtualenv.exe
You should still change directories into your new folder, but instead ofbin, there will be a Scripts folder that can run activateout of:
Scripts\activate
Once activated, you can install any other 3rd party Python package.
Note: It is recommended that you install all 3rd party packages, suchas ReportLab or Pillow, in a Python Virtual Environment or a userfolder. This prevents you from installing a lot of cruft in your systemPython installation.
I would also like to mention that pip supports a user flagthat tells it to install the package just for the current user if theplatform supports it. There is also an update flag (or just-U) that you an use to update a package. You can use this flag asfollows:
python -m pip install PACKAGE_NAME --upgrade
While you can also use pip install PACKAGE_NAME, it is now becoming arecommended practice to use the python -m approach. What this doesdifferently is that it uses whatever Python is on your path and installsto that Python version. The