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