Home arrow Python arrow Page 2 - Python for PDF Generation

Putting Virtual Ink to Virtual Paper - Python

The Portable Document Format (PDF) lets you create documents that look exactly the same on every platform. Sometimes a PDF document needs to be generated dynamically, however, and that can be quite a challenge. Fortunately, there are libraries that can help. This article examines one of those for Python.

TABLE OF CONTENTS:
  1. Python for PDF Generation
  2. Putting Virtual Ink to Virtual Paper
  3. Text Formatting Techniques
  4. Using Graphics
By: Peyton McCullough
Rating: starstarstarstarstar / 64
March 14, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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)
>>> pdf.setStrokeColorRGB(1, 0, 0)
>>> pdf.drawString(300, 300, "CLASSIFIED")

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:

>>> pdf.showPage()

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:

>>> pdf.save()

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)
>>> pdf.setFillColorRGB(1, 0, 0)
>>> pdf.drawCentredString(letter[0] / 2, inch * 6, "CLASSIFIED")
>>> pdf.setFont("Courier", 30)
>>> pdf.drawCentredString(letter[0] / 2, inch * 5, "For Your Eyes
Only")
>>> pdf.showPage()
>>> pdf.save()

There, the result now looks slightly more pleasing.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: