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.") 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() 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) 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, 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 = 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): 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) >>> 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 if len(sys.argv) < 3: The script is used like this: $ python texttopdf.py textDocument.txt pdfToCreate.txt
blog comments powered by Disqus |
|
|
|
|
|
|
|