Now that the ReportLab Toolkit has been installed, we can begin using it immediately. Fire up Python's interactive interpreter, and let's get started. The first step is to import canvas from the pdfgen module:
>>> from reportlab.pdfgen.canvas import Canvas
Since the ReportLab Toolkit is set up to use the A4 paper size by default, North American developers will have to perform an extra step to gain access to the standard 8.5" by 11" letter size:
>>> from reportlab.lib.pagesizes import letter
The pagesizes module also contains various other paper sizes in case you ever need to use them.
Next, we have to create the PDF document in the form of a Canvas object. It requires a filename argument, which may be an absolute or relative path (based in the current working directory):
>>> pdf = Canvas("test.pdf")
This will create an A4 page. However, as I mentioned before, not everyone will want an A4 page. To create a page based off of the letter page size, we must set pagesize:
>>> pdf = Canvas("test.pdf", pagesize = letter)
Throughout this article, I'll be using the letter page size simply because this is what I'm familiar with. In your own scripts, you're free to use whatever you're accustomed to.
We can now draw something in our PDF document. A string of text is probably the easiest place to start. We'll go ahead and draw one using the Courier font in red:
>>> pdf.setFont("Courier", 12)
An important thing to note here is that when specifying coordinates, the origin is in the lower left hand corner of the page, rather than the top left. It's also possible to specify measurements in other units. You can use centimeters, millimeters, inches and picas. The default unit of measurement is a point, equal to one seventy-second of an inch. The extra measurements are available from reportlab.lib.units:
>>> from reportlab.lib.units import cm, mm, inch, pica
To use the measurements, simply multiply them by however many units you want. Let's go ahead and draw a string of text one inch above the bottom of the page and one inch to the right of the page:
>>> pdf.drawString(2 * inch, inch, "For Your Eyes Only")
Now that we have some text on the page, let's close the page:
The showPage method closes the current page. Any further drawing will occur on the next page, though if all drawing has ended, another page will not be added. We now have to save the PDF document:
The ReportLab Toolkit saves our page, which you can now view. The first thing you'll notice is that it's rather ugly and blindly formatted. The text would look a lot better if it were centered, which is perfectly possible with the drawCentredString method (notice the British English spelling) and a bit of math. The drawCentredString method draws the text with its center on the given x-coordinate, which makes centering text easy since we only have to calculate the center of the page. We'll also change the font size (which, by the way, has been reset along with the font face and color since we started on a new page):
>>> pdf.setFont("Courier", 60)
There, the result now looks slightly more pleasing.
blog comments powered by Disqus