Python
  Home arrow Python arrow Page 4 - 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 - Using Graphics
    ( Page 4 of 4 )

    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
     

       

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek