def test0(self): """Test story with TOC and a cascaded header hierarchy. The story should contain exactly one table of contents that is immediatly followed by a list of of cascaded levels of header lines, each nested one level deeper than the previous one. Features to be visually confirmed by a human being are: 1. TOC lines are indented in multiples of 1 cm. 2. Wrapped TOC lines continue with additional 0.5 cm indentation. 3. Only entries of every second level has links ... """ maxLevels = 12 # Create styles to be used for document headers # on differnet levels. headerLevelStyles = [] for i in range(maxLevels): headerLevelStyles.append(makeHeaderStyle(i)) # Create styles to be used for TOC entry lines # for headers on differnet levels. tocLevelStyles = [] d, e = tableofcontents.delta, tableofcontents.epsilon for i in range(maxLevels): tocLevelStyles.append(makeTocHeaderStyle(i, d, e)) # Build story. story = [] styleSheet = getSampleStyleSheet() bt = styleSheet['BodyText'] description = '<font color=red>%s</font>' % self.test0.__doc__ story.append(XPreformatted(description, bt)) toc = tableofcontents.TableOfContents() toc.levelStyles = tocLevelStyles story.append(toc) for i in range(maxLevels): story.append( Paragraph('HEADER, LEVEL %d' % i, headerLevelStyles[i])) #now put some body stuff in. txt = xmlEscape(randomtext.randomText(randomtext.PYTHON, 5)) para = Paragraph(txt, makeBodyStyle()) story.append(para) path = outputfile('test_platypus_toc.pdf') doc = MyDocTemplate(path) doc.multiBuild(story)
def test1(self): """This shows a table which would take more than one page, and need multiple passes to render. But we preload it with the right headings to make it go faster. We used a simple 100-chapter document with one level. """ chapters = 30 #goes over one page headerStyle = makeHeaderStyle(0) d, e = tableofcontents.delta, tableofcontents.epsilon tocLevelStyle = makeTocHeaderStyle(0, d, e) bodyStyle = makeBodyStyle() # Build most of the story; we'll re-use it to # make documents with different numbers of passes. story = [] styleSheet = getSampleStyleSheet() bt = styleSheet['BodyText'] description = '<font color=red>%s</font>' % self.test1.__doc__ story.append(XPreformatted(description, bt)) for i in range(chapters): story.append(PageBreak()) story.append(Paragraph('This is chapter %d' % (i + 1), headerStyle)) #now put some lengthy body stuff in. for paras in range(random.randint(1, 3)): txt = xmlEscape(randomtext.randomText(randomtext.PYTHON, 5)) para = Paragraph(txt, bodyStyle) story.append(para) #try 1: empty TOC, 3 passes toc = tableofcontents.TableOfContents() toc.levelStyles = [tocLevelStyle] #only need one story1 = [toc] + story path = outputfile('test_platypus_toc_preload_1.pdf') doc = MyDocTemplate(path) passes = doc.multiBuild(story1) self.assertEquals(passes, 3) #try 2: now preload the TOC with the entries toc = tableofcontents.TableOfContents() toc.levelStyles = [tocLevelStyle] #only need one tocEntries = [] for i in range(chapters): #add tuple of (level, text, pageNum, key) #with an initial guess of pageNum=0 tocEntries.append((0, 'This is chapter %d' % (i + 1), 0, None)) toc.addEntries(tocEntries) story2 = [toc] + story path = outputfile('test_platypus_toc_preload_2.pdf') doc = MyDocTemplate(path) passes = doc.multiBuild(story2) self.assertEquals(passes, 2) #try 3: preload again but try to be really smart and work out #in advance what page everything starts on. We cannot #use a random story for this. toc3 = tableofcontents.TableOfContents() toc3.levelStyles = [tocLevelStyle] #only need one tocEntries = [] for i in range(chapters): #add tuple of (level, text, pageNum, key) #with an initial guess of pageNum= 3 title = ( 'This is chapter %d' % (i + 1) ) if i != 9 else '<a href="http://www.reportlab.com">onelink</a>' tocEntries.append((0, title, 2 + i, None)) toc3.addEntries(tocEntries) story3 = [toc3] for i in range(chapters): story3.append(PageBreak()) title = ( 'This is chapter %d' % (i + 1) ) if i != 9 else '<a href="http://www.reportlab.com">onelink</a>' story3.append(Paragraph(title, headerStyle)) txt = """ The paragraphs in this are not at all random, because we need to be absolutely, totally certain they will fit on one page. Each chapter will be one page long. """ para = Paragraph(txt, bodyStyle) story3.append(para) path = outputfile('test_platypus_toc_preload_3.pdf') doc = MyDocTemplate(path) passes = doc.multiBuild(story3)