1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#Copyright ReportLab Europe Ltd. 2000-2017
#see license.txt for license details
"""Test long documents with indexes, tables and cross-references
"""
__version__='3.3.0'
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import sys, os, time, random
from reportlab.rl_config import invariant as rl_invariant
from operator import truth
import unittest
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import Paragraph, Flowable, Frame, PageTemplate, BaseDocTemplate
from reportlab.platypus.frames import Frame
from reportlab.lib.randomtext import randomText, PYTHON
from reportlab.platypus.tableofcontents import TableOfContents, SimpleIndex
def myMainPageFrame(canvas, doc):
"The page frame used for all PDF documents."
canvas.saveState()
canvas.setFont('Times-Roman', 12)
pageNumber = canvas.getPageNumber()
canvas.drawString(10*cm, cm, str(pageNumber))
canvas.restoreState()
class MyDocTemplate(BaseDocTemplate):
_invalidInitArgs = ('pageTemplates',)
def __init__(self, filename, **kw):
frame1 = Frame(2.5*cm, 2.5*cm, 16*cm, 25*cm, id='Frame1')
self.allowSplitting = 0
self.showBoundary = 1
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate('normal', [frame1], myMainPageFrame)
self.addPageTemplates(template)
def afterFlowable(self, flowable):
"Registers TOC and Index entries and makes outline entries."
if flowable.__class__.__name__ == 'Paragraph':
styleName = flowable.style.name
if styleName == 'Heading1':
level = 0
text = flowable.getPlainText()
pageNum = self.page
self.notify('TOCEntry', (level, text, pageNum))
# Add PDF outline entries (not really needed/tested here).
key = str(hash(flowable))
c = self.canv
c.bookmarkPage(key)
c.addOutlineEntry(text, key, level=level, closed=0)
# index a bunch of pythonic buzzwords. In real life this
# would be driven by markup.
try:
text = flowable.getPlainText()
except:
return
for phrase in ['uniform','depraved','finger', 'Fraudulin']:
if text.find(phrase) > -1:
self.notify('IndexEntry', (phrase, self.page))
#print 'IndexEntry:',phrase, self.page
def _test0(self):
"This makes one long multi-page paragraph."
from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
if rl_invariant: random.seed(1629812163)
# Build story.
story = []
styleSheet = getSampleStyleSheet()
h1 = styleSheet['Heading1']
h1.pageBreakBefore = 1
h1.keepWithNext = 1
h1.outlineLevel = 0
h2 = styleSheet['Heading2']
h2.backColor = colors.cyan
h2.keepWithNext = 1
h2.outlineLevel = 1
bt = styleSheet['BodyText']
story.append(Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
story.append(Paragraph("""
Subsequent pages test cross-references: indexes, tables and individual
cross references. The number in brackets at the end of each paragraph
is its position in the story. (%d)""" % len(story), bt))
story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
toc = TableOfContents()
story.append(toc)
chapterNum = 1
for i in range(10):
story.append(Paragraph('Chapter %d: Chapters always starts a new page' % chapterNum, h1))
chapterNum = chapterNum + 1
for j in range(3):
story.append(Paragraph('Heading1 paragraphs should always'
'have a page break before. Heading 2 on the other hand'
'should always have a FRAME break before (%d)' % len(story), bt))
story.append(Paragraph('Heading 2 should always be kept with the next thing (%d)' % len(story), h2))
for j in range(3):
story.append(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
story.append(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h2))
story.append(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))
story.append(Paragraph('The Index which goes at the back', h1))
story.append(SimpleIndex())
doc = MyDocTemplate(outputfile('test_platypus_xref.pdf'))
doc.multiBuild(story)
class BreakingTestCase(unittest.TestCase):
"Test multi-page splitting of paragraphs (eyeball-test)."
def test0(self):
_test0(self)
def makeSuite():
return makeSuiteForClasses(BreakingTestCase)
#noruntests
if __name__ == "__main__":
if 'debug' in sys.argv:
_test1(None)
else:
unittest.TextTestRunner().run(makeSuite())
printLocation()
|