reportlab/platypus/doctemplate.py
changeset 221 3d71b66b14c6
parent 218 274db2129c04
child 225 1f58b00cf30e
--- a/reportlab/platypus/doctemplate.py	Tue May 16 15:58:27 2000 +0000
+++ b/reportlab/platypus/doctemplate.py	Tue May 16 16:15:16 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: doctemplate.py,v $
+#	Revision 1.8  2000/05/16 16:15:16  rgbecker
+#	Changes related to removal of SimpleFlowDocument
+#
 #	Revision 1.7  2000/05/16 14:28:55  rgbecker
 #	Fixes/Changes to get testplatypus to work with new framework
-#
+#	
 #	Revision 1.6  2000/05/15 15:07:32  rgbecker
 #	Added drawPage
 #	
@@ -52,7 +55,7 @@
 #	Revision 1.1  2000/05/12 12:53:33  rgbecker
 #	Initial try at a document template class
 #	
-__version__=''' $Id: doctemplate.py,v 1.7 2000/05/16 14:28:55 rgbecker Exp $ '''
+__version__=''' $Id: doctemplate.py,v 1.8 2000/05/16 16:15:16 rgbecker Exp $ '''
 __doc__="""
 More complicated Document model
 """
@@ -327,3 +330,84 @@
 			self.handle_flowable(flowables)
 
 		self._endBuild()
+
+class SimpleDocTemplate(BaseDocTemplate):
+	def handle_pageBegin(self):
+		self._handle_pageBegin()
+		self._handle_nextPageTemplate('Later')
+
+	def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
+		frameT = BasicFrame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal')
+		self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage),
+						PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)])
+		BaseDocTemplate.build(self,flowables)
+
+if __name__ == '__main__':
+	##########################################################
+	##
+	##	 testing
+	##
+	##########################################################
+	def randomText():
+		#this may or may not be appropriate in your company
+		from random import randint, choice
+
+		RANDOMWORDS = ['strategic','direction','proactive',
+		'reengineering','forecast','resources',
+		'forward-thinking','profit','growth','doubletalk',
+		'venture capital','IPO']
+
+		sentences = 5
+		output = ""
+		for sentenceno in range(randint(1,5)):
+			output = output + 'Blah'
+			for wordno in range(randint(10,25)):
+				if randint(0,4)==0:
+					word = choice(RANDOMWORDS)
+				else:
+					word = 'blah'
+				output = output + ' ' +word
+			output = output+'.'
+		return output
+
+	def myFirstPage(canvas, doc):
+		canvas.saveState()
+		canvas.setStrokeColor(red)
+		canvas.setLineWidth(5)
+		canvas.line(66,72,66,PAGE_HEIGHT-72)
+		canvas.setFont('Times-Bold',24)
+		canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS")
+		canvas.setFont('Times-Roman',12)
+		canvas.drawString(4 * inch, 0.75 * inch, "First Page")
+		canvas.restoreState()
+
+	def myLaterPages(canvas, doc):
+		canvas.saveState()
+		canvas.setStrokeColor(red)
+		canvas.setLineWidth(5)
+		canvas.line(66,72,66,PAGE_HEIGHT-72)
+		canvas.setFont('Times-Roman',12)
+		canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
+		canvas.restoreState()
+
+	def run():
+		objects_to_draw = []
+		from reportlab.lib.styles import ParagraphStyle
+		from paragraph import Paragraph
+
+		#need a style
+		normal = ParagraphStyle('normal')
+		normal.firstLineIndent = 18
+		normal.spaceBefore = 6
+		import random
+		for i in range(15):
+			height = 0.5 + (2*random.random())
+			box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
+			objects_to_draw.append(box)
+			para = Paragraph(randomText(), normal)
+			objects_to_draw.append(para)
+
+		SimpleDocTemplate('doctemplate.pdf',DEFAULT_PAGE_SIZE).build(objects_to_draw,
+			onFirstPage=myFirstPage,onLaterPages=myLaterPages)
+
+	run()