When making PDFs in python, we use reportlab for the most part. It is an extensive module that can do make about anything you would want in a PDF. Today, I will cover how to do the most basic function, putting some text somewhere on the page. Here is the simplest example:

1
2
3
4
5
6
7
#!/usr/bin/env python
from reportlab.pdfgen import canvas

c = canvas.Canvas('rldemo1.pdf')
c.drawString(100, 100, 'Hello, world!')
c.showPage()
c.save()

You will notice that reportlab takes care of opening the file when we create a new Canvas instance. The drawString method puts a piece of text in an interesting place. It starts 100 points from the left as expected, but also 100 points from the bottom. I expected the origin to be the top-left, but reportlab starts from the bottom-left. With showPage, we commit our changes and save will write the file to disc. If you run this, you will also notice that the page size is A4 by default, which is because ReportLab, the company, is based in the UK. So how do we change that and start from the top instead of the bottom in reportlab?

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

c = canvas.Canvas('rldemo2.pdf', pagesize=letter)
width, height = letter
c.drawString(100, height - 100, 'From the top!')
c.showPage()
c.save()

So here we supply Canvas with the letter pagesize. Just that simple. Now that we have the pagesize, which is but a tuple, we can extract the width and height. With the page height, we can subtract the offset from the top to have the text placed relative to the top of the page. Can I use something other than points? Real paper layouts are done in inches and centimeters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env python
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch, cm
from reportlab.pdfgen import canvas

c = canvas.Canvas('rldemo3.pdf', pagesize=letter)
width, height = letter
c.drawString(inch, height - inch, '1 inch')
c.drawString(inch, height - 2 * inch, '2 inches')
c.drawString(cm, cm, '1 cm')
c.drawString(cm, 2 * cm, '2 cm')
c.showPage()
c.save()

Our reportlab module features a variety of constants for unit size, which are multiples of points. One gotcha with all of the text placement. The text is always drawn top and right of the coordinates given with drawString, which you can see if you run this example. You can write text from the left now, but what about centered text and right-aligned text?

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas

c = canvas.Canvas('rldemo4.pdf', pagesize=letter)
width, height = letter
c.drawString(inch, height - inch, 'Left')
c.drawCentredString(width / 2.0, height - inch, 'Center') # Notice the UK

spelling :) c.drawRightString(width - inch, height - inch, 'Right') c.showPage() c.save()

There are two more methods for printing text, drawCentredString and drawRightString. The only difference from drawString is that these start drawing from the center and right. All three still go from the bottom up.

Posted by Tyler Lesmann on January 23, 2009 at 6:32
Tagged as: python reportlab
Comments
#1 kegionoulkito wrote this 1 year, 5 months ago

Great posts

Thank you for posting!

Post a comment