[reportlab-users] TableOfContents addEntry doesn't work?

613 views
Skip to first unread message

Mike Driscoll

unread,
Feb 15, 2018, 9:21:03 PM2/15/18
to reportlab-users
Hi,

I was trying to follow along with ReportLab's user guide's instruction on creating a table of contents using the addEntry method and I can't seem to get it to work. It just keeps adding a TOC placeholder in instead of adding the actual table of contents.


Could someone tell me what I am doing wrong?

I have tried the afterFlowable() override that it mentions as well and that works, but I wanted to know why doing it the other way does not.

Thanks,
Mike

-----------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org
Books: Python 101, Python 201: Intermediate Python 

Robin Becker

unread,
Feb 16, 2018, 5:03:41 AM2/16/18
to reportlab-users
Hi Mike,

you hit on a dirty secret of rendering processes. The toc is intended to be created alongside the rendering process which makes it
much harder to do as you intend which is to construct a static story with a TOC. TableOfContents relies on there being a change in
the table of contents during the build and only the last version gets to be shown.
--
Robin Becker
> Books: Python 101 <https://gum.co/py101>, Python 201: Intermediate Python
> <https://gum.co/py201>
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Robin Becker

unread,
Feb 16, 2018, 12:00:32 PM2/16/18
to reportlab-users
Hi again Mike,

to illustrate what I mean about the rendering time try this modification of your script which puts the addEntry into the rendering
process

############################################################################
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Flowable
from reportlab.platypus import PageBreak
from reportlab.platypus.tableofcontents import TableOfContents

class DelayedRef(Flowable):
_ZEROSIZE = True
def __init__(self, toc, *args):
self.args = args
self.toc = toc

def wrap(self,w,h):
return 0,0

def draw(self,*args,**kwd):
self.toc.addEntry(*self.args)

def simple_toc():
doc = SimpleDocTemplate("simple_toc.pdf")
story = []
styles = getSampleStyleSheet()

toc = TableOfContents()
toc.levelStyles = [
ParagraphStyle(fontName='Helvetica', fontSize=14, name='Heading1',
leftIndent=20, firstLineIndent=-20, spaceBefore=5,
leading=16),
ParagraphStyle(fontName='Times-Roman', fontSize=14, name='Heading2',
leftIndent=20, firstLineIndent=-20, spaceBefore=5,
leading=16),
]
story.append(toc)

ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.'''

para = Paragraph("The Beginning", style=styles['Heading1'])
story.append(DelayedRef(toc, 0, 'The Beginning', 1))
story.append(para)
para = Paragraph(ipsum, style=styles['Normal'])
story.append(para)
story.append(PageBreak())

para = Paragraph("The Middle", style=styles['Heading1'])
story.append(DelayedRef(toc, 0, 'The Middle', 2))
story.append(para)
para = Paragraph("The Middle Sub-Header", style=styles['Heading2'])
story.append(DelayedRef(toc, 1, 'The Middle Sub-Header', 2))
story.append(para)
para = Paragraph(ipsum, style=styles['Normal'])
story.append(para)
story.append(PageBreak())

para = Paragraph("The End", style=styles['Heading1'])
story.append(DelayedRef(toc, 0, 'The End', 3))
story.append(para)

doc.multiBuild(story)

if __name__ == '__main__':
simple_toc()
############################################################################
On 16/02/2018 02:19, Mike Driscoll wrote:
> Books: Python 101 <https://gum.co/py101>, Python 201: Intermediate Python
> <https://gum.co/py201>
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


--
Robin Becker
Reply all
Reply to author
Forward
0 new messages