Up arrow

"Introduction"

About me Héctor Alvarez / @monobotblog / monobot.soft@gmail.com

Reportlab

Reportlab is a PDF generating suite.

It's important to point out that it has an opensource part (where we are focusing) and a privative part that is intended for higher performance and has support included'

Installation

    Directly with pip

    
    $ pip install reportlab
                                

    Its recomended to install it inside virtualenv.

Self built PDF tutorial

Since its a PDF generating tool it has a very nice tutorial in PDF that is generated on the fly.

You can download it from here.

Those instructions are more in depth than this presentation.

How are the reportlab components?

A reportlab PDF document is named canvas that can contain a number of pages, the templates and inside them can be rendered a number of items named flowables

Canvas

The canvas is the space where everything happens, since its a space it has dimension and to be able to place the items it has coordinates.

And since its the main component its where you define the PDF name that will be generated.


from reportlab.pdfgen import canvas


def hello(c):
    c.drawString(100, 100, "Hello World")


c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
                            

canvas has a lot of different settings that are in cascade applied to the items that are placed in it

    setTitle Title of the pdf document.

    setFont Sets the font style and font size.

    setLineWidth Line width of the lines or draws.

    setStrokeColor Color of the lines and texts.

    setFillColor If the drawins are closed defines their fill color.

    setDash Sets the line dash style.

Its coordinates can be modified and rotated.

    translate Translates the coordinate system.

    rotate Rotates the coordinate system.

    saveState Saves the state of the canvas (position, rotation, colores, strokes, everything), before further changes.

    restoreState Restores previously saved state.

Template and Frame

The template defines where and how the different flowable can be rendered.

To be able to locate flowables inside a template you have to define on what regions those flowables can be rendered, those regions are named frames

Flowable

Anything that will be rendered is a flowable, there are many different flowables:

    paragraphs

    images

    tables

    graphics

    charts

But why that name?

The

Toolbox

It will be impossible to work with all the rest of the things with a good amount of tools like:

    fonts

    color

    dimensions (cms, inchs, A4, letter, etc...)