1
#Copyright ReportLab Europe Ltd. 2000-2004
2
#see license.txt for license details
3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/examples.py
7
def annotations(canvas):
8
from reportlab.lib.units import inch
9
canvas.drawString(inch, 2.5*inch,
10
"setAuthor, setTitle, setSubject have no visible effect")
11
canvas.drawString(inch, inch, "But if you are viewing this document dynamically")
12
canvas.drawString(inch, 0.5*inch, "please look at File/Document Info")
13
canvas.setAuthor("the ReportLab Team")
14
canvas.setTitle("ReportLab PDF Generation User Guide")
15
canvas.setSubject("How to Generate PDF files using the ReportLab modules")
18
# magic function making module
22
print "it worked", a, b
34
from reportlab.lib.units import inch
35
# move the origin up and to the left
36
c.translate(inch,inch)
38
c.setFont("Helvetica", 14)
40
c.setStrokeColorRGB(0.2,0.5,0.3)
41
c.setFillColorRGB(1,0,1)
43
c.line(0,0,0,1.7*inch)
46
c.rect(0.2*inch,0.2*inch,1*inch,1.5*inch, fill=1)
47
# make text go straight up
50
c.setFillColorRGB(0,0,0.77)
51
# say hello (note after rotate the y coord needs to be negative!)
52
c.drawString(0.3*inch, -inch, "Hello World")
57
from reportlab.lib.units import inch
58
from reportlab.lib.colors import pink, black, red, blue, green
60
c.setStrokeColor(pink)
61
c.grid([inch, 2*inch, 3*inch, 4*inch], [0.5*inch, inch, 1.5*inch, 2*inch, 2.5*inch])
62
c.setStrokeColor(black)
63
c.setFont("Times-Roman", 20)
64
c.drawString(0,0, "(0,0) the Origin")
65
c.drawString(2.5*inch, inch, "(2.5,1) in inches")
66
c.drawString(4*inch, 2.5*inch, "(4, 2.5)")
68
c.rect(0,2*inch,0.2*inch,0.3*inch, fill=1)
70
c.circle(4.5*inch, 0.4*inch, 0.2*inch, fill=1)
74
def translate(canvas):
75
from reportlab.lib.units import cm
76
canvas.translate(2.3*cm, 0.3*cm)
82
canvas.scale(0.75, 0.5)
86
testscaletranslate = """
87
def scaletranslate(canvas):
88
from reportlab.lib.units import inch
89
canvas.setFont("Courier-BoldOblique", 12)
92
# scale then translate
93
canvas.scale(0.3, 0.5)
94
canvas.translate(2.4*inch, 1.5*inch)
95
canvas.drawString(0, 2.7*inch, "Scale then translate")
97
# forget the scale and translate...
99
# translate then scale
100
canvas.translate(2.4*inch, 1.5*inch)
101
canvas.scale(0.3, 0.5)
102
canvas.drawString(0, 2.7*inch, "Translate then scale")
108
from reportlab.lib.units import inch
109
canvas.translate(5.5*inch, 0)
110
canvas.scale(-1.0, 1.0)
116
from reportlab.lib import colors
117
from reportlab.lib.units import inch
119
y = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2
120
rdy=h/5.0; texty=h+2*rdy
121
canvas.setFont("Helvetica",10)
122
for [namedcolor, name] in (
123
[colors.lavenderblush, "lavenderblush"],
124
[colors.lawngreen, "lawngreen"],
125
[colors.lemonchiffon, "lemonchiffon"],
126
[colors.lightblue, "lightblue"],
127
[colors.lightcoral, "lightcoral"]):
128
canvas.setFillColor(namedcolor)
129
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
130
canvas.setFillColor(black)
131
canvas.drawCentredString(x+dx/2, y+texty, name)
134
for rgb in [(1,0,0), (0,1,0), (0,0,1), (0.5,0.3,0.1), (0.4,0.5,0.3)]:
136
canvas.setFillColorRGB(r,g,b)
137
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
138
canvas.setFillColor(black)
139
canvas.drawCentredString(x+dx/2, y+texty, "r%s g%s b%s"%rgb)
142
for cmyk in [(1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1), (0,0,0,0)]:
144
canvas.setFillColorCMYK(c,m,y1,k)
145
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
146
canvas.setFillColor(black)
147
canvas.drawCentredString(x+dx/2, y+texty, "c%s m%s y%s k%s"%cmyk)
150
for gray in (0.0, 0.25, 0.50, 0.75, 1.0):
151
canvas.setFillGray(gray)
152
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
153
canvas.setFillColor(black)
154
canvas.drawCentredString(x+dx/2, y+texty, "gray: %s"%gray)
160
from reportlab.lib.units import inch
161
from reportlab.lib.colors import pink, green, brown, white
164
for color in (pink, green, brown):
165
canvas.setFillColor(color)
166
canvas.rect(x,0,dx,3*inch,stroke=0,fill=1)
168
canvas.setFillColor(white)
169
canvas.setStrokeColor(white)
170
canvas.setFont("Helvetica-Bold", 85)
171
canvas.drawCentredString(2.75*inch, 1.3*inch, "SPUMONI")
175
def spumoni2(canvas):
176
from reportlab.lib.units import inch
177
from reportlab.lib.colors import pink, green, brown, white, black
178
# draw the previous drawing
180
# now put an ice cream cone on top of it:
181
# first draw a triangle (ice cream cone)
182
p = canvas.beginPath()
185
p.moveTo(xcenter-radius, 1.5*inch)
186
p.lineTo(xcenter+radius, 1.5*inch)
188
canvas.setFillColor(brown)
189
canvas.setStrokeColor(black)
190
canvas.drawPath(p, fill=1)
191
# draw some circles (scoops)
193
for color in (pink, green, brown):
194
canvas.setFillColor(color)
195
canvas.circle(xcenter, y, radius, fill=1)
201
from reportlab.lib.colors import yellow, green, red, black
202
from reportlab.lib.units import inch
205
# define the bezier curve control points
206
x1,y1, x2,y2, x3,y3, x4,y4 = d,1.5*i, 1.5*i,d, 3*i,d, 5.5*i-d,3*i-d
207
# draw a figure enclosing the control points
208
canvas.setFillColor(yellow)
209
p = canvas.beginPath()
211
for (x,y) in [(x2,y2), (x3,y3), (x4,y4)]:
213
canvas.drawPath(p, fill=1, stroke=0)
214
# draw the tangent lines
215
canvas.setLineWidth(inch*0.1)
216
canvas.setStrokeColor(green)
217
canvas.line(x1,y1,x2,y2)
218
canvas.setStrokeColor(red)
219
canvas.line(x3,y3,x4,y4)
220
# finally draw the curve
221
canvas.setStrokeColor(black)
222
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4)
227
from reportlab.lib.colors import yellow, green, red, black
228
from reportlab.lib.units import inch
229
# make a sequence of control points
230
xd,yd = 5.5*inch/2, 3*inch/2
232
dxdy = [(0,0.33), (0.33,0.33), (0.75,1), (0.875,0.875),
233
(0.875,0.875), (1,0.75), (0.33,0.33), (0.33,0)]
235
for xoffset in (1,-1):
238
px = xc + xd*xoffset*dx
239
py = yc + yd*yoffset*dy
240
pointlist.append((px,py))
243
px = xc + xd*xoffset*dx
244
py = yc + yd*yoffset*dy
245
pointlist.append((px,py))
246
# draw tangent lines and curves
247
canvas.setLineWidth(inch*0.1)
249
[(x1,y1),(x2,y2),(x3,y3),(x4,y4)] = pointlist[:4]
251
canvas.setLineWidth(inch*0.1)
252
canvas.setStrokeColor(green)
253
canvas.line(x1,y1,x2,y2)
254
canvas.setStrokeColor(red)
255
canvas.line(x3,y3,x4,y4)
256
# finally draw the curve
257
canvas.setStrokeColor(black)
258
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4)
262
def pencil(canvas, text="No.2"):
263
from reportlab.lib.colors import yellow, red, black,white
264
from reportlab.lib.units import inch
266
canvas.setStrokeColor(black)
267
canvas.setLineWidth(4)
269
canvas.setFillColor(red)
270
canvas.circle(30*u, 5*u, 5*u, stroke=1, fill=1)
271
# draw all else but the tip (mainly rectangles with different fills)
272
canvas.setFillColor(yellow)
273
canvas.rect(10*u,0,20*u,10*u, stroke=1, fill=1)
274
canvas.setFillColor(black)
275
canvas.rect(23*u,0,8*u,10*u,fill=1)
276
canvas.roundRect(14*u, 3.5*u, 8*u, 3*u, 1.5*u, stroke=1, fill=1)
277
canvas.setFillColor(white)
278
canvas.rect(25*u,u,1.2*u,8*u, fill=1,stroke=0)
279
canvas.rect(27.5*u,u,1.2*u,8*u, fill=1, stroke=0)
280
canvas.setFont("Times-Roman", 3*u)
281
canvas.drawCentredString(18*u, 4*u, text)
283
penciltip(canvas,debug=0)
284
# draw broken lines across the body.
285
canvas.setDash([10,5,16,10],0)
286
canvas.line(11*u,2.5*u,22*u,2.5*u)
287
canvas.line(22*u,7.5*u,12*u,7.5*u)
291
def penciltip(canvas, debug=1):
292
from reportlab.lib.colors import tan, black, green
293
from reportlab.lib.units import inch
295
canvas.setLineWidth(4)
297
canvas.scale(2.8,2.8) # make it big
298
canvas.setLineWidth(1) # small lines
299
canvas.setStrokeColor(black)
300
canvas.setFillColor(tan)
301
p = canvas.beginPath()
305
p.curveTo(11.5*u,10*u, 11.5*u,7.5*u, 10*u,7.5*u)
306
p.curveTo(12*u,7.5*u, 11*u,2.5*u, 9.7*u,2.5*u)
307
p.curveTo(10.5*u,2.5*u, 11*u,0, 10*u,0)
308
canvas.drawPath(p, stroke=1, fill=1)
309
canvas.setFillColor(black)
310
p = canvas.beginPath()
315
canvas.drawPath(p, stroke=1, fill=1)
317
canvas.setStrokeColor(green) # put in a frame of reference
318
canvas.grid([0,5*u,10*u,15*u], [0,5*u,10*u])
321
testnoteannotation = """
322
from reportlab.platypus.flowables import Flowable
323
class NoteAnnotation(Flowable):
324
'''put a pencil in the margin.'''
325
def wrap(self, *args):
326
return (1,10) # I take up very little space! (?)
329
canvas.translate(-10,-10)
331
canvas.scale(0.2,0.2)
332
pencil(canvas, text="NOTE")
335
testhandannotation = """
336
from reportlab.platypus.flowables import Flowable
337
from reportlab.lib.colors import tan, green
338
class HandAnnotation(Flowable):
339
'''A hand flowable.'''
340
def __init__(self, xoffset=0, size=None, fillcolor=tan, strokecolor=green):
341
from reportlab.lib.units import inch
342
if size is None: size=4*inch
343
self.fillcolor, self.strokecolor = fillcolor, strokecolor
344
self.xoffset = xoffset
346
# normal size is 4 inches
347
self.scale = size/(4.0*inch)
348
def wrap(self, *args):
349
return (self.xoffset, self.size)
352
canvas.setLineWidth(6)
353
canvas.setFillColor(self.fillcolor)
354
canvas.setStrokeColor(self.strokecolor)
355
canvas.translate(self.xoffset+self.size,0)
357
canvas.scale(self.scale, self.scale)
358
hand(canvas, debug=0, fill=1)
362
well she hit Net Solutions
363
and she registered her own .com site now
364
and filled it up with yahoo profile pics
365
she snarfed in one night now
366
and she made 50 million when Hugh Hefner
367
bought up the rights now
368
and she'll have fun fun fun
369
til her Daddy takes the keyboard away'''
371
lyrics = string.split(lyrics, "\n")
373
def textsize(canvas):
374
from reportlab.lib.units import inch
375
from reportlab.lib.colors import magenta, red
376
canvas.setFont("Times-Roman", 20)
377
canvas.setFillColor(red)
378
canvas.drawCentredString(2.75*inch, 2.5*inch, "Font size examples")
379
canvas.setFillColor(magenta)
384
canvas.setFont("Helvetica", size)
385
canvas.drawRightString(x,y,"%s points: " % size)
386
canvas.drawString(x,y, line)
392
def star(canvas, title="Title Here", aka="Comment here.",
393
xcenter=None, ycenter=None, nvertices=5):
395
from reportlab.lib.units import inch
397
if xcenter is None: xcenter=2.75*inch
398
if ycenter is None: ycenter=1.5*inch
399
canvas.drawCentredString(xcenter, ycenter+1.3*radius, title)
400
canvas.drawCentredString(xcenter, ycenter-1.4*radius, aka)
401
p = canvas.beginPath()
402
p.moveTo(xcenter,ycenter+radius)
403
from math import pi, cos, sin
406
for vertex in range(nvertices-1):
407
nextangle = angle*(vertex+1)+startangle
408
x = xcenter + radius*cos(nextangle)
409
y = ycenter + radius*sin(nextangle)
418
from reportlab.lib.units import inch
420
canvas.setLineWidth(5)
421
star(canvas, "Default: mitered join", "0: pointed", xcenter = 1*inch)
422
canvas.setLineJoin(1)
423
star(canvas, "Round join", "1: rounded")
424
canvas.setLineJoin(2)
425
star(canvas, "Bevelled join", "2: square", xcenter=4.5*inch)
430
from reportlab.lib.units import inch
432
canvas.setLineWidth(5)
433
star(canvas, "Default", "no projection",xcenter = 1*inch,
436
star(canvas, "Round cap", "1: ends in half circle", nvertices=4)
438
star(canvas, "Square cap", "2: projects out half a width", xcenter=4.5*inch,
444
from reportlab.lib.units import inch
447
star(canvas, "Simple dashes", "6 points on, 3 off", xcenter = 1*inch)
449
star(canvas, "Dots", "One on, two off")
450
canvas.setDash([1,1,3,3,1,4,4,1], 0)
451
star(canvas, "Complex Pattern", "[1,1,3,3,1,4,4,1]", xcenter=4.5*inch)
454
testcustomfont1 = """
455
def customfont1(canvas):
456
# we know some glyphs are missing, suppress warnings
457
import reportlab.rl_config
458
reportlab.rl_config.warnOnMissingFontGlyphs = 0
461
from reportlab.pdfbase import pdfmetrics
462
afmFile, pfbFile = rl_doc_utils.getJustFontPaths()
463
justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
464
faceName = 'Wargames' # pulled from AFM file
465
pdfmetrics.registerTypeFace(justFace)
466
justFont = pdfmetrics.Font('Wargames',
469
pdfmetrics.registerFont(justFont)
471
canvas.setFont('Wargames', 32)
472
canvas.drawString(10, 150, 'This should be in')
473
canvas.drawString(10, 100, 'Wargames')
477
def ttffont1(canvas):
478
# we know some glyphs are missing, suppress warnings
479
import reportlab.rl_config
480
reportlab.rl_config.warnOnMissingFontGlyphs = 0
481
from reportlab.pdfbase import pdfmetrics
482
from reportlab.pdfbase.ttfonts import TTFont
483
pdfmetrics.registerFont(TTFont('PenguinAttack', 'PenguinAttack.ttf'))
484
from reportlab.pdfgen.canvas import Canvas
486
canvas.setFont('PenguinAttack', 24)
487
canvas.drawString(10, 150, "Some UTF-8 text encoded")
488
canvas.drawString(10, 100, "in the PenguinAttack TT Font!")
491
testcursormoves1 = """
492
def cursormoves1(canvas):
493
from reportlab.lib.units import inch
494
textobject = canvas.beginText()
495
textobject.setTextOrigin(inch, 2.5*inch)
496
textobject.setFont("Helvetica-Oblique", 14)
498
textobject.textLine(line)
499
textobject.setFillGray(0.4)
500
textobject.textLines('''
501
With many apologies to the Beach Boys
502
and anyone else who finds this objectionable
504
canvas.drawText(textobject)
507
testcursormoves2 = """
508
def cursormoves2(canvas):
509
from reportlab.lib.units import inch
510
textobject = canvas.beginText()
511
textobject.setTextOrigin(2, 2.5*inch)
512
textobject.setFont("Helvetica-Oblique", 14)
514
textobject.textOut(line)
515
textobject.moveCursor(14,14) # POSITIVE Y moves down!!!
516
textobject.setFillColorRGB(0.4,0,1)
517
textobject.textLines('''
518
With many apologies to the Beach Boys
519
and anyone else who finds this objectionable
521
canvas.drawText(textobject)
525
def charspace(canvas):
526
from reportlab.lib.units import inch
527
textobject = canvas.beginText()
528
textobject.setTextOrigin(3, 2.5*inch)
529
textobject.setFont("Helvetica-Oblique", 10)
532
textobject.setCharSpace(charspace)
533
textobject.textLine("%s: %s" %(charspace,line))
534
charspace = charspace+0.5
535
textobject.setFillGray(0.4)
536
textobject.textLines('''
537
With many apologies to the Beach Boys
538
and anyone else who finds this objectionable
540
canvas.drawText(textobject)
544
def wordspace(canvas):
545
from reportlab.lib.units import inch
546
textobject = canvas.beginText()
547
textobject.setTextOrigin(3, 2.5*inch)
548
textobject.setFont("Helvetica-Oblique", 12)
551
textobject.setWordSpace(wordspace)
552
textobject.textLine("%s: %s" %(wordspace,line))
553
wordspace = wordspace+2.5
554
textobject.setFillColorCMYK(0.4,0,0.4,0.2)
555
textobject.textLines('''
556
With many apologies to the Beach Boys
557
and anyone else who finds this objectionable
559
canvas.drawText(textobject)
561
testhorizontalscale = """
562
def horizontalscale(canvas):
563
from reportlab.lib.units import inch
564
textobject = canvas.beginText()
565
textobject.setTextOrigin(3, 2.5*inch)
566
textobject.setFont("Helvetica-Oblique", 12)
567
horizontalscale = 80 # 100 is default
569
textobject.setHorizScale(horizontalscale)
570
textobject.textLine("%s: %s" %(horizontalscale,line))
571
horizontalscale = horizontalscale+10
572
textobject.setFillColorCMYK(0.0,0.4,0.4,0.2)
573
textobject.textLines('''
574
With many apologies to the Beach Boys
575
and anyone else who finds this objectionable
577
canvas.drawText(textobject)
581
from reportlab.lib.units import inch
582
textobject = canvas.beginText()
583
textobject.setTextOrigin(3, 2.5*inch)
584
textobject.setFont("Helvetica-Oblique", 14)
587
textobject.setLeading(leading)
588
textobject.textLine("%s: %s" %(leading,line))
589
leading = leading+2.5
590
textobject.setFillColorCMYK(0.8,0,0,0.3)
591
textobject.textLines('''
592
With many apologies to the Beach Boys
593
and anyone else who finds this objectionable
595
canvas.drawText(textobject)
599
def hand(canvas, debug=1, fill=0):
600
(startx, starty) = (0,0)
602
( 0, 2), ( 0, 4), ( 0, 8), # back of hand
603
( 5, 8), ( 7,10), ( 7,14),
604
(10,14), (10,13), ( 7.5, 8), # thumb
605
(13, 8), (14, 8), (17, 8),
606
(19, 8), (19, 6), (17, 6),
607
(15, 6), (13, 6), (11, 6), # index, pointing
608
(12, 6), (13, 6), (14, 6),
609
(16, 6), (16, 4), (14, 4),
610
(13, 4), (12, 4), (11, 4), # middle
611
(11.5, 4), (12, 4), (13, 4),
612
(15, 4), (15, 2), (13, 2),
613
(12.5, 2), (11.5, 2), (11, 2), # ring
614
(11.5, 2), (12, 2), (12.5, 2),
615
(14, 2), (14, 0), (12.5, 0),
616
(10, 0), (8, 0), (6, 0), # pinky, then close
618
from reportlab.lib.units import inch
619
if debug: canvas.setLineWidth(6)
621
p = canvas.beginPath()
622
p.moveTo(startx, starty)
625
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3]
627
p.curveTo(x1*u,y1*u,x2*u,y2*u,x3*u,y3*u)
629
canvas.drawPath(p, fill=fill)
631
from reportlab.lib.colors import red, green
632
(lastx, lasty) = (startx, starty)
635
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3]
637
canvas.setStrokeColor(red)
638
canvas.line(lastx*u,lasty*u, x1*u,y1*u)
639
canvas.setStrokeColor(green)
640
canvas.line(x2*u,y2*u, x3*u,y3*u)
641
(lastx,lasty) = (x3,y3)
646
canvas.translate(20,10)
647
canvas.setLineWidth(3)
648
canvas.setFillColorRGB(0.1, 0.3, 0.9)
649
canvas.setStrokeGray(0.5)
650
hand(canvas, debug=0, fill=1)
655
from reportlab.lib.units import inch
656
text = "Now is the time for all good men to..."
659
for font in canvas.getAvailableFonts():
660
canvas.setFont(font, 10)
661
canvas.drawString(x,y,text)
662
canvas.setFont("Helvetica", 10)
663
canvas.drawRightString(x-10,y, font+":")
669
from reportlab.lib.units import inch
670
canvas.setLineWidth(4)
671
canvas.setStrokeColorRGB(0.8, 1, 0.6)
672
# draw rectangles enclosing the arcs
673
canvas.rect(inch, inch, 1.5*inch, inch)
674
canvas.rect(3*inch, inch, inch, 1.5*inch)
675
canvas.setStrokeColorRGB(0, 0.2, 0.4)
676
canvas.setFillColorRGB(1, 0.6, 0.8)
677
p = canvas.beginPath()
678
p.moveTo(0.2*inch, 0.2*inch)
679
p.arcTo(inch, inch, 2.5*inch,2*inch, startAng=-30, extent=135)
680
p.arc(3*inch, inch, 4*inch, 2.5*inch, startAng=-45, extent=270)
681
canvas.drawPath(p, fill=1, stroke=1)
683
testvariousshapes = """
684
def variousshapes(canvas):
685
from reportlab.lib.units import inch
687
canvas.setStrokeGray(0.5)
688
canvas.grid(range(0,11*inch/2,inch/2), range(0,7*inch/2,inch/2))
689
canvas.setLineWidth(4)
690
canvas.setStrokeColorRGB(0, 0.2, 0.7)
691
canvas.setFillColorRGB(1, 0.6, 0.8)
692
p = canvas.beginPath()
693
p.rect(0.5*inch, 0.5*inch, 0.5*inch, 2*inch)
694
p.circle(2.75*inch, 1.5*inch, 0.3*inch)
695
p.ellipse(3.5*inch, 0.5*inch, 1.2*inch, 2*inch)
696
canvas.drawPath(p, fill=1, stroke=1)
699
testclosingfigures = """
700
def closingfigures(canvas):
701
from reportlab.lib.units import inch
702
h = inch/3.0; k = inch/2.0
703
canvas.setStrokeColorRGB(0.2,0.3,0.5)
704
canvas.setFillColorRGB(0.8,0.6,0.2)
705
canvas.setLineWidth(4)
706
p = canvas.beginPath()
709
xc,yc = inch*i, inch*j
711
p.arcTo(xc-h, yc-k, xc+h, yc+k, startAng=0, extent=60*i)
712
# close only the first one, not the second one
715
canvas.drawPath(p, fill=1, stroke=1)
720
#first create a form...
721
canvas.beginForm("SpumoniForm")
722
#re-use some drawing functions from earlier
727
canvas.doForm("SpumoniForm")
730
def doctemplateillustration(canvas):
731
from reportlab.lib.units import inch
732
canvas.setFont("Helvetica", 10)
733
canvas.drawString(inch/4.0, 2.75*inch, "DocTemplate")
738
for name in ("two column", "chapter page", "title page"):
739
canvas.setFillColorRGB(0.5,1.0,1.0)
740
canvas.rect(x,y,W,H, fill=1)
741
canvas.setFillColorRGB(0,0,0)
742
canvas.drawString(x+inch/8, y+H-Wd, "PageTemplate")
743
canvas.drawCentredString(x+W/2.0, y-Wd, name)
749
canvas.translate(Wd+d, Hd+d)
750
for name in ("left Frame", "right Frame"):
751
canvas.setFillColorRGB(1.0,0.5,1.0)
752
canvas.rect(0,0, dW,hD, fill=1)
753
canvas.setFillGray(0.7)
760
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=0)
762
canvas.setFillColorRGB(0,0,0)
765
canvas.drawString(d,-dW/2, name)
766
canvas.restoreState()
767
canvas.translate(dW+d,0)
768
canvas.restoreState()
769
canvas.setFillColorRGB(1.0, 0.5, 1.0)
774
canvas.rect(mx, my, mW, mH, fill=1)
775
canvas.rect(Wd+2*(W+Wd)+d, Hd+3*d, W-2*d, H/2.0, fill=1)
776
canvas.setFillGray(0.7)
777
canvas.rect(Wd+2*(W+Wd)+d+dd, Hd+5*d, W-2*d-2*dd, H/2.0-2*d-dd, fill=1)
780
ddH = (mH-6*dd-mH/5.0)/3.0
783
canvas.setFillGray(0.7)
784
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=1)
785
canvas.setFillGray(0)
786
canvas.drawString(xx+dd/2.0,yy+dd/2.0, "flowable %s" %(157-i))
788
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H/2.0, "First Flowable")
789
canvas.setFont("Times-BoldItalic", 8)
790
canvas.setFillGray(0)
791
canvas.drawCentredString(mx+mW/2.0, my+mH+3*dd, "Chapter 6: Lubricants")
792
canvas.setFont("Times-BoldItalic", 10)
793
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H-H/4, "College Life")
798
from types import StringType
799
from string import strip
800
for (a,b) in g.items():
801
if a[:4]=="test" and type(b) is StringType:
808
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
809
from reportlab.lib.styles import getSampleStyleSheet
810
from reportlab.rl_config import defaultPageSize
811
from reportlab.lib.units import inch
812
PAGE_HEIGHT=defaultPageSize[1]; PAGE_WIDTH=defaultPageSize[0]
813
styles = getSampleStyleSheet()
815
platypusfirstpage = """
816
Title = "Hello world"
817
pageinfo = "platypus example"
818
def myFirstPage(canvas, doc):
820
canvas.setFont('Times-Bold',16)
821
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
822
canvas.setFont('Times-Roman',9)
823
canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
824
canvas.restoreState()
826
platypusnextpage = """
827
def myLaterPages(canvas, doc):
829
canvas.setFont('Times-Roman',9)
830
canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
831
canvas.restoreState()
835
doc = SimpleDocTemplate("phello.pdf")
836
Story = [Spacer(1,2*inch)]
837
style = styles["Normal"]
839
bogustext = ("This is Paragraph number %s. " % i) *20
840
p = Paragraph(bogustext, style)
842
Story.append(Spacer(1,0.2*inch))
843
doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
846
if __name__=="__main__":
847
# then do the platypus hello world
848
for b in platypussetup, platypusfirstpage, platypusnextpage, platypusgo: