Skip to content

Hello World

Given the advent of ReportLab 4.0, we thought it would be best to start a demo series showcasing some of the awesome work we have been doing at ReportLab over the last 20 years!

To kickstart this new series, we will be creating a simple hello world (again) portable document format (PDF) in ReportLab and ReportLab Plus.

For way more information and examples, have a look at the full documentation.

Installation

The complete installation guide for your system can be found at in the documentation, below is the commands I have used on my MacBook Air M1 with Ventura 13.2 and Python 3.11.

Create virtual environment:

We recommend using a virtual environment if you are following along in with this demo:

python3.11 -m venv rpt
source rpt/bin/activate

ReportLab

The opensource PDF toolkit (the basic PDF tools we think everyone needs) can simply be installed using the following.

Install reportlab:

pip install reportlab

Yup it's as easy as that!

ReportLab plus

ReportLab Plus is our extension of the opensource reportlab package. As you will see in the demo series it's a massive time saver with the extra modules / services.

After you have created your account you can simply install rlextra (this will install reportlab as well as the other dependencies):

pip install rlextra -i https://www.reportlab.com/pypi/

You will have to enter in your username/email and password.

First PDF

So our installation is complete, let's create our first PDF document with the opensource and the Plus package.

Open source

There are several ways to make a basic page with ReportLab (like platypus) but to keep things simple we will just use the Canvas class.

# import the canvas object
from reportlab.pdfgen import canvas

# create a Canvas object with a filename
c = canvas.Canvas("rl-hello_again.pdf", pagesize=(595.27, 841.89))  # A4 pagesize
# draw a string at x=100, y=800 points
# point ~ standard desktop publishing (72 DPI)
# coordinate system:
#   y
#   |
#   |   page
#   |
#   |
#   0-------x
c.drawString(50, 780, "Hello Again")
# finish page
c.showPage()
# construct and save file to .pdf
c.save()

Above is a modified example from chapter 2 of the docs. All we are doing is creating a page using the Canvas class, drawing a string in the top left and saving it to a file. If we copy the code above into a file (I call it rl-hello_again.py) and run python3 rl-hello_again.py we can see it outputed a PDF file.

rl-hello_again.png

ReportLab plus

Let's do the same again but this time with a little bit of help from rml. The best thing about ReportLab Plus (in my opinion) is if you don't know how to do something, don't worry it's probably already been done before! Have a look at our samples or tutorials and modify them as you wish.

The first thing you may ask is well what is rml? Report Markup Language (rml) is our own language which massively speeds up development and design of reusable generated PDF's. If you are already used to HTML or XML, then rml should be no problem. A very lightweight rml file is shown below:

<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="rlplus-hello_again.pdf">

    <template>
        <pageTemplate id="main">
            <frame id="first" x1="72" y1="72" width="451" height="698"/>
        </pageTemplate>
    </template>

    <stylesheet>
    <!-- This is your stylesheet -->
    </stylesheet>

    <!-- The story (your actual content) starts below this comment -->

    <story>
        <para>
            Hello Again
        </para>
    </story>

</document>

This rml file is split up into five sections (for now):

  • doctype

  • document

  • template

  • stylesheet

  • story

For this tutorial just know that story is where we input anything custom. From here we can use the command line entry point:

rml2pdf rlplus-hello_again.rml 

or inside a python file:

# import rml2pdf from rlextra
from rlextra.rml2pdf import rml2pdf

# open rml
with open("rlplus-hello_again.rml", "r") as rml:
    rml2pdf.go(rml.read(), "rlplus-hello_again.pdf")
    rml.close()

rlplus-hello_again.png

So there we have it, the most basic PDF you will probably ever make with ReportLab and ReportLab Plus.