[reportlab-users] Bulleted text

Robin Becker robin at reportlab.com
Fri Jun 30 08:26:52 EDT 2006


Saketh Bhamidipati wrote:
........
>>
>> The problem here is that you don't explain (or perhaps care) what happens
>> when
>> one or other of the frames fills up. There is an example in the
>> test_platypus_general.py script that shows a document template filling
>> parallel
>> frames.
>>
>>
>> The approach there works because we're very careful about the content.
>> -- 
>> Robin Becker
>> _______________________________________________
.....
I copied the AndyTemplate class instead of NotalonTemplate. and changed a couple 
of definitions and that works fine. I don't know what was wrong with your version.

Here's the version that works

#!/usr/bin/python

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Flowable
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
from reportlab.platypus.frames import Frame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.platypus.xpreformatted import XPreformatted
from reportlab.lib.styles import ParagraphStyle, PropertySet
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
from reportlab.lib.units import inch
from reportlab.lib.colors import black
PAGE_HEIGHT = defaultPageSize[1]; PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
def bullet(text):
    return (u"\N{BULLET} " + unicode(text))

Title = "Hello, World!"
Author = "Saketh Bhamidipati"

class AndyTemplate(BaseDocTemplate):
     _invalidInitArgs = ('pageTemplates',)
     def __init__(self, filename, **kw):
         frame1 = Frame(2 * inch, 1 * inch, 5.75 * inch, 9 * inch, id = 'TF', 
showBoundary = 1)
         frame2 = Frame(0.5 * inch, 1 * inch, 1.5 * inch, 9 * inch, id = 'HF', 
showBoundary = 1)
         self.allowSplitting = 0
         apply(BaseDocTemplate.__init__,(self,filename),kw)
 
self.addPageTemplates(PageTemplate('normal',[frame1,frame2],myPageTemplate))

     def fillFrame(self,flowables):
         f = self.frame
         while len(flowables)>0 and f is self.frame:
             self.handle_flowable(flowables)

     def build(self, flowables1, flowables2):
         assert filter(lambda x: not isinstance(x,Flowable), flowables1)==[], 
"flowables1 argument error"
         assert filter(lambda x: not isinstance(x,Flowable), flowables2)==[], 
"flowables2 argument error"
         self._startBuild()
         while (len(flowables1) > 0 or len(flowables1) > 0):
             self.clean_hanging()
             self.fillFrame(flowables1)
             self.fillFrame(flowables2)

         self._endBuild()

def myPageTemplate(canvas, doc):
    PAGE_WIDTH, PAGE_HEIGHT = canvas._pagesize
    canvas.saveState()
    canvas.setFont('Times-Roman', 12)
    canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108, Title)
    canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch, Author)
    canvas.setFont('Times-Roman', 10)
    canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" % doc.page)
    canvas.restoreState()

def drawHeadings(canvas, doc):
    pass

def go():
    doc = AndyTemplate("hanuman.pdf")
    Story = []
    style = styles["Normal"]
    Story.append(Paragraph(bullet("Luke, I <i>am</i> your father. Now die. " * 
200) , style))
    doc.build(Story, Story[:])
go()
-- 
Robin Becker


More information about the reportlab-users mailing list