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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PYTHON

Python for PDF Generation
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 42
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · Hello,PDFs are pretty popular and have been like that for a while. Government...
       · Thanks for the tutorial. I'm on page 1 and have my first PDF in 5 minutes. I'm a...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway