author | rgbecker |
Wed, 25 Oct 2000 08:57:46 +0000 | |
changeset 494 | 54257447cfe9 |
parent 405 | 8ee871d827a2 |
child 500 | 58d712fef651 |
permissions | -rw-r--r-- |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab |
|
4 |
#$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.29 2000/10/25 08:57:45 rgbecker Exp $ |
|
5 |
__version__=''' $Id: doctemplate.py,v 1.29 2000/10/25 08:57:45 rgbecker Exp $ ''' |
|
197 | 6 |
__doc__=""" |
268 | 7 |
This module contains the core structure of platypus. |
8 |
||
305 | 9 |
Platypus constructs documents. Document styles are determined by DocumentTemplates. |
268 | 10 |
|
11 |
Each DocumentTemplate contains one or more PageTemplates which defines the look of the |
|
12 |
pages of the document. |
|
13 |
||
14 |
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page |
|
15 |
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and |
|
16 |
a set of Frames which enclose the flowing part of the page (for example the paragraphs, |
|
17 |
tables, or non-fixed diagrams of the text). |
|
18 |
||
19 |
A document is built when a DocumentTemplate is fed a sequence of Flowables. |
|
20 |
The action of the build consumes the flowables in order and places them onto |
|
21 |
frames on pages as space allows. When a frame runs out of space the next frame |
|
22 |
of the page is used. If no frame remains a new page is created. A new page |
|
23 |
can also be created if a page break is forced. |
|
24 |
||
25 |
The special invisible flowable NextPageTemplate can be used to specify |
|
26 |
the page template for the next page (which by default is the one being used |
|
27 |
for the current frame). |
|
197 | 28 |
""" |
279 | 29 |
from reportlab.platypus.flowables import * |
30 |
from reportlab.platypus.paragraph import Paragraph |
|
31 |
from reportlab.platypus.frames import Frame |
|
32 |
import reportlab.lib.sequencer |
|
197 | 33 |
from types import * |
34 |
import sys |
|
35 |
||
253 | 36 |
def _doNothing(canvas, doc): |
37 |
"Dummy callback for onPage" |
|
38 |
pass |
|
39 |
||
197 | 40 |
class ActionFlowable(Flowable): |
268 | 41 |
'''This Flowable is never drawn, it can be used for data driven controls |
42 |
For example to change a page template (from one column to two, for example) |
|
43 |
use NextPageTemplate which creates an ActionFlowable. |
|
44 |
''' |
|
199 | 45 |
def __init__(self,action=[]): |
46 |
if type(action) not in (ListType, TupleType): |
|
47 |
action = (action,) |
|
48 |
self.action = action |
|
197 | 49 |
|
50 |
def wrap(self, availWidth, availHeight): |
|
316 | 51 |
'''Should never be called.''' |
197 | 52 |
raise NotImplementedError |
53 |
||
54 |
def draw(self): |
|
316 | 55 |
'''Should never be called.''' |
197 | 56 |
raise NotImplementedError |
57 |
||
58 |
def apply(self,doc): |
|
316 | 59 |
''' |
60 |
This is called by the doc.build processing to allow the instance to |
|
61 |
implement its behaviour |
|
62 |
''' |
|
199 | 63 |
action = self.action[0] |
64 |
args = tuple(self.action[1:]) |
|
255 | 65 |
arn = 'handle_'+action |
199 | 66 |
try: |
255 | 67 |
apply(getattr(doc,arn), args) |
68 |
except AttributeError, aerr: |
|
69 |
if aerr.args[0]==arn: |
|
70 |
raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action |
|
71 |
else: |
|
72 |
raise |
|
268 | 73 |
except "bogus": |
199 | 74 |
t, v, None = sys.exc_info() |
305 | 75 |
raise t, "%s\n handle_%s args=%s"%(v,action,args) |
197 | 76 |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
77 |
def __call__(self): |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
78 |
return self |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
79 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
80 |
FrameBreak = ActionFlowable('frameEnd') |
197 | 81 |
PageBegin = ActionFlowable('pageBegin') |
82 |
||
83 |
class NextPageTemplate(ActionFlowable): |
|
268 | 84 |
"""When you get to the next page, use the template specified (change to two column, for example) """ |
197 | 85 |
def __init__(self,pt): |
199 | 86 |
ActionFlowable.__init__(self,('nextPageTemplate',pt)) |
197 | 87 |
|
88 |
class PageTemplate: |
|
89 |
""" |
|
227 | 90 |
essentially a list of Frames and an onPage routine to call at the start |
332 | 91 |
of a page when this is selected. onPageEnd gets called at the end. |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
92 |
derived classes can also implement beforeDrawPage and afterDrawPage if they want |
197 | 93 |
""" |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
94 |
def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing): |
197 | 95 |
if type(frames) not in (ListType,TupleType): frames = [frames] |
227 | 96 |
assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error" |
197 | 97 |
self.id = id |
98 |
self.frames = frames |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
99 |
self.onPage = onPage |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
100 |
self.onPageEnd = onPageEnd |
197 | 101 |
|
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
102 |
def beforeDrawPage(self,canv,doc): |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
103 |
"""Override this if you want additional functionality or prefer |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
104 |
a class based page routine. Called before any flowables for |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
105 |
this page are processed.""" |
214 | 106 |
pass |
107 |
||
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
108 |
def afterDrawPage(self, canv, doc): |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
109 |
"""This is called after the last flowable for the page has |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
110 |
been processed. You might use this if the page header or |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
111 |
footer needed knowledge of what flowables were drawn on |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
112 |
this page.""" |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
113 |
pass |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
114 |
|
197 | 115 |
class BaseDocTemplate: |
116 |
""" |
|
117 |
First attempt at defining a document template class. |
|
118 |
||
119 |
The basic idea is simple. |
|
120 |
0) The document has a list of data associated with it |
|
121 |
this data should derive from flowables. We'll have |
|
122 |
special classes like PageBreak, FrameBreak to do things |
|
123 |
like forcing a page end etc. |
|
124 |
||
125 |
1) The document has one or more page templates. |
|
126 |
||
127 |
2) Each page template has one or more frames. |
|
128 |
||
129 |
3) The document class provides base methods for handling the |
|
130 |
story events and some reasonable methods for getting the |
|
131 |
story flowables into the frames. |
|
132 |
||
133 |
4) The document instances can override the base handler routines. |
|
268 | 134 |
|
135 |
Most of the methods for this class are not called directly by the user, |
|
136 |
but in some advanced usages they may need to be overridden via subclassing. |
|
137 |
||
138 |
EXCEPTION: doctemplate.build(...) must be called for most reasonable uses |
|
139 |
since it builds a document using the page template. |
|
140 |
||
310 | 141 |
Each document template builds exactly one document into a file specified |
142 |
by the filename argument on initialization. |
|
143 |
||
268 | 144 |
Possible keyword arguments for the initialization: |
145 |
||
305 | 146 |
pageTemplates: A list of templates. Must be nonempty. Names |
268 | 147 |
assigned to the templates are used for referring to them so no two used |
305 | 148 |
templates should have the same name. For example you might want one template |
268 | 149 |
for a title page, one for a section first page, one for a first page of |
150 |
a chapter and two more for the interior of a chapter on odd and even pages. |
|
151 |
If this argument is omitted then at least one pageTemplate should be provided |
|
152 |
using the addPageTemplates method before the document is built. |
|
310 | 153 |
showBoundary: if set draw a box around the frame boundaries. |
268 | 154 |
leftMargin: |
155 |
rightMargin: |
|
156 |
topMargin: |
|
157 |
bottomMargin: Margin sizes in points (default 1 inch) |
|
158 |
These margins may be overridden by the pageTemplates. They are primarily of interest |
|
159 |
for the SimpleDocumentTemplate subclass. |
|
160 |
allowSplitting: If set flowables (eg, paragraphs) may be split across frames or pages |
|
161 |
(default: 1) |
|
162 |
title: Internal title for document (does not automatically display on any page) |
|
163 |
author: Internal author for document (does not automatically display on any page) |
|
197 | 164 |
""" |
255 | 165 |
_initArgs = { 'pagesize':DEFAULT_PAGE_SIZE, |
253 | 166 |
'pageTemplates':[], |
167 |
'showBoundary':0, |
|
168 |
'leftMargin':inch, |
|
169 |
'rightMargin':inch, |
|
170 |
'topMargin':inch, |
|
171 |
'bottomMargin':inch, |
|
172 |
'allowSplitting':1, |
|
173 |
'title':None, |
|
174 |
'author':None, |
|
175 |
'_pageBreakQuick':1} |
|
176 |
_invalidInitArgs = () |
|
197 | 177 |
|
253 | 178 |
def __init__(self, filename, **kw): |
268 | 179 |
"""create a document template bound to a filename (see class documentation for keyword arguments)""" |
197 | 180 |
self.filename = filename |
253 | 181 |
|
182 |
for k in self._initArgs.keys(): |
|
183 |
if not kw.has_key(k): |
|
184 |
v = self._initArgs[k] |
|
185 |
else: |
|
186 |
if k in self._invalidInitArgs: |
|
187 |
raise ValueError, "Invalid argument %s" % k |
|
188 |
v = kw[k] |
|
189 |
setattr(self,k,v) |
|
190 |
||
191 |
p = self.pageTemplates |
|
192 |
self.pageTemplates = [] |
|
193 |
self.addPageTemplates(p) |
|
194 |
self._calc() |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
195 |
self.afterInit() |
279 | 196 |
|
253 | 197 |
def _calc(self): |
255 | 198 |
self._rightMargin = self.pagesize[0] - self.rightMargin |
199 |
self._topMargin = self.pagesize[1] - self.topMargin |
|
253 | 200 |
self.width = self._rightMargin - self.leftMargin |
201 |
self.height = self._topMargin - self.bottomMargin |
|
197 | 202 |
|
203 |
def clean_hanging(self): |
|
249 | 204 |
'handle internal postponed actions' |
197 | 205 |
while len(self._hanging): |
206 |
self.handle_flowable(self._hanging) |
|
207 |
||
208 |
def addPageTemplates(self,pageTemplates): |
|
268 | 209 |
'add one or a sequence of pageTemplates' |
197 | 210 |
if type(pageTemplates) not in (ListType,TupleType): |
211 |
pageTemplates = [pageTemplates] |
|
212 |
assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error" |
|
213 |
for t in pageTemplates: |
|
214 |
self.pageTemplates.append(t) |
|
215 |
||
216 |
def handle_documentBegin(self): |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
217 |
'''implement actions at beginning of document''' |
197 | 218 |
self._hanging = [PageBegin] |
219 |
self.pageTemplate = self.pageTemplates[0] |
|
220 |
self.page = 0 |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
221 |
self.beforeDocument() |
197 | 222 |
|
223 |
def handle_pageBegin(self): |
|
249 | 224 |
'''Perform actions required at beginning of page. |
225 |
shouldn't normally be called directly''' |
|
197 | 226 |
self.page = self.page + 1 |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
227 |
self.pageTemplate.beforeDrawPage(self.canv,self) |
197 | 228 |
self.pageTemplate.onPage(self.canv,self) |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
229 |
self.beforePage() |
197 | 230 |
if hasattr(self,'_nextFrameIndex'): |
231 |
del self._nextFrameIndex |
|
232 |
self.frame = self.pageTemplate.frames[0] |
|
233 |
self.handle_frameBegin() |
|
234 |
||
235 |
def handle_pageEnd(self): |
|
236 |
''' show the current page |
|
237 |
check the next page template |
|
238 |
hang a page begin |
|
239 |
''' |
|
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
240 |
self.pageTemplate.afterDrawPage(self.canv, self) |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
241 |
self.pageTemplate.onPageEnd(self.canv, self) |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
242 |
self.afterPage() |
197 | 243 |
self.canv.showPage() |
244 |
if hasattr(self,'_nextPageTemplateIndex'): |
|
245 |
self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex] |
|
246 |
del self._nextPageTemplateIndex |
|
247 |
self._hanging.append(PageBegin) |
|
248 |
||
249 |
def handle_pageBreak(self): |
|
250 |
'''some might choose not to end all the frames''' |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
251 |
if self._pageBreakQuick: |
197 | 252 |
self.handle_pageEnd() |
253 |
else: |
|
254 |
n = len(self._hanging) |
|
255 |
while len(self._hanging)==n: |
|
256 |
self.handle_frameEnd() |
|
257 |
||
258 |
def handle_frameBegin(self,*args): |
|
249 | 259 |
'''What to do at the beginning of a page''' |
197 | 260 |
self.frame._reset() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
261 |
if self.showBoundary or self.frame.showBoundary: |
226 | 262 |
self.frame.drawBoundary(self.canv) |
197 | 263 |
|
264 |
def handle_frameEnd(self): |
|
265 |
''' Handles the semantics of the end of a frame. This includes the selection of |
|
266 |
the next frame or if this is the last frame then invoke pageEnd. |
|
267 |
''' |
|
268 |
if hasattr(self,'_nextFrameIndex'): |
|
269 |
frame = self.pageTemplate.frames[self._nextFrameIndex] |
|
270 |
del self._nextFrameIndex |
|
204 | 271 |
self.handle_frameBegin() |
197 | 272 |
elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]: |
273 |
self.handle_pageEnd() |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
274 |
self.frame = None |
197 | 275 |
else: |
276 |
f = self.frame |
|
277 |
self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1] |
|
278 |
self.handle_frameBegin() |
|
279 |
||
280 |
def handle_nextPageTemplate(self,pt): |
|
281 |
'''On endPage chenge to the page template with name or index pt''' |
|
282 |
if type(pt) is StringType: |
|
283 |
for t in self.pageTemplates: |
|
284 |
if t.id == pt: |
|
285 |
self._nextPageTemplateIndex = self.pageTemplates.index(t) |
|
286 |
return |
|
287 |
raise ValueError, "can't find template('%s')"%pt |
|
288 |
elif type(pt) is IntType: |
|
289 |
self._nextPageTemplateIndex = pt |
|
290 |
else: |
|
291 |
raise TypeError, "argument pt should be string or integer" |
|
292 |
||
293 |
def handle_nextFrame(self,fx): |
|
294 |
'''On endFrame chenge to the frame with name or index fx''' |
|
295 |
if type(fx) is StringType: |
|
296 |
for f in self.pageTemplate.frames: |
|
297 |
if f.id == fx: |
|
298 |
self._nextFrameIndex = self.pageTemplate.frames.index(f) |
|
299 |
return |
|
300 |
raise ValueError, "can't find frame('%s')"%fx |
|
301 |
elif type(fx) is IntType: |
|
302 |
self._nextFrameIndex = fx |
|
303 |
else: |
|
304 |
raise TypeError, "argument fx should be string or integer" |
|
305 |
||
306 |
def handle_currentFrame(self,fx): |
|
307 |
'''chenge to the frame with name or index fx''' |
|
308 |
if type(fx) is StringType: |
|
309 |
for f in self.pageTemplate.frames: |
|
310 |
if f.id == fx: |
|
311 |
self._nextFrameIndex = self.pageTemplate.frames.index(f) |
|
312 |
return |
|
313 |
raise ValueError, "can't find frame('%s')"%fx |
|
314 |
elif type(fx) is IntType: |
|
315 |
self._nextFrameIndex = fx |
|
316 |
else: |
|
317 |
raise TypeError, "argument fx should be string or integer" |
|
318 |
||
319 |
def handle_flowable(self,flowables): |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
320 |
'''try to handle one flowable from the front of list flowables.''' |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
321 |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
322 |
#allow document a chance to look at, modify or ignore |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
323 |
#the object(s) about to be processed |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
324 |
self.filterFlowables(flowables) |
197 | 325 |
f = flowables[0] |
326 |
del flowables[0] |
|
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
327 |
if f is None: |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
328 |
return |
197 | 329 |
|
330 |
if isinstance(f,PageBreak): |
|
331 |
self.handle_pageBreak() |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
332 |
self.afterFlowable(f) |
197 | 333 |
elif isinstance(f,ActionFlowable): |
334 |
f.apply(self) |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
335 |
self.afterFlowable(f) |
197 | 336 |
else: |
337 |
#general case we have to do something |
|
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
338 |
if self.frame.add(f, self.canv, trySplit=self.allowSplitting): |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
339 |
self.afterFlowable(f) |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
340 |
else: |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
341 |
if self.allowSplitting: |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
342 |
# see if this is a splittable thing |
325 | 343 |
S = self.frame.split(f,self.canv) |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
344 |
n = len(S) |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
345 |
else: |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
346 |
n = 0 |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
347 |
|
197 | 348 |
if n: |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
349 |
if self.frame.add(S[0], self.canv, trySplit=0): |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
350 |
self.afterFlowable(f) |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
351 |
else: |
200 | 352 |
raise "LayoutError", "splitting error" |
353 |
del S[0] |
|
354 |
for f in xrange(n-1): |
|
197 | 355 |
flowables.insert(f,S[f]) # put split flowables back on the list |
356 |
else: |
|
249 | 357 |
if hasattr(f,'postponed'): |
358 |
raise "LayoutError", "Flowable too large" |
|
359 |
f.postponed = 1 |
|
197 | 360 |
flowables.insert(0,f) # put the flowable back |
361 |
self.handle_frameEnd() |
|
362 |
||
204 | 363 |
#these are provided so that deriving classes can refer to them |
197 | 364 |
_handle_documentBegin = handle_documentBegin |
365 |
_handle_pageBegin = handle_pageBegin |
|
366 |
_handle_pageEnd = handle_pageEnd |
|
367 |
_handle_frameBegin = handle_frameBegin |
|
368 |
_handle_frameEnd = handle_frameEnd |
|
369 |
_handle_flowable = handle_flowable |
|
370 |
_handle_nextPageTemplate = handle_nextPageTemplate |
|
371 |
_handle_currentFrame = handle_currentFrame |
|
372 |
_handle_nextFrame = handle_nextFrame |
|
373 |
||
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
374 |
def _startBuild(self, filename=None, canvasmaker=canvas.Canvas): |
253 | 375 |
self._calc() |
405 | 376 |
self.canv = canvasmaker(filename or self.filename,pagesize=self.pagesize) |
197 | 377 |
self.handle_documentBegin() |
378 |
||
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
379 |
def _endBuild(self): |
197 | 380 |
if self._hanging!=[] and self._hanging[-1] is PageBegin: |
381 |
del self._hanging[-1] |
|
382 |
self.clean_hanging() |
|
383 |
else: |
|
384 |
self.clean_hanging() |
|
385 |
self.handle_pageBreak() |
|
386 |
||
387 |
self.canv.save() |
|
339 | 388 |
#AR - hack - for some reason a document did not |
389 |
#have these: |
|
390 |
#if hasattr(self, 'frame'): del self.frame |
|
391 |
#if hasattr(self, 'pageTemplate'): del self.pageTemplate |
|
197 | 392 |
del self.frame, self.pageTemplate |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
393 |
|
272
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
394 |
def build(self, flowables, filename=None, canvasmaker=canvas.Canvas): |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
395 |
"""Build the document from a list of flowables. |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
396 |
If the filename argument is provided then that filename is used |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
397 |
rather than the one provided upon initialization. |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
398 |
If the canvasmaker argument is provided then it will be used |
305 | 399 |
instead of the default. For example a slideshow might use |
272
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
400 |
an alternate canvas which places 6 slides on a page (by |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
401 |
doing translations, scalings and redefining the page break |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
402 |
operations). |
80107aaffa7e
new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents:
268
diff
changeset
|
403 |
""" |
268 | 404 |
#assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error" |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
405 |
self._startBuild(filename,canvasmaker) |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
406 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
407 |
while len(flowables): |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
408 |
self.clean_hanging() |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
409 |
self.handle_flowable(flowables) |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
410 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
411 |
self._endBuild() |
221 | 412 |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
413 |
#these are pure virtuals override in derived classes |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
414 |
#NB these get called at suitable places by the base class |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
415 |
#so if you derive and override the handle_xxx methods |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
416 |
#it's up to you to ensure that they maintain the needed consistency |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
417 |
def afterInit(self): |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
418 |
"""This is called after initialisation of the base class.""" |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
419 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
420 |
|
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
421 |
def beforeDocument(self): |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
422 |
"""This is called before any processing is |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
423 |
done on the document.""" |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
424 |
pass |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
425 |
|
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
426 |
def beforePage(self): |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
427 |
"""This is called at the beginning of page |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
428 |
processing, and immediately before the |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
429 |
beforeDrawPage method of the current page |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
430 |
template.""" |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
431 |
pass |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
432 |
|
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
433 |
def afterPage(self): |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
434 |
"""This is called after page processing, and |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
435 |
immediately after the afterDrawPage method |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
436 |
of the current page template.""" |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
437 |
pass |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
438 |
|
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
439 |
def filterFlowables(self,flowables): |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
440 |
'''called to filter flowables at the start of the main handle_flowable method. |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
441 |
Upon return if flowables[0] has been set to None it is discarded and the main |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
442 |
method returns. |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
443 |
''' |
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
444 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
445 |
|
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
446 |
def afterFlowable(self, flowable): |
295
1f00bd112cb0
remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents:
294
diff
changeset
|
447 |
'''called after a flowable has been rendered''' |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
448 |
pass |
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
449 |
|
221 | 450 |
class SimpleDocTemplate(BaseDocTemplate): |
268 | 451 |
"""A special case document template that will handle many simple documents. |
452 |
See documentation for BaseDocTemplate. No pageTemplates are required |
|
305 | 453 |
for this special case. A page templates are inferred from the |
268 | 454 |
margin information and the onFirstPage, onLaterPages arguments to the build method. |
455 |
||
456 |
A document which has all pages with the same look except for the first |
|
457 |
page may can be built using this special approach. |
|
305 | 458 |
""" |
322 | 459 |
_invalidInitArgs = ('pageTemplates',) |
221 | 460 |
def handle_pageBegin(self): |
316 | 461 |
'''override base method to add a change of page template after the firstpage. |
462 |
''' |
|
221 | 463 |
self._handle_pageBegin() |
464 |
self._handle_nextPageTemplate('Later') |
|
465 |
||
466 |
def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing): |
|
305 | 467 |
"""build the document using the flowables. Annotate the first page using the onFirstPage |
468 |
function and later pages using the onLaterPages function. The onXXX pages should follow |
|
469 |
the signature |
|
470 |
||
471 |
def myOnFirstPage(canvas, document): |
|
472 |
# do annotations and modify the document |
|
473 |
... |
|
474 |
||
316 | 475 |
The functions can do things like draw logos, page numbers, |
305 | 476 |
footers, etcetera. They can use external variables to vary |
477 |
the look (for example providing page numbering or section names). |
|
478 |
""" |
|
322 | 479 |
self._calc() #in case we changed margins sizes etc |
227 | 480 |
frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') |
221 | 481 |
self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage), |
482 |
PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)]) |
|
225 | 483 |
if onFirstPage is _doNothing and hasattr(self,'onFirstPage'): |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
484 |
self.pageTemplates[0].beforeDrawPage = self.onFirstPage |
225 | 485 |
if onLaterPages is _doNothing and hasattr(self,'onLaterPages'): |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
486 |
self.pageTemplates[1].beforeDrawPage = self.onLaterPages |
221 | 487 |
BaseDocTemplate.build(self,flowables) |
488 |
||
489 |
if __name__ == '__main__': |
|
490 |
########################################################## |
|
491 |
## |
|
492 |
## testing |
|
493 |
## |
|
494 |
########################################################## |
|
495 |
def randomText(): |
|
496 |
#this may or may not be appropriate in your company |
|
497 |
from random import randint, choice |
|
498 |
||
499 |
RANDOMWORDS = ['strategic','direction','proactive', |
|
500 |
'reengineering','forecast','resources', |
|
501 |
'forward-thinking','profit','growth','doubletalk', |
|
502 |
'venture capital','IPO'] |
|
503 |
||
504 |
sentences = 5 |
|
505 |
output = "" |
|
506 |
for sentenceno in range(randint(1,5)): |
|
507 |
output = output + 'Blah' |
|
508 |
for wordno in range(randint(10,25)): |
|
509 |
if randint(0,4)==0: |
|
510 |
word = choice(RANDOMWORDS) |
|
511 |
else: |
|
512 |
word = 'blah' |
|
513 |
output = output + ' ' +word |
|
514 |
output = output+'.' |
|
515 |
return output |
|
516 |
||
517 |
def myFirstPage(canvas, doc): |
|
518 |
canvas.saveState() |
|
519 |
canvas.setStrokeColor(red) |
|
520 |
canvas.setLineWidth(5) |
|
521 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
|
522 |
canvas.setFont('Times-Bold',24) |
|
523 |
canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS") |
|
524 |
canvas.setFont('Times-Roman',12) |
|
525 |
canvas.drawString(4 * inch, 0.75 * inch, "First Page") |
|
526 |
canvas.restoreState() |
|
527 |
||
528 |
def myLaterPages(canvas, doc): |
|
529 |
canvas.saveState() |
|
530 |
canvas.setStrokeColor(red) |
|
531 |
canvas.setLineWidth(5) |
|
532 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
|
533 |
canvas.setFont('Times-Roman',12) |
|
534 |
canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page) |
|
535 |
canvas.restoreState() |
|
536 |
||
537 |
def run(): |
|
538 |
objects_to_draw = [] |
|
539 |
from reportlab.lib.styles import ParagraphStyle |
|
253 | 540 |
#from paragraph import Paragraph |
225 | 541 |
from doctemplate import SimpleDocTemplate |
221 | 542 |
|
543 |
#need a style |
|
544 |
normal = ParagraphStyle('normal') |
|
545 |
normal.firstLineIndent = 18 |
|
546 |
normal.spaceBefore = 6 |
|
547 |
import random |
|
548 |
for i in range(15): |
|
549 |
height = 0.5 + (2*random.random()) |
|
550 |
box = XBox(6 * inch, height * inch, 'Box Number %d' % i) |
|
551 |
objects_to_draw.append(box) |
|
552 |
para = Paragraph(randomText(), normal) |
|
553 |
objects_to_draw.append(para) |
|
554 |
||
253 | 555 |
SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw, |
221 | 556 |
onFirstPage=myFirstPage,onLaterPages=myLaterPages) |
557 |
||
558 |
run() |
|
294 | 559 |