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