Python
  Home arrow Python arrow Page 3 - 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 - Text Formatting Techniques
    ( Page 3 of 4 )

    Drawing strings of text one at a time is fine and good for some purposes, like above when we only needed to place two distinct lines of text. However, imagine creating a bigger document. Positioning individual strings of text would get real boring real fast. Thankfully, there are other ways to position text.

    Text objects allow for larger amounts of text to be added to a page, starting from a given point. You can pass any number of lines into one, and it will put space in between them. To create a text object, the beginText method must be called, which accepts the starting coordinates and returns a text object:

    >>> rhyme = pdf.beginText(inch * 1, inch * 10)

    Calling the textLine method allows a line of text to be added. Further calls will each be put on a new line:

    >>> rhyme.textLine("Humpty Dumpty sat on a wall.")
    >>> rhyme.textLine("Humpty Dumpty had a great fall.")
    >>> rhyme.textLine("All the king's horses and all the king's
    men")
    >>> rhyme.textLine("Couldn't put Humpty together again.")

    After all of the necessary text is added, the text object must be drawn to the page:

    >>> pdf.drawText(rhyme)

    Saving and viewing the page will show that it has been formatted nicely:

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

    It's also possible to draw all of the lines in the same method call by using the textLines method. The following code will produce a result identical to the previous page, but it requires less work than the previous approach:

    >>> rhyme = pdf.beginText(inch * 1, inch * 10)
    >>> rhyme.textLines("""Humpty Dumpty sat on a wall.
    Humpty Dumpty had a great fall.
    All the king's horses and all the king's men
    Couldn't put Humpty together again.""")
    >>> pdf.drawText(rhyme)
    >>> pdf.showPage()
    >>> pdf.save()

    However, this approach still has its problems. Consider long documents that need to have standard headers, standard footers and wrapped words. Using text objects to do everything isn't entirely practical for documents like this. A tool called Platypus (Page Layout and Typography Using Scripts) exists which can make everything a lot easier. Generating a very simple PDF document with Platypus isn't difficult, either. We'll use simplified tools to get introduced to Platypus. The first step is to import the required modules:

    >>> from reportlab.platypus import Paragraph, SimpleDocTemplate,
    Spacer
    >>> from reportlab.lib.styles import getSampleStyleSheet

    Now that we've imported what we need, it's necessary to call getSampleStyleSheet to get what its name implies--a simple style sheet that we can use:

    >>> style = getSampleStyleSheet()

    Next, we create an instance of SimpleDocTemplate, which will be used to structure our document:

    >>> pdf = SimpleDocTemplate("testplatypus.pdf", pagesize =
    letter)

    Just as in creating a canvas, you're required to pass a filename, which may be either absolute or relative to the current working directory.

    We now have to create what's called a story. A story is basically a list of elements (which are termed flowables by Platypus) to be used within a page:

    >>> story = []

    We'll also need some text for a paragraph (which we will repeat multiple times for an example):

    >>> text = "Paragraphs are quite easy to create with Platypus, and Platypus handles things like word wrapping for you. There's not a lot of coding work involved if you wish to create something simple."

    We'll loop through and create twenty-five paragraphs for our document, with a half-inch space after each one. All of this will need to be added to our Platypus story:

    >>> for x in xrange(25):
          para = Paragraph(text, style["Normal"])
          story.append(para)
          story.append(Spacer(inch * .5, inch * .5))

    Finally, we can generate the resulting document:

    >>> pdf.build(story)

    Another interesting thing about Platypus it the ability to embed formatting tags inside of paragraphs. It works like this:

    >>> pdf = SimpleDocTemplate("testplatypus.pdf", pagesize = letter)
    >>> story = []
    >>> for color in ["red", "green", "blue"]:
          para = Paragraph("<font color='%s'>This is <b>%
    s</b>.</font>" % (color, color), style["Normal"])
          story.append(para)
          story.append(Spacer(inch * .5, inch * .5))     

    >>> pdf.build(story)

    The result is three paragraphs, each half an inch apart and in a different color.

    Platypus provides a huge benefit over the pdfgen module when it comes to quickly and efficiently generating dynamic documents. Consider this script, which takes the contents of a simple text file and generates a PDF document:

    import sys

    from reportlab.lib.pagesizes import letter
    from reportlab.lib.styles import getSampleStyleSheet
    from reportlab.lib.units import inch
    from reportlab.platypus import Paragraph, SimpleDocTemplate,
    Spacer

    if len(sys.argv) < 3:
        print "Usage: <script> textfile pdffile"
        sys.exit()
    else:
        pdf = SimpleDocTemplate(sys.argv[2], pagesize = letter)
        story = []
        style = getSampleStyleSheet()
        text = file(sys.argv[1]).read()
        paragraphs = text.split("\n")
        for para in paragraphs:
            story.append(Paragraph(para, style["Normal"]))
            story.append(Spacer(0, inch * .1))
        pdf.build(story)

    The script is used like this:

    $ python texttopdf.py textDocument.txt pdfToCreate.txt



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