Python
  Home arrow Python arrow Page 2 - Python for PDF Generation
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python for PDF Generation
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 51
    2006-03-14


    Table of Contents:
  • Python for PDF Generation
  • Putting Virtual Ink to Virtual Paper
  • Text Formatting Techniques
  • Using Graphics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python for PDF Generation - Putting Virtual Ink to Virtual Paper
    ( Page 2 of 4 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek