[reportlab-users] build vs. multiBuild with TableOfContents()

Sebastian Ware sebastian at urbantalk.se
Fri Oct 12 12:12:50 EDT 2007

I got it to work :) by adding the following custom class:

from reportlab.platypus.doctemplate import _doNothing, PageTemplate,
from reportlab.platypus.frames import Frame
from reportlab.platypus import SimpleDocTemplate
class VisitDocTemplate(SimpleDocTemplate):
"The document template used for all PDF documents."

def afterFlowable(self, flowable):
"Registers TOC entries and makes outline entries."
if flowable.__class__.__name__ == 'Paragraph':
styleName = flowable.style.name
if styleName[:1] == 'h':
# Register TOC entries.
level = int(styleName[1:]) - 1
text = flowable.getPlainText()
pageNum = self.page
self.notify('TOCEntry', (level, text, pageNum))

# Add PDF outline entries (not really needed/tested
key = str(hash(flowable))
c = self.canv
c.addOutlineEntry(text, key, level=level, closed=0)

def multiBuild(self,flowables,onFirstPage=_doNothing,
onLaterPages=_doNothing, canvasmaker=canvas.Canvas):
"""build the document using the flowables. Annotate the
first page using the onFirstPage
function and later pages using the onLaterPages
function. The onXXX pages should follow
the signature

def myOnFirstPage(canvas, document):
# do annotations and modify the document

The functions can do things like draw logos, page
footers, etcetera. They can use external variables to
the look (for example providing page numbering or
section names).
self._calc() #in case we changed margins sizes etc
frameT = Frame(self.leftMargin, self.bottomMargin,
self.width, self.height, id='normal')
(id='First',frames=frameT, onPage=onFirstPage,pagesize=self.pagesize),
if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
self.pageTemplates[0].beforeDrawPage = self.onFirstPage
if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
self.pageTemplates[1].beforeDrawPage = self.onLaterPages

Mvh Sebastian

12 okt 2007 kl. 17.49 skrev Sebastian Ware:

> Hi!


> I am working to render my TableOfContents(). All I get is the

> placeholder text.


> I can see from the test that you are using...


> doc.multiBuild(story)


> ...whereas I have used (SimpleDocTemplate())...


> doc.build(story, onFirstPage=self.myFirstPage,

> onLaterPages=self.myLaterPages)


> Do I have to use multiBuild? And if so, how would I keep the

> onFirstPage, onLaterPages behavior? (multiBuild doesn't take the

> onFirstPage, onLaterPages parameters :( )


> Mvh Sebastian


> _______________________________________________

> 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