author | rgbecker |
Thu, 01 Jun 2000 15:23:06 +0000 | |
changeset 253 | cfcf8d555a2c |
parent 249 | 1d9ea4f00348 |
child 255 | ee9e321e747d |
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 $ |
|
253 | 34 |
# Revision 1.13 2000/06/01 15:23:06 rgbecker |
35 |
# Platypus re-organisation |
|
36 |
# |
|
249 | 37 |
# Revision 1.12 2000/05/26 10:27:37 rgbecker |
38 |
# Fixed infinite recursion bug |
|
253 | 39 |
# |
227 | 40 |
# Revision 1.11 2000/05/17 22:17:38 rgbecker |
41 |
# Renamed BasicFrame to Frame |
|
249 | 42 |
# |
226 | 43 |
# Revision 1.10 2000/05/17 16:29:40 rgbecker |
44 |
# Removal of SimpleFrame |
|
227 | 45 |
# |
225 | 46 |
# Revision 1.9 2000/05/17 15:37:33 rgbecker |
47 |
# Changes related to removal of SimpleFlowDocument |
|
226 | 48 |
# |
221 | 49 |
# Revision 1.8 2000/05/16 16:15:16 rgbecker |
50 |
# Changes related to removal of SimpleFlowDocument |
|
225 | 51 |
# |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
52 |
# 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
|
53 |
# Fixes/Changes to get testplatypus to work with new framework |
221 | 54 |
# |
214 | 55 |
# Revision 1.6 2000/05/15 15:07:32 rgbecker |
56 |
# Added drawPage |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
57 |
# |
206 | 58 |
# Revision 1.5 2000/05/13 08:33:53 rgbecker |
59 |
# fix typo in import |
|
214 | 60 |
# |
204 | 61 |
# Revision 1.4 2000/05/12 16:21:02 rgbecker |
62 |
# _donothing explicit import |
|
206 | 63 |
# |
200 | 64 |
# Revision 1.3 2000/05/12 14:53:38 rgbecker |
65 |
# Handle splitting error in build |
|
204 | 66 |
# |
199 | 67 |
# Revision 1.2 2000/05/12 14:45:31 rgbecker |
68 |
# Single actions only in ActionFlowables |
|
200 | 69 |
# |
197 | 70 |
# Revision 1.1 2000/05/12 12:53:33 rgbecker |
71 |
# Initial try at a document template class |
|
199 | 72 |
# |
253 | 73 |
__version__=''' $Id: doctemplate.py,v 1.13 2000/06/01 15:23:06 rgbecker Exp $ ''' |
197 | 74 |
__doc__=""" |
75 |
More complicated Document model |
|
76 |
""" |
|
253 | 77 |
from flowables import * |
78 |
from frames import Frame |
|
197 | 79 |
from types import * |
80 |
import sys |
|
81 |
||
253 | 82 |
def _doNothing(canvas, doc): |
83 |
"Dummy callback for onPage" |
|
84 |
pass |
|
85 |
||
197 | 86 |
class ActionFlowable(Flowable): |
87 |
'''This Flowable is never drawn, it can be used for data driven controls''' |
|
199 | 88 |
def __init__(self,action=[]): |
89 |
if type(action) not in (ListType, TupleType): |
|
90 |
action = (action,) |
|
91 |
self.action = action |
|
197 | 92 |
|
93 |
def wrap(self, availWidth, availHeight): |
|
94 |
raise NotImplementedError |
|
95 |
||
96 |
def draw(self): |
|
97 |
raise NotImplementedError |
|
98 |
||
99 |
def apply(self,doc): |
|
199 | 100 |
action = self.action[0] |
101 |
args = tuple(self.action[1:]) |
|
102 |
try: |
|
103 |
apply(getattr(doc,'handle_'+action), args) |
|
104 |
except AttributeError: |
|
105 |
raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action |
|
106 |
except: |
|
107 |
t, v, None = sys.exc_info() |
|
108 |
raise t, "%s\n handle_%s args=%s"%(v,action,args) |
|
197 | 109 |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
110 |
def __call__(self): |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
111 |
return self |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
112 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
113 |
FrameBreak = ActionFlowable('frameEnd') |
197 | 114 |
PageBegin = ActionFlowable('pageBegin') |
115 |
||
116 |
class NextPageTemplate(ActionFlowable): |
|
117 |
def __init__(self,pt): |
|
199 | 118 |
ActionFlowable.__init__(self,('nextPageTemplate',pt)) |
197 | 119 |
|
120 |
class PageTemplate: |
|
121 |
""" |
|
227 | 122 |
essentially a list of Frames and an onPage routine to call at the start |
197 | 123 |
of a page when this is selected. |
214 | 124 |
derived classes can also implement drawPage if they want |
197 | 125 |
""" |
126 |
def __init__(self,id=None,frames=[],onPage=None): |
|
127 |
if type(frames) not in (ListType,TupleType): frames = [frames] |
|
227 | 128 |
assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error" |
197 | 129 |
self.id = id |
130 |
self.frames = frames |
|
131 |
self.onPage = onPage or _doNothing |
|
132 |
||
214 | 133 |
def drawPage(self,canv,doc): |
134 |
''' Override this if you want additional functionality or prefer a class |
|
135 |
based page routine |
|
136 |
''' |
|
137 |
pass |
|
138 |
||
197 | 139 |
class BaseDocTemplate: |
140 |
""" |
|
141 |
First attempt at defining a document template class. |
|
142 |
||
143 |
The basic idea is simple. |
|
144 |
0) The document has a list of data associated with it |
|
145 |
this data should derive from flowables. We'll have |
|
146 |
special classes like PageBreak, FrameBreak to do things |
|
147 |
like forcing a page end etc. |
|
148 |
||
149 |
1) The document has one or more page templates. |
|
150 |
||
151 |
2) Each page template has one or more frames. |
|
152 |
||
153 |
3) The document class provides base methods for handling the |
|
154 |
story events and some reasonable methods for getting the |
|
155 |
story flowables into the frames. |
|
156 |
||
157 |
4) The document instances can override the base handler routines. |
|
158 |
""" |
|
253 | 159 |
_initArgs = { 'pageSize':DEFAULT_PAGE_SIZE, |
160 |
'pageTemplates':[], |
|
161 |
'showBoundary':0, |
|
162 |
'leftMargin':inch, |
|
163 |
'rightMargin':inch, |
|
164 |
'topMargin':inch, |
|
165 |
'bottomMargin':inch, |
|
166 |
'allowSplitting':1, |
|
167 |
'title':None, |
|
168 |
'author':None, |
|
169 |
'_pageBreakQuick':1} |
|
170 |
_invalidInitArgs = () |
|
197 | 171 |
|
253 | 172 |
def __init__(self, filename, **kw): |
197 | 173 |
self.filename = filename |
253 | 174 |
|
175 |
for k in self._initArgs.keys(): |
|
176 |
if not kw.has_key(k): |
|
177 |
v = self._initArgs[k] |
|
178 |
else: |
|
179 |
if k in self._invalidInitArgs: |
|
180 |
raise ValueError, "Invalid argument %s" % k |
|
181 |
v = kw[k] |
|
182 |
setattr(self,k,v) |
|
183 |
||
184 |
p = self.pageTemplates |
|
185 |
self.pageTemplates = [] |
|
186 |
self.addPageTemplates(p) |
|
187 |
self._calc() |
|
188 |
||
189 |
def _calc(self): |
|
190 |
self._rightMargin = self.pageSize[0] - self.rightMargin |
|
191 |
self._topMargin = self.pageSize[1] - self.topMargin |
|
192 |
self.width = self._rightMargin - self.leftMargin |
|
193 |
self.height = self._topMargin - self.bottomMargin |
|
197 | 194 |
|
195 |
def clean_hanging(self): |
|
249 | 196 |
'handle internal postponed actions' |
197 | 197 |
while len(self._hanging): |
198 |
self.handle_flowable(self._hanging) |
|
199 |
||
200 |
def addPageTemplates(self,pageTemplates): |
|
249 | 201 |
'add one or a sequence of pageTamplates' |
197 | 202 |
if type(pageTemplates) not in (ListType,TupleType): |
203 |
pageTemplates = [pageTemplates] |
|
204 |
assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error" |
|
205 |
for t in pageTemplates: |
|
206 |
self.pageTemplates.append(t) |
|
207 |
||
208 |
def handle_documentBegin(self): |
|
249 | 209 |
'''Hook actions at beginning of document''' |
197 | 210 |
self._hanging = [PageBegin] |
211 |
self.pageTemplate = self.pageTemplates[0] |
|
212 |
self.page = 0 |
|
213 |
||
214 |
def handle_pageBegin(self): |
|
249 | 215 |
'''Perform actions required at beginning of page. |
216 |
shouldn't normally be called directly''' |
|
197 | 217 |
self.page = self.page + 1 |
214 | 218 |
self.pageTemplate.drawPage(self.canv,self) |
197 | 219 |
self.pageTemplate.onPage(self.canv,self) |
220 |
if hasattr(self,'_nextFrameIndex'): |
|
221 |
del self._nextFrameIndex |
|
222 |
self.frame = self.pageTemplate.frames[0] |
|
223 |
self.handle_frameBegin() |
|
224 |
||
225 |
def handle_pageEnd(self): |
|
226 |
''' show the current page |
|
227 |
check the next page template |
|
228 |
hang a page begin |
|
229 |
''' |
|
230 |
self.canv.showPage() |
|
231 |
if hasattr(self,'_nextPageTemplateIndex'): |
|
232 |
self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex] |
|
233 |
del self._nextPageTemplateIndex |
|
234 |
self._hanging.append(PageBegin) |
|
235 |
||
236 |
def handle_pageBreak(self): |
|
237 |
'''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
|
238 |
if self._pageBreakQuick: |
197 | 239 |
self.handle_pageEnd() |
240 |
else: |
|
241 |
n = len(self._hanging) |
|
242 |
while len(self._hanging)==n: |
|
243 |
self.handle_frameEnd() |
|
244 |
||
245 |
def handle_frameBegin(self,*args): |
|
249 | 246 |
'''What to do at the beginning of a page''' |
197 | 247 |
self.frame._reset() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
248 |
if self.showBoundary or self.frame.showBoundary: |
226 | 249 |
self.frame.drawBoundary(self.canv) |
197 | 250 |
|
251 |
def handle_frameEnd(self): |
|
252 |
''' Handles the semantics of the end of a frame. This includes the selection of |
|
253 |
the next frame or if this is the last frame then invoke pageEnd. |
|
254 |
''' |
|
255 |
if hasattr(self,'_nextFrameIndex'): |
|
256 |
frame = self.pageTemplate.frames[self._nextFrameIndex] |
|
257 |
del self._nextFrameIndex |
|
204 | 258 |
self.handle_frameBegin() |
197 | 259 |
elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]: |
260 |
self.handle_pageEnd() |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
261 |
self.frame = None |
197 | 262 |
else: |
263 |
f = self.frame |
|
264 |
self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1] |
|
265 |
self.handle_frameBegin() |
|
266 |
||
267 |
def handle_nextPageTemplate(self,pt): |
|
268 |
'''On endPage chenge to the page template with name or index pt''' |
|
269 |
if type(pt) is StringType: |
|
270 |
for t in self.pageTemplates: |
|
271 |
if t.id == pt: |
|
272 |
self._nextPageTemplateIndex = self.pageTemplates.index(t) |
|
273 |
return |
|
274 |
raise ValueError, "can't find template('%s')"%pt |
|
275 |
elif type(pt) is IntType: |
|
276 |
self._nextPageTemplateIndex = pt |
|
277 |
else: |
|
278 |
raise TypeError, "argument pt should be string or integer" |
|
279 |
||
280 |
def handle_nextFrame(self,fx): |
|
281 |
'''On endFrame chenge to the frame with name or index fx''' |
|
282 |
if type(fx) is StringType: |
|
283 |
for f in self.pageTemplate.frames: |
|
284 |
if f.id == fx: |
|
285 |
self._nextFrameIndex = self.pageTemplate.frames.index(f) |
|
286 |
return |
|
287 |
raise ValueError, "can't find frame('%s')"%fx |
|
288 |
elif type(fx) is IntType: |
|
289 |
self._nextFrameIndex = fx |
|
290 |
else: |
|
291 |
raise TypeError, "argument fx should be string or integer" |
|
292 |
||
293 |
def handle_currentFrame(self,fx): |
|
294 |
'''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_flowable(self,flowables): |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
307 |
'''try to handle one flowable from the front of list flowables.''' |
197 | 308 |
f = flowables[0] |
309 |
del flowables[0] |
|
310 |
||
311 |
if isinstance(f,PageBreak): |
|
312 |
self.handle_pageBreak() |
|
313 |
elif isinstance(f,ActionFlowable): |
|
314 |
f.apply(self) |
|
315 |
else: |
|
316 |
#general case we have to do something |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
317 |
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
|
318 |
if self.allowSplitting: |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
319 |
# see if this is a splittable thing |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
320 |
S = self.frame.split(f) |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
321 |
n = len(S) |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
322 |
else: |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
323 |
n = 0 |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
324 |
|
197 | 325 |
if n: |
200 | 326 |
if not self.frame.add(S[0], self.canv, trySplit=0): |
327 |
raise "LayoutError", "splitting error" |
|
328 |
del S[0] |
|
329 |
for f in xrange(n-1): |
|
197 | 330 |
flowables.insert(f,S[f]) # put split flowables back on the list |
331 |
else: |
|
249 | 332 |
if hasattr(f,'postponed'): |
333 |
raise "LayoutError", "Flowable too large" |
|
334 |
f.postponed = 1 |
|
197 | 335 |
flowables.insert(0,f) # put the flowable back |
336 |
self.handle_frameEnd() |
|
337 |
||
204 | 338 |
#these are provided so that deriving classes can refer to them |
197 | 339 |
_handle_documentBegin = handle_documentBegin |
340 |
_handle_pageBegin = handle_pageBegin |
|
341 |
_handle_pageEnd = handle_pageEnd |
|
342 |
_handle_frameBegin = handle_frameBegin |
|
343 |
_handle_frameEnd = handle_frameEnd |
|
344 |
_handle_flowable = handle_flowable |
|
345 |
_handle_nextPageTemplate = handle_nextPageTemplate |
|
346 |
_handle_currentFrame = handle_currentFrame |
|
347 |
_handle_nextFrame = handle_nextFrame |
|
348 |
||
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
349 |
def _startBuild(self): |
253 | 350 |
self._calc() |
197 | 351 |
self.canv = canvas.Canvas(self.filename) |
352 |
self.handle_documentBegin() |
|
353 |
||
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
354 |
def _endBuild(self): |
197 | 355 |
if self._hanging!=[] and self._hanging[-1] is PageBegin: |
356 |
del self._hanging[-1] |
|
357 |
self.clean_hanging() |
|
358 |
else: |
|
359 |
self.clean_hanging() |
|
360 |
self.handle_pageBreak() |
|
361 |
||
362 |
self.canv.save() |
|
363 |
del self.frame, self.pageTemplate |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
364 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
365 |
def build(self, flowables): |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
366 |
assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error" |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
367 |
self._startBuild() |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
368 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
369 |
while len(flowables): |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
370 |
self.clean_hanging() |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
371 |
self.handle_flowable(flowables) |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
372 |
|
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
373 |
self._endBuild() |
221 | 374 |
|
375 |
class SimpleDocTemplate(BaseDocTemplate): |
|
376 |
def handle_pageBegin(self): |
|
377 |
self._handle_pageBegin() |
|
378 |
self._handle_nextPageTemplate('Later') |
|
379 |
||
380 |
def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing): |
|
227 | 381 |
frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') |
221 | 382 |
self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage), |
383 |
PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)]) |
|
225 | 384 |
if onFirstPage is _doNothing and hasattr(self,'onFirstPage'): |
385 |
self.pageTemplates[0].drawPage = self.onFirstPage |
|
386 |
if onLaterPages is _doNothing and hasattr(self,'onLaterPages'): |
|
387 |
self.pageTemplates[1].drawPage = self.onLaterPages |
|
221 | 388 |
BaseDocTemplate.build(self,flowables) |
389 |
||
390 |
if __name__ == '__main__': |
|
391 |
########################################################## |
|
392 |
## |
|
393 |
## testing |
|
394 |
## |
|
395 |
########################################################## |
|
396 |
def randomText(): |
|
397 |
#this may or may not be appropriate in your company |
|
398 |
from random import randint, choice |
|
399 |
||
400 |
RANDOMWORDS = ['strategic','direction','proactive', |
|
401 |
'reengineering','forecast','resources', |
|
402 |
'forward-thinking','profit','growth','doubletalk', |
|
403 |
'venture capital','IPO'] |
|
404 |
||
405 |
sentences = 5 |
|
406 |
output = "" |
|
407 |
for sentenceno in range(randint(1,5)): |
|
408 |
output = output + 'Blah' |
|
409 |
for wordno in range(randint(10,25)): |
|
410 |
if randint(0,4)==0: |
|
411 |
word = choice(RANDOMWORDS) |
|
412 |
else: |
|
413 |
word = 'blah' |
|
414 |
output = output + ' ' +word |
|
415 |
output = output+'.' |
|
416 |
return output |
|
417 |
||
418 |
def myFirstPage(canvas, doc): |
|
419 |
canvas.saveState() |
|
420 |
canvas.setStrokeColor(red) |
|
421 |
canvas.setLineWidth(5) |
|
422 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
|
423 |
canvas.setFont('Times-Bold',24) |
|
424 |
canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS") |
|
425 |
canvas.setFont('Times-Roman',12) |
|
426 |
canvas.drawString(4 * inch, 0.75 * inch, "First Page") |
|
427 |
canvas.restoreState() |
|
428 |
||
429 |
def myLaterPages(canvas, doc): |
|
430 |
canvas.saveState() |
|
431 |
canvas.setStrokeColor(red) |
|
432 |
canvas.setLineWidth(5) |
|
433 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
|
434 |
canvas.setFont('Times-Roman',12) |
|
435 |
canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page) |
|
436 |
canvas.restoreState() |
|
437 |
||
438 |
def run(): |
|
439 |
objects_to_draw = [] |
|
440 |
from reportlab.lib.styles import ParagraphStyle |
|
253 | 441 |
#from paragraph import Paragraph |
225 | 442 |
from doctemplate import SimpleDocTemplate |
221 | 443 |
|
444 |
#need a style |
|
445 |
normal = ParagraphStyle('normal') |
|
446 |
normal.firstLineIndent = 18 |
|
447 |
normal.spaceBefore = 6 |
|
448 |
import random |
|
449 |
for i in range(15): |
|
450 |
height = 0.5 + (2*random.random()) |
|
451 |
box = XBox(6 * inch, height * inch, 'Box Number %d' % i) |
|
452 |
objects_to_draw.append(box) |
|
453 |
para = Paragraph(randomText(), normal) |
|
454 |
objects_to_draw.append(para) |
|
455 |
||
253 | 456 |
SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw, |
221 | 457 |
onFirstPage=myFirstPage,onLaterPages=myLaterPages) |
458 |
||
459 |
run() |