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