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  
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 
Moblin 
JMSL Numerical Library 
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


    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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...





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