Home arrow Python arrow Page 4 - Python for PDF Generation

Using Graphics - 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

Drawing graphics in a PDF isn't very difficult with the ReportLab Toolkit. Let's go back to the pdfgen module and draw a few shapes on the page. Go ahead and create a new canvas to work with:

>>> pdf = Canvas("graphics.pdf", pagesize = letter)

We'll now draw a line that spans across the top of the page, leaving an inch on its left, right and top:

>>> pdf.line(inch, inch * 10, inch * 7.5, inch * 10)

We have the freedom to choose whatever colors we want for graphics. Here, we set the stroke color (the color outlining an image) to black and the fill color to lime green:

>>> pdf.setStrokeColorRGB(0, 0, 0)
>>> pdf.setFillColorRGB(0, 1, 0)

Using these colors, we can draw shapes. Notice how we have the option of specifying whether or not we want to stroke or fill the image being drawn. If, however, we choose to omit these variables, the shape will be stroked but not filled:

>>> pdf.rect(inch, inch, inch * 2, inch * 2, stroke = True, fill
= True)
>>> pdf.circle(inch * 5, inch * 5, inch)
>>> pdf.circle(inch * 5, inch * 5, inch * .5, False, True)

Save the page and then take a look at the result:

>>> pdf.showPage()
>>> pdf.save()

If the shape that you want isn't available, you can also draw it within the ReportLab Toolkit yourself, using paths. With paths, it's possible to draw lines from point to point to construct a shape. After that, you can stroke and fill the shape. Let's set colors, first. Our shape will be stroked with blue and filled with red:

>>> pdf.setStrokeColorRGB(0, 0, 1)
>>> pdf.setFillColorRGB(1, 0, 0)

Next, we have to create a path object to be used:

>>> path = pdf.beginPath()

Before we begin drawing, let's move it to a starting point:

>>> path.moveTo(inch * 4, inch * 4)

Now let's get to the drawing part. We'll create three lines that form a triangle by using the lineTo method:

>>> path.lineTo(inch * 3, inch * 4)
>>> path.lineTo(inch * 3.5, inch * 5)
>>> path.lineTo(inch * 4, inch * 4)

When we're done with a path, we simply have to draw it. We can specify whether we want a stroke or a fill or both:

>>> pdf.drawPath(path, True, True)

Save the page and take a look at the triangle:

>>> pdf.showPage()
>>> pdf.save()

We're not limited to drawing everything by hand. It's also possible to draw existing images into a PDF document. For example, let's draw the DevShed symbol on a page:

Images can be drawn using the drawImage method of a canvas object, which also returns the image dimensions:

>>> pdf.drawImage("devshed.jpg", inch, inch * 10)
(34, 24)
>>> pdf.showPage()
>>> pdf.save()

If you're using Platypus, you'll want to use the Image flowable object instead:

>>> from reportlab.platypus import Image
>>> pdf = SimpleDocTemplate("logoDoc.pdf")
>>> pdf.build([Image("devshed.gif")])

Conclusion

The Portable Document Format is very popular because of its ability to render pages that look exactly the same in many environments. There are many libraries out there that deal with generating PDF documents dynamically, and the ReportLab Toolkit is one of those libraries. In this article, we examined the low-level pdfgen module, which allows text and images to be positioned precisely on a page. While this approach is fine for many purposes, it becomes impractical when dealing with larger amounts of content. For those situations, Platypus is the tool of choice. It takes care of things such as word wrapping and page breaks for us, allowing us to spend more time on other things.

You should now be familiar with the basics of the ReportLab Toolkit. From here, try to create scripts that generate dynamic PDF documents from data sources, such as text files (which we took a look at already, though you can certainly attempt to improve our script) and databases.



 
 
>>> 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: