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
138
139
140
141
142
143
|
#copyright ReportLab Inc. 2000
#see rllicense.txt for license details
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/docs/tools/rltemplate.py?cvsroot=reportlab
#$Header$
# doc template for RL manuals. Currently YAML is hard-coded
#to use this, which is wrong.
from reportlab.platypus import PageTemplate, \
BaseDocTemplate, SimpleDocTemplate, Frame, Paragraph
from reportlab.lib.units import inch, cm
from reportlab.rl_config import defaultPageSize
class FrontCoverTemplate(PageTemplate):
def __init__(self, id, pageSize=defaultPageSize):
self.pageWidth = pageSize[0]
self.pageHeight = pageSize[1]
frame1 = Frame(inch,
3*inch,
self.pageWidth - 2*inch,
self.pageHeight - 518, id='cover')
PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def afterDrawPage(self, canvas, doc):
canvas.saveState()
#canvas.drawImage('../images/replogo.gif',2*inch, 8*inch)
canvas.setFont('Times-Roman', 10)
canvas.line(inch, 120, self.pageWidth - inch, 120)
#canvas.drawString(inch, 100, 'Lombard Business Park')
#canvas.drawString(inch, 88, '8 Lombard Road')
#canvas.drawString(inch, 76, 'Wimbledon')
#canvas.drawString(inch, 64, 'London, ENGLAND SW19 3TZ')
#canvas.drawRightString(self.pageWidth - inch, 100, '103 Bayard Street')
#canvas.drawRightString(self.pageWidth - inch, 88, 'New Brunswick')
#canvas.drawRightString(self.pageWidth - inch, 76, 'New Jersey, 08904)')
#canvas.drawRightString(self.pageWidth - inch, 64, 'USA')
canvas.restoreState()
class OneColumnTemplate(PageTemplate):
def __init__(self, id, pageSize=defaultPageSize):
self.pageWidth = pageSize[0]
self.pageHeight = pageSize[1]
frame1 = Frame(inch,
inch,
self.pageWidth - 2*inch,
self.pageHeight - 2*inch,
id='normal')
PageTemplate.__init__(self, id, [frame1]) # note lack of onPage
def afterDrawPage(self, canvas, doc):
y = self.pageHeight - 50
canvas.saveState()
canvas.setFont('Times-Roman', 10)
canvas.drawString(inch, y+8, doc.title)
canvas.drawRightString(self.pageWidth - inch, y+8, doc.chapter)
canvas.line(inch, y, self.pageWidth - inch, y)
canvas.drawCentredString(doc.pagesize[0] / 2, 0.75*inch, 'Page %d' % canvas.getPageNumber())
canvas.restoreState()
class TwoColumnTemplate(PageTemplate):
def __init__(self, id, pageSize=defaultPageSize):
self.pageWidth = pageSize[0]
self.pageHeight = pageSize[1]
colWidth = 0.5 * (self.pageWidth - 2.25*inch)
frame1 = Frame(inch,
inch,
colWidth,
self.pageHeight - 2*inch,
id='leftCol')
frame2 = Frame(0.5 * self.pageWidth + 0.125,
inch,
colWidth,
self.pageHeight - 2*inch,
id='rightCol')
PageTemplate.__init__(self, id, [frame1, frame2]) # note lack of onPage
def afterDrawPage(self, canvas, doc):
y = self.pageHeight - 50
canvas.saveState()
canvas.setFont('Times-Roman', 10)
canvas.drawString(inch, y+8, doc.title)
canvas.drawRightString(self.pageWidth - inch, y+8, doc.chapter)
canvas.line(inch, y, self.pageWidth - inch, y*inch)
canvas.drawCentredString(doc.pagesize[0] / 2, 0.75*inch, 'Page %d' % canvas.getPageNumber())
canvas.restoreState()
# class RLDocTemplate(BaseDocTemplate):
class RLDocTemplate(SimpleDocTemplate):
def afterInit(self):
self.addPageTemplates(FrontCoverTemplate('Cover', self.pagesize))
self.addPageTemplates(OneColumnTemplate('Normal', self.pagesize))
self.addPageTemplates(TwoColumnTemplate('TwoColumn', self.pagesize))
self.addPageTemplates(OneColumnTemplate('Cover', self.pagesize))
#just playing
self.title = "(Document Title Goes Here)"
self.chapter = "(No chapter yet)"
self.chapterNo = 1 #unique keys
self.sectionNo = 1 # unique keys
## # AR hack
## self.counter = 1
def beforeDocument(self):
self.canv.showOutline()
def afterFlowable(self, flowable):
"""Detect Level 1 and 2 headings, build outline,
and track chapter title."""
if isinstance(flowable, Paragraph):
style = flowable.style.name
## #AR debug text
## try:
## print '%d: %s...' % (self.counter, flowable.getPlainText()[0:40])
## except AttributeError:
## print '%d: (something with ABag)' % self.counter
## self.counter = self.counter + 1
if style == 'Title':
self.title = flowable.getPlainText()
elif style == 'Heading1':
self.chapter = flowable.getPlainText()
key = 'ch%d' % self.chapterNo
self.canv.bookmarkPage(key)
self.canv.addOutlineEntry(flowable.getPlainText(),
key, 0, 0)
self.chapterNo = self.chapterNo + 1
self.sectionNo = 1
elif style == 'Heading2':
self.section = flowable.text
key = 'ch%ds%d' % (self.chapterNo, self.sectionNo)
self.canv.bookmarkPage(key)
self.canv.addOutlineEntry(flowable.getPlainText(),
key, 1, 0)
self.sectionNo = self.sectionNo + 1
|