[reportlab-users] Variable (data-driven) page footers

Tim Meneely meneelyt at gmail.com
Thu Oct 6 18:38:19 EDT 2011


I'm sorry; I'm a newbie and I know my thinking is fundamentally
flawed, but I've spent a long time searching and trying and I can't
find how to accomplish a simple-ish task.

I'm in django, printing from a database model; it happens to be a list
of families. I want a report with a variable number of pages per
family. How many pages depends upon family details - such as how many
people are in the family. Each family in the list always starts on a
new page. I want to put a footer on every page with the family name.

I'll paste what I've tried most recently below... but it doesn't work
and I knew it was wrong before I wrote it. The footer is constant
throughout the document, with "I want this to match the family on the
page: Family D". "ThisHousehold" apparently is set when doc.build is
called. I need the page footer to be updated during the build.

Help, please? Pointers to examples? (I did search.)

Thanks,
Tim Meneely
Pittsburgh PA USA

# Part of a django view.py
def some_view(request):
ThisHousehold = 'placeholder_to_overwrite' # A global that doesn't work
def ThisHouseholdIs():
# An attempt to make a "callback"... which of course is pitiful
return ThisHousehold

class SimpleDocTemplateTKM(SimpleDocTemplate):
def beforePage(self):
self.canv.saveState()
self.canv.drawString(0.5*inch, 0.5*inch, 'I want this to
match the family on the page: '+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)
ThisHousehold = h
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