[reportlab-users] TableOfContents addEntry doesn't work?
Robin Becker
robin at reportlab.com
Fri Feb 16 11:59:31 EST 2018
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:
> 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.
>
> Here is my code:
> https://gist.github.com/driscollis/69d4ef8a3f3714484e0788d747b3d5d5
>
> 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 <https://gum.co/py101>, Python 201: Intermediate Python
> <https://gum.co/py201>
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>
--
Robin Becker
More information about the reportlab-users
mailing list