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