[reportlab-users] Variable (data-driven) page footers
    Tim Meneely 
    meneelyt at gmail.com
       
    Sat Oct  8 20:36:16 EDT 2011
    
    
  
Solved!
For anyone finding this in posterity: I figured it out, or at least
came up with _a_ way to do it. Fundamental issues I had were:
- My variable (ThisHousehold) had to be a property of the DocTemplate
(SimpleDocTemplateTKM) in my case.
- Switching from "beforePage" to "afterPage" - that actually makes sense to me.
- The "DocAssign" flowable. I needed to make the assignment as part of
the story creation rather than as part of building the doc...
I'm not sure I totally understand the dynamics of what's going on
here, but a version that does what I want is below. I learned a lot
meanwhile - in particular the value of reading and re-reading the
manual.
Tim Meneely
Pittsburgh, PA USA
# Part of a django view.py
def some_view(request):
    class SimpleDocTemplateTKM(SimpleDocTemplate):
        ThisHousehold = 'placeholder_to_overwrite'
        def ThisHouseholdIs(self):
            return self.ThisHousehold
        def afterPage(self):
            self.canv.saveState()
            self.canv.drawString(0.5*inch, 0.5*inch, 'I want this to
match the family on the page: '+self.ThisHouseholdIs())
            self.canv.restoreState()
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'filename=test.pdf'
    doc = SimpleDocTemplateTKM(response)
    styles = getSampleStyleSheet()
    styleN = styles['Normal']
    my_table_of_families = ['Family A','Family B','Family C','Family D']
    Story = []
    for h in my_table_of_families:
        members = random.randrange(10, 100)
        Story.append(DocAssign('doc.ThisHousehold',"'"+h+"'")) # Very
cool - allows me to perform a variable assignment while the story is
being built.
        Story.append(Paragraph("This story is about the household: "+h,styleN))
        for i in range(members):
            Story.append(Paragraph("This would be details about
members of household: "+h,styleN))
        Story.append(Paragraph("This concludes the story about the
household: "+h,styleN))
        Story.append(PageBreak())
    doc.build(Story)
    return response
    
    
More information about the reportlab-users
mailing list