[reportlab-users] PageTemplates or not?

Thomas Zehbe tz at ingenion.de
Thu May 12 03:28:57 EDT 2005


Hi Juergen,
PageTemplates are the way to go, I thing. I`m generating letter form documents 
from within an application just the way you want 2 do (i believe :-).

I derive a Doc template like this:

class BriefDocTemplate(BaseDocTemplate):
    ...
    def __init__(self, filename, **kw):
        frame1 = Frame(LM, BM, PW-LM-RM, PH-BM-10.5*cm, id='F1', 
leftPadding=0)
        frame2 = Frame(LM, BM, PW-LM-RM, PH-BM-TM, id='F2', leftPadding=0)
        #self.allowSplitting = 0
        apply(BaseDocTemplate.__init__, (self, filename), kw)
        template = PageTemplate('First', [frame1], reportFirstPage)
        self.addPageTemplates(template)
        template = PageTemplate('Next', [frame2], reportLaterPages)
        self.addPageTemplates(template)
        ...

where the report*Page functions look like this:

def reportFirstPage(canvas, doc):
    canvas.saveState()
    fontsize = 7
    canvas.setFont("Helvetica", fontsize)
    canvas.drawString(LM, PH-5*cm, doc.Absender)
    canvas.line(LM, PH-5.05*cm, LM+6.3*cm, PH-5.05*cm)
    fontsize = 9
    canvas.setFont("Helvetica", fontsize)
    canvas.drawString(LM, PH-9*cm, "Gesprächspartner:")
    canvas.drawString(LM, PH-9*cm-1.2*fontsize, doc.bearbeiter.namevoll)
    if doc.bearbeiter.fon != '':
        fon = doc.bearbeiter.fon
    else:
        fon = "(05 11) xxxxx
    if doc.bearbeiter.fax != '':
        fax = doc.bearbeiter.fax
    else:
        fax = "(05 11) xxxxxx"
    canvas.saveState()
    fontsize = 9
    canvas.setFont("Helvetica-Bold", fontsize)
    canvas.drawString(PW-6.5*cm, PH-6*cm, 'Telefon: ')
    canvas.drawString(PW-5*cm, PH-6*cm, fon)
    canvas.drawString(PW-6.5*cm, PH-6*cm-fontsize*1.2, 'Telefax: ')
    canvas.drawString(PW-5*cm, PH-6*cm-fontsize*1.2, fax)
    canvas.drawString(PW-6.5*cm, PH-6*cm-fontsize*2.4, "Mail: ")
    canvas.drawString(PW-5*cm, PH-6*cm-fontsize*2.4, doc.bearbeiter.email)
    ....

def reportLaterPages(canvas, doc):
    canvas.saveState()
    canvas.drawImage(logo,PW-7.0*cm, PH-2.5*cm, 240*0.68, 34*0.68)
    drawBottom(canvas)
    canvas.setFont("Helvetica", 9)
    canvas.drawRightString(PW-RM-1*cm, BM, "Seite %d" % doc.page)
    canvas.restoreState()

And then in the "BriefDocTemplate" class you have to override a predefined 
event handler:

    def handle_pageBegin(self):
        '''override base method to add a change of page template after the 
firstpage.
        '''
        self._handle_pageBegin()
        self._handle_nextPageTemplate('Next')

You use this using something like this:

        doc = BriefDocTemplate(docfile)
        doc.setDatum(aufRec['docdatum'])
        doc.setBearbeiter(bearbeiter)
        .....
        Story = []; #[Spacer(cm, 1.3*cm)]
        ...
        p = Paragraph("<b>BESTELLUNG</b>", styBetreff3)
        Story.append(p)
        ...
        try:
            doc.build(Story) 
        except:
            print "Pfad <%s> nicht vorhanden" % docfile

Hope this helps a bit. If you have further questions, feel free to ask. I´ll 
try to remember, what I´ve done ...
Regards
-- 
Thomas Zehbe

INGENION GmbH
Luther Weg 50
31515 Wunstorf

Tel 05031-902042
Fax 05031-902049
www.ingenion.de

Am Dienstag, 10. Mai 2005 18:55 schrieb Kumula Development Team:
> Hi people,
>
> currently I'm working on the themeable business letter generation for
> "Kumula Letters" (http://www.kumula.org). But there is still one problem to
> be fixed, though I know the toolkit dokumentation by heart already... ;-)
>
> If I want to make a business letter template, I need on the first page a
> Frame for the address field, another Frame for the meta data (like current
> date, user's sign etc.) and of course the Frame for the main text. On later
> pages, I still need the main text and optional the meta data, but no
> address field anymore.
>
> The problem is: If I make the Frames without PageTemplates, just on the
> canvas, the result is partly how it should be: separate areas for different
> contents. But this way I have no chance to use ReportLab's automatical
> wrapping functions, if the main text has to be spread over more pages. If I
> use the whole Platypus functionality instead and place the frames on a
> PageTemplate, the text is wrapped through the different frames, but I don't
> want this for the address field and the meta data.
>
> To cut a long story short: What is the best way to implement a business
> letter template? Do there exist any demos anywhere?
>
> Maybe somebody can help me, that would be nice... :-)
>
> Greetz,
>   Juergen
>
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users




More information about the reportlab-users mailing list