2963
|
1 |
import string
|
|
2 |
|
|
3 |
testannotations="""
|
|
4 |
def annotations(canvas):
|
|
5 |
from reportlab.lib.units import inch
|
|
6 |
canvas.drawString(inch, 2.5*inch,
|
|
7 |
"setAuthor, setTitle, setSubject have no visible effect")
|
|
8 |
canvas.drawString(inch, inch, "But if you are viewing this document dynamically")
|
|
9 |
canvas.drawString(inch, 0.5*inch, "please look at File/Document Info")
|
|
10 |
canvas.setAuthor("the ReportLab Team")
|
|
11 |
canvas.setTitle("ReportLab PDF Generation User Guide")
|
|
12 |
canvas.setSubject("How to Generate PDF files using the ReportLab modules")
|
|
13 |
"""
|
|
14 |
|
|
15 |
# magic function making module
|
|
16 |
|
|
17 |
test1 = """
|
|
18 |
def f(a,b):
|
|
19 |
print "it worked", a, b
|
|
20 |
return a+b
|
|
21 |
"""
|
|
22 |
|
|
23 |
test2 = """
|
|
24 |
def g(n):
|
|
25 |
if n==0: return 1
|
|
26 |
else: return n*g(n-1)
|
|
27 |
"""
|
|
28 |
|
|
29 |
testhello = """
|
|
30 |
def hello(c):
|
|
31 |
from reportlab.lib.units import inch
|
|
32 |
# move the origin up and to the left
|
|
33 |
c.translate(inch,inch)
|
|
34 |
# define a large font
|
|
35 |
c.setFont("Helvetica", 14)
|
|
36 |
# choose some colors
|
|
37 |
c.setStrokeColorRGB(0.2,0.5,0.3)
|
|
38 |
c.setFillColorRGB(1,0,1)
|
|
39 |
# draw some lines
|
|
40 |
c.line(0,0,0,1.7*inch)
|
|
41 |
c.line(0,0,1*inch,0)
|
|
42 |
# draw a rectangle
|
|
43 |
c.rect(0.2*inch,0.2*inch,1*inch,1.5*inch, fill=1)
|
|
44 |
# make text go straight up
|
|
45 |
c.rotate(90)
|
|
46 |
# change color
|
|
47 |
c.setFillColorRGB(0,0,0.77)
|
|
48 |
# say hello (note after rotate the y coord needs to be negative!)
|
|
49 |
c.drawString(0.3*inch, -inch, "Hello World")
|
|
50 |
"""
|
|
51 |
|
|
52 |
testcoords = """
|
|
53 |
def coords(canvas):
|
|
54 |
from reportlab.lib.units import inch
|
|
55 |
from reportlab.lib.colors import pink, black, red, blue, green
|
|
56 |
c = canvas
|
|
57 |
c.setStrokeColor(pink)
|
|
58 |
c.grid([inch, 2*inch, 3*inch, 4*inch], [0.5*inch, inch, 1.5*inch, 2*inch, 2.5*inch])
|
|
59 |
c.setStrokeColor(black)
|
|
60 |
c.setFont("Times-Roman", 20)
|
|
61 |
c.drawString(0,0, "(0,0) the Origin")
|
|
62 |
c.drawString(2.5*inch, inch, "(2.5,1) in inches")
|
|
63 |
c.drawString(4*inch, 2.5*inch, "(4, 2.5)")
|
|
64 |
c.setFillColor(red)
|
|
65 |
c.rect(0,2*inch,0.2*inch,0.3*inch, fill=1)
|
|
66 |
c.setFillColor(green)
|
|
67 |
c.circle(4.5*inch, 0.4*inch, 0.2*inch, fill=1)
|
|
68 |
"""
|
|
69 |
|
|
70 |
testtranslate = """
|
|
71 |
def translate(canvas):
|
|
72 |
from reportlab.lib.units import cm
|
|
73 |
canvas.translate(2.3*cm, 0.3*cm)
|
|
74 |
coords(canvas)
|
|
75 |
"""
|
|
76 |
|
|
77 |
testscale = """
|
|
78 |
def scale(canvas):
|
|
79 |
canvas.scale(0.75, 0.5)
|
|
80 |
coords(canvas)
|
|
81 |
"""
|
|
82 |
|
|
83 |
testscaletranslate = """
|
|
84 |
def scaletranslate(canvas):
|
|
85 |
from reportlab.lib.units import inch
|
|
86 |
canvas.setFont("Courier-BoldOblique", 12)
|
|
87 |
# save the state
|
|
88 |
canvas.saveState()
|
|
89 |
# scale then translate
|
|
90 |
canvas.scale(0.3, 0.5)
|
|
91 |
canvas.translate(2.4*inch, 1.5*inch)
|
|
92 |
canvas.drawString(0, 2.7*inch, "Scale then translate")
|
|
93 |
coords(canvas)
|
|
94 |
# forget the scale and translate...
|
|
95 |
canvas.restoreState()
|
|
96 |
# translate then scale
|
|
97 |
canvas.translate(2.4*inch, 1.5*inch)
|
|
98 |
canvas.scale(0.3, 0.5)
|
|
99 |
canvas.drawString(0, 2.7*inch, "Translate then scale")
|
|
100 |
coords(canvas)
|
|
101 |
"""
|
|
102 |
|
|
103 |
testmirror = """
|
|
104 |
def mirror(canvas):
|
|
105 |
from reportlab.lib.units import inch
|
|
106 |
canvas.translate(5.5*inch, 0)
|
|
107 |
canvas.scale(-1.0, 1.0)
|
|
108 |
coords(canvas)
|
|
109 |
"""
|
|
110 |
|
|
111 |
testcolors = """
|
|
112 |
def colors(canvas):
|
|
113 |
from reportlab.lib import colors
|
|
114 |
from reportlab.lib.units import inch
|
|
115 |
black = colors.black
|
|
116 |
y = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2
|
|
117 |
rdy=h/5.0; texty=h+2*rdy
|
|
118 |
canvas.setFont("Helvetica",10)
|
|
119 |
for [namedcolor, name] in (
|
|
120 |
[colors.lavenderblush, "lavenderblush"],
|
|
121 |
[colors.lawngreen, "lawngreen"],
|
|
122 |
[colors.lemonchiffon, "lemonchiffon"],
|
|
123 |
[colors.lightblue, "lightblue"],
|
|
124 |
[colors.lightcoral, "lightcoral"]):
|
|
125 |
canvas.setFillColor(namedcolor)
|
|
126 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
|
|
127 |
canvas.setFillColor(black)
|
|
128 |
canvas.drawCentredString(x+dx/2, y+texty, name)
|
|
129 |
x = x+dx
|
|
130 |
y = y + dy; x = 0
|
|
131 |
for rgb in [(1,0,0), (0,1,0), (0,0,1), (0.5,0.3,0.1), (0.4,0.5,0.3)]:
|
|
132 |
r,g,b = rgb
|
|
133 |
canvas.setFillColorRGB(r,g,b)
|
|
134 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
|
|
135 |
canvas.setFillColor(black)
|
|
136 |
canvas.drawCentredString(x+dx/2, y+texty, "r%s g%s b%s"%rgb)
|
|
137 |
x = x+dx
|
|
138 |
y = y + dy; x = 0
|
|
139 |
for cmyk in [(1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1), (0,0,0,0)]:
|
|
140 |
c,m,y1,k = cmyk
|
|
141 |
canvas.setFillColorCMYK(c,m,y1,k)
|
|
142 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
|
|
143 |
canvas.setFillColor(black)
|
|
144 |
canvas.drawCentredString(x+dx/2, y+texty, "c%s m%s y%s k%s"%cmyk)
|
|
145 |
x = x+dx
|
|
146 |
y = y + dy; x = 0
|
|
147 |
for gray in (0.0, 0.25, 0.50, 0.75, 1.0):
|
|
148 |
canvas.setFillGray(gray)
|
|
149 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1)
|
|
150 |
canvas.setFillColor(black)
|
|
151 |
canvas.drawCentredString(x+dx/2, y+texty, "gray: %s"%gray)
|
|
152 |
x = x+dx
|
|
153 |
"""
|
|
154 |
|
|
155 |
testspumoni = """
|
|
156 |
def spumoni(canvas):
|
|
157 |
from reportlab.lib.units import inch
|
|
158 |
from reportlab.lib.colors import pink, green, brown, white
|
|
159 |
x = 0; dx = 0.4*inch
|
|
160 |
for i in range(4):
|
|
161 |
for color in (pink, green, brown):
|
|
162 |
canvas.setFillColor(color)
|
|
163 |
canvas.rect(x,0,dx,3*inch,stroke=0,fill=1)
|
|
164 |
x = x+dx
|
|
165 |
canvas.setFillColor(white)
|
|
166 |
canvas.setStrokeColor(white)
|
|
167 |
canvas.setFont("Helvetica-Bold", 85)
|
|
168 |
canvas.drawCentredString(2.75*inch, 1.3*inch, "SPUMONI")
|
|
169 |
"""
|
|
170 |
|
|
171 |
testspumoni2 = """
|
|
172 |
def spumoni2(canvas):
|
|
173 |
from reportlab.lib.units import inch
|
|
174 |
from reportlab.lib.colors import pink, green, brown, white, black
|
|
175 |
# draw the previous drawing
|
|
176 |
spumoni(canvas)
|
|
177 |
# now put an ice cream cone on top of it:
|
|
178 |
# first draw a triangle (ice cream cone)
|
|
179 |
p = canvas.beginPath()
|
|
180 |
xcenter = 2.75*inch
|
|
181 |
radius = 0.45*inch
|
|
182 |
p.moveTo(xcenter-radius, 1.5*inch)
|
|
183 |
p.lineTo(xcenter+radius, 1.5*inch)
|
|
184 |
p.lineTo(xcenter, 0)
|
|
185 |
canvas.setFillColor(brown)
|
|
186 |
canvas.setStrokeColor(black)
|
|
187 |
canvas.drawPath(p, fill=1)
|
|
188 |
# draw some circles (scoops)
|
|
189 |
y = 1.5*inch
|
|
190 |
for color in (pink, green, brown):
|
|
191 |
canvas.setFillColor(color)
|
|
192 |
canvas.circle(xcenter, y, radius, fill=1)
|
|
193 |
y = y+radius
|
|
194 |
"""
|
|
195 |
|
|
196 |
testbezier = """
|
|
197 |
def bezier(canvas):
|
|
198 |
from reportlab.lib.colors import yellow, green, red, black
|
|
199 |
from reportlab.lib.units import inch
|
|
200 |
i = inch
|
|
201 |
d = i/4
|
|
202 |
# define the bezier curve control points
|
|
203 |
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
|
|
204 |
# draw a figure enclosing the control points
|
|
205 |
canvas.setFillColor(yellow)
|
|
206 |
p = canvas.beginPath()
|
|
207 |
p.moveTo(x1,y1)
|
|
208 |
for (x,y) in [(x2,y2), (x3,y3), (x4,y4)]:
|
|
209 |
p.lineTo(x,y)
|
|
210 |
canvas.drawPath(p, fill=1, stroke=0)
|
|
211 |
# draw the tangent lines
|
|
212 |
canvas.setLineWidth(inch*0.1)
|
|
213 |
canvas.setStrokeColor(green)
|
|
214 |
canvas.line(x1,y1,x2,y2)
|
|
215 |
canvas.setStrokeColor(red)
|
|
216 |
canvas.line(x3,y3,x4,y4)
|
|
217 |
# finally draw the curve
|
|
218 |
canvas.setStrokeColor(black)
|
|
219 |
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4)
|
|
220 |
"""
|
|
221 |
|
|
222 |
testbezier2 = """
|
|
223 |
def bezier2(canvas):
|
|
224 |
from reportlab.lib.colors import yellow, green, red, black
|
|
225 |
from reportlab.lib.units import inch
|
|
226 |
# make a sequence of control points
|
|
227 |
xd,yd = 5.5*inch/2, 3*inch/2
|
|
228 |
xc,yc = xd,yd
|
|
229 |
dxdy = [(0,0.33), (0.33,0.33), (0.75,1), (0.875,0.875),
|
|
230 |
(0.875,0.875), (1,0.75), (0.33,0.33), (0.33,0)]
|
|
231 |
pointlist = []
|
|
232 |
for xoffset in (1,-1):
|
|
233 |
yoffset = xoffset
|
|
234 |
for (dx,dy) in dxdy:
|
|
235 |
px = xc + xd*xoffset*dx
|
|
236 |
py = yc + yd*yoffset*dy
|
|
237 |
pointlist.append((px,py))
|
|
238 |
yoffset = -xoffset
|
|
239 |
for (dy,dx) in dxdy:
|
|
240 |
px = xc + xd*xoffset*dx
|
|
241 |
py = yc + yd*yoffset*dy
|
|
242 |
pointlist.append((px,py))
|
|
243 |
# draw tangent lines and curves
|
|
244 |
canvas.setLineWidth(inch*0.1)
|
|
245 |
while pointlist:
|
|
246 |
[(x1,y1),(x2,y2),(x3,y3),(x4,y4)] = pointlist[:4]
|
|
247 |
del pointlist[:4]
|
|
248 |
canvas.setLineWidth(inch*0.1)
|
|
249 |
canvas.setStrokeColor(green)
|
|
250 |
canvas.line(x1,y1,x2,y2)
|
|
251 |
canvas.setStrokeColor(red)
|
|
252 |
canvas.line(x3,y3,x4,y4)
|
|
253 |
# finally draw the curve
|
|
254 |
canvas.setStrokeColor(black)
|
|
255 |
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4)
|
|
256 |
"""
|
|
257 |
|
|
258 |
testpencil = """
|
|
259 |
def pencil(canvas, text="No.2"):
|
|
260 |
from reportlab.lib.colors import yellow, red, black,white
|
|
261 |
from reportlab.lib.units import inch
|
|
262 |
u = inch/10.0
|
|
263 |
canvas.setStrokeColor(black)
|
|
264 |
canvas.setLineWidth(4)
|
|
265 |
# draw erasor
|
|
266 |
canvas.setFillColor(red)
|
|
267 |
canvas.circle(30*u, 5*u, 5*u, stroke=1, fill=1)
|
|
268 |
# draw all else but the tip (mainly rectangles with different fills)
|
|
269 |
canvas.setFillColor(yellow)
|
|
270 |
canvas.rect(10*u,0,20*u,10*u, stroke=1, fill=1)
|
|
271 |
canvas.setFillColor(black)
|
|
272 |
canvas.rect(23*u,0,8*u,10*u,fill=1)
|
|
273 |
canvas.roundRect(14*u, 3.5*u, 8*u, 3*u, 1.5*u, stroke=1, fill=1)
|
|
274 |
canvas.setFillColor(white)
|
|
275 |
canvas.rect(25*u,u,1.2*u,8*u, fill=1,stroke=0)
|
|
276 |
canvas.rect(27.5*u,u,1.2*u,8*u, fill=1, stroke=0)
|
|
277 |
canvas.setFont("Times-Roman", 3*u)
|
|
278 |
canvas.drawCentredString(18*u, 4*u, text)
|
|
279 |
# now draw the tip
|
|
280 |
penciltip(canvas,debug=0)
|
|
281 |
# draw broken lines across the body.
|
|
282 |
canvas.setDash([10,5,16,10],0)
|
|
283 |
canvas.line(11*u,2.5*u,22*u,2.5*u)
|
|
284 |
canvas.line(22*u,7.5*u,12*u,7.5*u)
|
|
285 |
"""
|
|
286 |
|
|
287 |
testpenciltip = """
|
|
288 |
def penciltip(canvas, debug=1):
|
|
289 |
from reportlab.lib.colors import tan, black, green
|
|
290 |
from reportlab.lib.units import inch
|
|
291 |
u = inch/10.0
|
|
292 |
canvas.setLineWidth(4)
|
|
293 |
if debug:
|
|
294 |
canvas.scale(2.8,2.8) # make it big
|
|
295 |
canvas.setLineWidth(1) # small lines
|
|
296 |
canvas.setStrokeColor(black)
|
|
297 |
canvas.setFillColor(tan)
|
|
298 |
p = canvas.beginPath()
|
|
299 |
p.moveTo(10*u,0)
|
|
300 |
p.lineTo(0,5*u)
|
|
301 |
p.lineTo(10*u,10*u)
|
|
302 |
p.curveTo(11.5*u,10*u, 11.5*u,7.5*u, 10*u,7.5*u)
|
|
303 |
p.curveTo(12*u,7.5*u, 11*u,2.5*u, 9.7*u,2.5*u)
|
|
304 |
p.curveTo(10.5*u,2.5*u, 11*u,0, 10*u,0)
|
|
305 |
canvas.drawPath(p, stroke=1, fill=1)
|
|
306 |
canvas.setFillColor(black)
|
|
307 |
p = canvas.beginPath()
|
|
308 |
p.moveTo(0,5*u)
|
|
309 |
p.lineTo(4*u,3*u)
|
|
310 |
p.lineTo(5*u,4.5*u)
|
|
311 |
p.lineTo(3*u,6.5*u)
|
|
312 |
canvas.drawPath(p, stroke=1, fill=1)
|
|
313 |
if debug:
|
|
314 |
canvas.setStrokeColor(green) # put in a frame of reference
|
|
315 |
canvas.grid([0,5*u,10*u,15*u], [0,5*u,10*u])
|
|
316 |
"""
|
|
317 |
|
|
318 |
testnoteannotation = """
|
|
319 |
from reportlab.platypus.flowables import Flowable
|
|
320 |
class NoteAnnotation(Flowable):
|
|
321 |
'''put a pencil in the margin.'''
|
|
322 |
def wrap(self, *args):
|
|
323 |
return (1,10) # I take up very little space! (?)
|
|
324 |
def draw(self):
|
|
325 |
canvas = self.canv
|
|
326 |
canvas.translate(-10,-10)
|
|
327 |
canvas.rotate(180)
|
|
328 |
canvas.scale(0.2,0.2)
|
|
329 |
pencil(canvas, text="NOTE")
|
|
330 |
"""
|
|
331 |
|
|
332 |
testhandannotation = """
|
|
333 |
from reportlab.platypus.flowables import Flowable
|
|
334 |
from reportlab.lib.colors import tan, green
|
|
335 |
class HandAnnotation(Flowable):
|
|
336 |
'''A hand flowable.'''
|
|
337 |
def __init__(self, xoffset=0, size=None, fillcolor=tan, strokecolor=green):
|
|
338 |
from reportlab.lib.units import inch
|
|
339 |
if size is None: size=4*inch
|
|
340 |
self.fillcolor, self.strokecolor = fillcolor, strokecolor
|
|
341 |
self.xoffset = xoffset
|
|
342 |
self.size = size
|
|
343 |
# normal size is 4 inches
|
|
344 |
self.scale = size/(4.0*inch)
|
|
345 |
def wrap(self, *args):
|
|
346 |
return (self.xoffset, self.size)
|
|
347 |
def draw(self):
|
|
348 |
canvas = self.canv
|
|
349 |
canvas.setLineWidth(6)
|
|
350 |
canvas.setFillColor(self.fillcolor)
|
|
351 |
canvas.setStrokeColor(self.strokecolor)
|
|
352 |
canvas.translate(self.xoffset+self.size,0)
|
|
353 |
canvas.rotate(90)
|
|
354 |
canvas.scale(self.scale, self.scale)
|
|
355 |
hand(canvas, debug=0, fill=1)
|
|
356 |
"""
|
|
357 |
|
|
358 |
lyrics = '''\
|
|
359 |
well she hit Net Solutions
|
|
360 |
and she registered her own .com site now
|
|
361 |
and filled it up with yahoo profile pics
|
|
362 |
she snarfed in one night now
|
|
363 |
and she made 50 million when Hugh Hefner
|
|
364 |
bought up the rights now
|
|
365 |
and she'll have fun fun fun
|
|
366 |
til her Daddy takes the keyboard away'''
|
|
367 |
|
|
368 |
lyrics = string.split(lyrics, "\n")
|
|
369 |
testtextsize = """
|
|
370 |
def textsize(canvas):
|
|
371 |
from reportlab.lib.units import inch
|
|
372 |
from reportlab.lib.colors import magenta, red
|
|
373 |
canvas.setFont("Times-Roman", 20)
|
|
374 |
canvas.setFillColor(red)
|
|
375 |
canvas.drawCentredString(2.75*inch, 2.5*inch, "Font size examples")
|
|
376 |
canvas.setFillColor(magenta)
|
|
377 |
size = 7
|
|
378 |
y = 2.3*inch
|
|
379 |
x = 1.3*inch
|
|
380 |
for line in lyrics:
|
|
381 |
canvas.setFont("Helvetica", size)
|
|
382 |
canvas.drawRightString(x,y,"%s points: " % size)
|
|
383 |
canvas.drawString(x,y, line)
|
|
384 |
y = y-size*1.2
|
|
385 |
size = size+1.5
|
|
386 |
"""
|
|
387 |
|
|
388 |
teststar = """
|
|
389 |
def star(canvas, title="Title Here", aka="Comment here.",
|
|
390 |
xcenter=None, ycenter=None, nvertices=5):
|
|
391 |
from math import pi
|
|
392 |
from reportlab.lib.units import inch
|
|
393 |
radius=inch/3.0
|
|
394 |
if xcenter is None: xcenter=2.75*inch
|
|
395 |
if ycenter is None: ycenter=1.5*inch
|
|
396 |
canvas.drawCentredString(xcenter, ycenter+1.3*radius, title)
|
|
397 |
canvas.drawCentredString(xcenter, ycenter-1.4*radius, aka)
|
|
398 |
p = canvas.beginPath()
|
|
399 |
p.moveTo(xcenter,ycenter+radius)
|
|
400 |
from math import pi, cos, sin
|
|
401 |
angle = (2*pi)*2/5.0
|
|
402 |
startangle = pi/2.0
|
|
403 |
for vertex in range(nvertices-1):
|
|
404 |
nextangle = angle*(vertex+1)+startangle
|
|
405 |
x = xcenter + radius*cos(nextangle)
|
|
406 |
y = ycenter + radius*sin(nextangle)
|
|
407 |
p.lineTo(x,y)
|
|
408 |
if nvertices==5:
|
|
409 |
p.close()
|
|
410 |
canvas.drawPath(p)
|
|
411 |
"""
|
|
412 |
|
|
413 |
testjoins = """
|
|
414 |
def joins(canvas):
|
|
415 |
from reportlab.lib.units import inch
|
|
416 |
# make lines big
|
|
417 |
canvas.setLineWidth(5)
|
|
418 |
star(canvas, "Default: mitered join", "0: pointed", xcenter = 1*inch)
|
|
419 |
canvas.setLineJoin(1)
|
|
420 |
star(canvas, "Round join", "1: rounded")
|
|
421 |
canvas.setLineJoin(2)
|
|
422 |
star(canvas, "Bevelled join", "2: square", xcenter=4.5*inch)
|
|
423 |
"""
|
|
424 |
|
|
425 |
testcaps = """
|
|
426 |
def caps(canvas):
|
|
427 |
from reportlab.lib.units import inch
|
|
428 |
# make lines big
|
|
429 |
canvas.setLineWidth(5)
|
|
430 |
star(canvas, "Default", "no projection",xcenter = 1*inch,
|
|
431 |
nvertices=4)
|
|
432 |
canvas.setLineCap(1)
|
|
433 |
star(canvas, "Round cap", "1: ends in half circle", nvertices=4)
|
|
434 |
canvas.setLineCap(2)
|
|
435 |
star(canvas, "Square cap", "2: projects out half a width", xcenter=4.5*inch,
|
|
436 |
nvertices=4)
|
|
437 |
"""
|
|
438 |
|
|
439 |
testdashes = """
|
|
440 |
def dashes(canvas):
|
|
441 |
from reportlab.lib.units import inch
|
|
442 |
# make lines big
|
|
443 |
canvas.setDash(6,3)
|
|
444 |
star(canvas, "Simple dashes", "6 points on, 3 off", xcenter = 1*inch)
|
|
445 |
canvas.setDash(1,2)
|
|
446 |
star(canvas, "Dots", "One on, two off")
|
|
447 |
canvas.setDash([1,1,3,3,1,4,4,1], 0)
|
|
448 |
star(canvas, "Complex Pattern", "[1,1,3,3,1,4,4,1]", xcenter=4.5*inch)
|
|
449 |
"""
|
|
450 |
|
|
451 |
testcursormoves1 = """
|
|
452 |
def cursormoves1(canvas):
|
|
453 |
from reportlab.lib.units import inch
|
|
454 |
textobject = canvas.beginText()
|
|
455 |
textobject.setTextOrigin(inch, 2.5*inch)
|
|
456 |
textobject.setFont("Helvetica-Oblique", 14)
|
|
457 |
for line in lyrics:
|
|
458 |
textobject.textLine(line)
|
|
459 |
textobject.setFillGray(0.4)
|
|
460 |
textobject.textLines('''
|
|
461 |
With many apologies to the Beach Boys
|
|
462 |
and anyone else who finds this objectionable
|
|
463 |
''')
|
|
464 |
canvas.drawText(textobject)
|
|
465 |
"""
|
|
466 |
|
|
467 |
testcursormoves2 = """
|
|
468 |
def cursormoves2(canvas):
|
|
469 |
from reportlab.lib.units import inch
|
|
470 |
textobject = canvas.beginText()
|
|
471 |
textobject.setTextOrigin(2, 2.5*inch)
|
|
472 |
textobject.setFont("Helvetica-Oblique", 14)
|
|
473 |
for line in lyrics:
|
|
474 |
textobject.textOut(line)
|
|
475 |
textobject.moveCursor(14,14) # POSITIVE Y moves down!!!
|
|
476 |
textobject.setFillColorRGB(0.4,0,1)
|
|
477 |
textobject.textLines('''
|
|
478 |
With many apologies to the Beach Boys
|
|
479 |
and anyone else who finds this objectionable
|
|
480 |
''')
|
|
481 |
canvas.drawText(textobject)
|
|
482 |
"""
|
|
483 |
|
|
484 |
testcharspace = """
|
|
485 |
def charspace(canvas):
|
|
486 |
from reportlab.lib.units import inch
|
|
487 |
textobject = canvas.beginText()
|
|
488 |
textobject.setTextOrigin(3, 2.5*inch)
|
|
489 |
textobject.setFont("Helvetica-Oblique", 10)
|
|
490 |
charspace = 0
|
|
491 |
for line in lyrics:
|
|
492 |
textobject.setCharSpace(charspace)
|
|
493 |
textobject.textLine("%s: %s" %(charspace,line))
|
|
494 |
charspace = charspace+0.5
|
|
495 |
textobject.setFillGray(0.4)
|
|
496 |
textobject.textLines('''
|
|
497 |
With many apologies to the Beach Boys
|
|
498 |
and anyone else who finds this objectionable
|
|
499 |
''')
|
|
500 |
canvas.drawText(textobject)
|
|
501 |
"""
|
|
502 |
|
|
503 |
testwordspace = """
|
|
504 |
def wordspace(canvas):
|
|
505 |
from reportlab.lib.units import inch
|
|
506 |
textobject = canvas.beginText()
|
|
507 |
textobject.setTextOrigin(3, 2.5*inch)
|
|
508 |
textobject.setFont("Helvetica-Oblique", 12)
|
|
509 |
wordspace = 0
|
|
510 |
for line in lyrics:
|
|
511 |
textobject.setWordSpace(wordspace)
|
|
512 |
textobject.textLine("%s: %s" %(wordspace,line))
|
|
513 |
wordspace = wordspace+2.5
|
|
514 |
textobject.setFillColorCMYK(0.4,0,0.4,0.2)
|
|
515 |
textobject.textLines('''
|
|
516 |
With many apologies to the Beach Boys
|
|
517 |
and anyone else who finds this objectionable
|
|
518 |
''')
|
|
519 |
canvas.drawText(textobject)
|
|
520 |
"""
|
|
521 |
testhorizontalscale = """
|
|
522 |
def horizontalscale(canvas):
|
|
523 |
from reportlab.lib.units import inch
|
|
524 |
textobject = canvas.beginText()
|
|
525 |
textobject.setTextOrigin(3, 2.5*inch)
|
|
526 |
textobject.setFont("Helvetica-Oblique", 12)
|
|
527 |
horizontalscale = 80 # 100 is default
|
|
528 |
for line in lyrics:
|
|
529 |
textobject.setHorizScale(horizontalscale)
|
|
530 |
textobject.textLine("%s: %s" %(horizontalscale,line))
|
|
531 |
horizontalscale = horizontalscale+10
|
|
532 |
textobject.setFillColorCMYK(0.0,0.4,0.4,0.2)
|
|
533 |
textobject.textLines('''
|
|
534 |
With many apologies to the Beach Boys
|
|
535 |
and anyone else who finds this objectionable
|
|
536 |
''')
|
|
537 |
canvas.drawText(textobject)
|
|
538 |
"""
|
|
539 |
testleading = """
|
|
540 |
def leading(canvas):
|
|
541 |
from reportlab.lib.units import inch
|
|
542 |
textobject = canvas.beginText()
|
|
543 |
textobject.setTextOrigin(3, 2.5*inch)
|
|
544 |
textobject.setFont("Helvetica-Oblique", 14)
|
|
545 |
leading = 8
|
|
546 |
for line in lyrics:
|
|
547 |
textobject.setLeading(leading)
|
|
548 |
textobject.textLine("%s: %s" %(leading,line))
|
|
549 |
leading = leading+2.5
|
|
550 |
textobject.setFillColorCMYK(0.8,0,0,0.3)
|
|
551 |
textobject.textLines('''
|
|
552 |
With many apologies to the Beach Boys
|
|
553 |
and anyone else who finds this objectionable
|
|
554 |
''')
|
|
555 |
canvas.drawText(textobject)
|
|
556 |
"""
|
|
557 |
|
|
558 |
testhand = """
|
|
559 |
def hand(canvas, debug=1, fill=0):
|
|
560 |
(startx, starty) = (0,0)
|
|
561 |
curves = [
|
|
562 |
( 0, 2), ( 0, 4), ( 0, 8), # back of hand
|
|
563 |
( 5, 8), ( 7,10), ( 7,14),
|
|
564 |
(10,14), (10,13), ( 7.5, 8), # thumb
|
|
565 |
(13, 8), (14, 8), (17, 8),
|
|
566 |
(19, 8), (19, 6), (17, 6),
|
|
567 |
(15, 6), (13, 6), (11, 6), # index, pointing
|
|
568 |
(12, 6), (13, 6), (14, 6),
|
|
569 |
(16, 6), (16, 4), (14, 4),
|
|
570 |
(13, 4), (12, 4), (11, 4), # middle
|
|
571 |
(11.5, 4), (12, 4), (13, 4),
|
|
572 |
(15, 4), (15, 2), (13, 2),
|
|
573 |
(12.5, 2), (11.5, 2), (11, 2), # ring
|
|
574 |
(11.5, 2), (12, 2), (12.5, 2),
|
|
575 |
(14, 2), (14, 0), (12.5, 0),
|
|
576 |
(10, 0), (8, 0), (6, 0), # pinky, then close
|
|
577 |
]
|
|
578 |
from reportlab.lib.units import inch
|
|
579 |
if debug: canvas.setLineWidth(6)
|
|
580 |
u = inch*0.2
|
|
581 |
p = canvas.beginPath()
|
|
582 |
p.moveTo(startx, starty)
|
|
583 |
ccopy = list(curves)
|
|
584 |
while ccopy:
|
|
585 |
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3]
|
|
586 |
del ccopy[:3]
|
|
587 |
p.curveTo(x1*u,y1*u,x2*u,y2*u,x3*u,y3*u)
|
|
588 |
p.close()
|
|
589 |
canvas.drawPath(p, fill=fill)
|
|
590 |
if debug:
|
|
591 |
from reportlab.lib.colors import red, green
|
|
592 |
(lastx, lasty) = (startx, starty)
|
|
593 |
ccopy = list(curves)
|
|
594 |
while ccopy:
|
|
595 |
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3]
|
|
596 |
del ccopy[:3]
|
|
597 |
canvas.setStrokeColor(red)
|
|
598 |
canvas.line(lastx*u,lasty*u, x1*u,y1*u)
|
|
599 |
canvas.setStrokeColor(green)
|
|
600 |
canvas.line(x2*u,y2*u, x3*u,y3*u)
|
|
601 |
(lastx,lasty) = (x3,y3)
|
|
602 |
"""
|
|
603 |
|
|
604 |
testhand2 = """
|
|
605 |
def hand2(canvas):
|
|
606 |
canvas.translate(20,10)
|
|
607 |
canvas.setLineWidth(3)
|
|
608 |
canvas.setFillColorRGB(0.1, 0.3, 0.9)
|
|
609 |
canvas.setStrokeGray(0.5)
|
|
610 |
hand(canvas, debug=0, fill=1)
|
|
611 |
"""
|
|
612 |
|
|
613 |
testfonts = """
|
|
614 |
def fonts(canvas):
|
|
615 |
from reportlab.lib.units import inch
|
|
616 |
text = "Now is the time for all good men to..."
|
|
617 |
x = 1.8*inch
|
|
618 |
y = 2.7*inch
|
|
619 |
for font in canvas.getAvailableFonts():
|
|
620 |
canvas.setFont(font, 10)
|
|
621 |
canvas.drawString(x,y,text)
|
|
622 |
canvas.setFont("Helvetica", 10)
|
|
623 |
canvas.drawRightString(x-10,y, font+":")
|
|
624 |
y = y-13
|
|
625 |
"""
|
|
626 |
|
|
627 |
testarcs = """
|
|
628 |
def arcs(canvas):
|
|
629 |
from reportlab.lib.units import inch
|
|
630 |
canvas.setLineWidth(4)
|
|
631 |
canvas.setStrokeColorRGB(0.8, 1, 0.6)
|
|
632 |
# draw rectangles enclosing the arcs
|
|
633 |
canvas.rect(inch, inch, 1.5*inch, inch)
|
|
634 |
canvas.rect(3*inch, inch, inch, 1.5*inch)
|
|
635 |
canvas.setStrokeColorRGB(0, 0.2, 0.4)
|
|
636 |
canvas.setFillColorRGB(1, 0.6, 0.8)
|
|
637 |
p = canvas.beginPath()
|
|
638 |
p.moveTo(0.2*inch, 0.2*inch)
|
|
639 |
p.arcTo(inch, inch, 2.5*inch,2*inch, startAng=-30, extent=135)
|
|
640 |
p.arc(3*inch, inch, 4*inch, 2.5*inch, startAng=-45, extent=270)
|
|
641 |
canvas.drawPath(p, fill=1, stroke=1)
|
|
642 |
"""
|
|
643 |
testvariousshapes = """
|
|
644 |
def variousshapes(canvas):
|
|
645 |
from reportlab.lib.units import inch
|
|
646 |
inch = int(inch)
|
|
647 |
canvas.setStrokeGray(0.5)
|
|
648 |
canvas.grid(range(0,11*inch/2,inch/2), range(0,7*inch/2,inch/2))
|
|
649 |
canvas.setLineWidth(4)
|
|
650 |
canvas.setStrokeColorRGB(0, 0.2, 0.7)
|
|
651 |
canvas.setFillColorRGB(1, 0.6, 0.8)
|
|
652 |
p = canvas.beginPath()
|
|
653 |
p.rect(0.5*inch, 0.5*inch, 0.5*inch, 2*inch)
|
|
654 |
p.circle(2.75*inch, 1.5*inch, 0.3*inch)
|
|
655 |
p.ellipse(3.5*inch, 0.5*inch, 1.2*inch, 2*inch)
|
|
656 |
canvas.drawPath(p, fill=1, stroke=1)
|
|
657 |
"""
|
|
658 |
|
|
659 |
testclosingfigures = """
|
|
660 |
def closingfigures(canvas):
|
|
661 |
from reportlab.lib.units import inch
|
|
662 |
h = inch/3.0; k = inch/2.0
|
|
663 |
canvas.setStrokeColorRGB(0.2,0.3,0.5)
|
|
664 |
canvas.setFillColorRGB(0.8,0.6,0.2)
|
|
665 |
canvas.setLineWidth(4)
|
|
666 |
p = canvas.beginPath()
|
|
667 |
for i in (1,2,3,4):
|
|
668 |
for j in (1,2):
|
|
669 |
xc,yc = inch*i, inch*j
|
|
670 |
p.moveTo(xc,yc)
|
|
671 |
p.arcTo(xc-h, yc-k, xc+h, yc+k, startAng=0, extent=60*i)
|
|
672 |
# close only the first one, not the second one
|
|
673 |
if j==1:
|
|
674 |
p.close()
|
|
675 |
canvas.drawPath(p, fill=1, stroke=1)
|
|
676 |
"""
|
|
677 |
|
|
678 |
testforms = """
|
|
679 |
def forms(canvas):
|
|
680 |
#first create a form...
|
|
681 |
canvas.beginForm("SpumoniForm")
|
|
682 |
#re-use some drawing functions from earlier
|
|
683 |
spumoni(canvas)
|
|
684 |
canvas.endForm()
|
|
685 |
|
|
686 |
#then draw it
|
|
687 |
canvas.doForm("SpumoniForm")
|
|
688 |
"""
|
|
689 |
|
|
690 |
def doctemplateillustration(canvas):
|
|
691 |
from reportlab.lib.units import inch
|
|
692 |
canvas.setFont("Helvetica", 10)
|
|
693 |
canvas.drawString(inch/4.0, 2.75*inch, "DocTemplate")
|
|
694 |
W = 4/3.0*inch
|
|
695 |
H = 2*inch
|
|
696 |
Wd = x = inch/4.0
|
|
697 |
Hd =y = inch/2.0
|
|
698 |
for name in ("two column", "chapter page", "title page"):
|
|
699 |
canvas.setFillColorRGB(0.5,1.0,1.0)
|
|
700 |
canvas.rect(x,y,W,H, fill=1)
|
|
701 |
canvas.setFillColorRGB(0,0,0)
|
|
702 |
canvas.drawString(x+inch/8, y+H-Wd, "PageTemplate")
|
|
703 |
canvas.drawCentredString(x+W/2.0, y-Wd, name)
|
|
704 |
x = x+W+Wd
|
|
705 |
canvas.saveState()
|
|
706 |
d = inch/16
|
|
707 |
dW = (W-3*d)/2.0
|
|
708 |
hD = H -2*d-Wd
|
|
709 |
canvas.translate(Wd+d, Hd+d)
|
|
710 |
for name in ("left Frame", "right Frame"):
|
|
711 |
canvas.setFillColorRGB(1.0,0.5,1.0)
|
|
712 |
canvas.rect(0,0, dW,hD, fill=1)
|
|
713 |
canvas.setFillGray(0.7)
|
|
714 |
dd= d/2.0
|
|
715 |
ddH = (hD-6*dd)/5.0
|
|
716 |
ddW = dW-2*dd
|
|
717 |
yy = dd
|
|
718 |
xx = dd
|
|
719 |
for i in range(5):
|
|
720 |
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=0)
|
|
721 |
yy = yy+ddH+dd
|
|
722 |
canvas.setFillColorRGB(0,0,0)
|
|
723 |
canvas.saveState()
|
|
724 |
canvas.rotate(90)
|
|
725 |
canvas.drawString(d,-dW/2, name)
|
|
726 |
canvas.restoreState()
|
|
727 |
canvas.translate(dW+d,0)
|
|
728 |
canvas.restoreState()
|
|
729 |
canvas.setFillColorRGB(1.0, 0.5, 1.0)
|
|
730 |
mx = Wd+W+Wd+d
|
|
731 |
my = Hd+d
|
|
732 |
mW = W-2*d
|
|
733 |
mH = H-d-Hd
|
|
734 |
canvas.rect(mx, my, mW, mH, fill=1)
|
|
735 |
canvas.rect(Wd+2*(W+Wd)+d, Hd+3*d, W-2*d, H/2.0, fill=1)
|
|
736 |
canvas.setFillGray(0.7)
|
|
737 |
canvas.rect(Wd+2*(W+Wd)+d+dd, Hd+5*d, W-2*d-2*dd, H/2.0-2*d-dd, fill=1)
|
|
738 |
xx = mx+dd
|
|
739 |
yy = my+mH/5.0
|
|
740 |
ddH = (mH-6*dd-mH/5.0)/3.0
|
|
741 |
ddW = mW - 2*dd
|
|
742 |
for i in range(3):
|
|
743 |
canvas.setFillGray(0.7)
|
|
744 |
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=1)
|
|
745 |
canvas.setFillGray(0)
|
|
746 |
canvas.drawString(xx+dd/2.0,yy+dd/2.0, "flowable %s" %(157-i))
|
|
747 |
yy = yy+ddH+dd
|
|
748 |
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H/2.0, "First Flowable")
|
|
749 |
canvas.setFont("Times-BoldItalic", 8)
|
|
750 |
canvas.setFillGray(0)
|
|
751 |
canvas.drawCentredString(mx+mW/2.0, my+mH+3*dd, "Chapter 6: Lubricants")
|
|
752 |
canvas.setFont("Times-BoldItalic", 10)
|
|
753 |
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H-H/4, "College Life")
|
|
754 |
|
|
755 |
class PlatIllust:
|
|
756 |
#wrap the above for PP#
|
|
757 |
def __init__(self, x, y, scale=1):
|
|
758 |
self.x = x
|
|
759 |
self.y = y
|
|
760 |
self.scale = scale
|
|
761 |
def drawOn(self, canvas):
|
|
762 |
canvas.saveState()
|
|
763 |
canvas.translate(self.x, self.y)
|
|
764 |
canvas.scale(self.scale, self.scale)
|
|
765 |
doctemplateillustration(canvas)
|
|
766 |
canvas.restoreState()
|
|
767 |
|
|
768 |
class PingoIllust:
|
|
769 |
#wrap the above for PP#
|
|
770 |
def __init__(self, x, y, scale=1):
|
|
771 |
## print 'Pingo illustration %f, %f, %f' % (x,y,scale)
|
|
772 |
self.x = x
|
|
773 |
self.y = y
|
|
774 |
self.scale = scale
|
|
775 |
def drawOn(self, canvas):
|
|
776 |
canvas.rect(self.x, self.y, 100,100, stroke=1, fill=1)
|
|
777 |
## from pingo import testdrawings
|
|
778 |
## from pingo import pingopdf
|
|
779 |
## drawing = testdrawings.getDrawing3()
|
|
780 |
## canvas.saveState()
|
|
781 |
## canvas.scale(self.scale, self.scale)
|
|
782 |
## pingopdf.draw(drawing, canvas, self.x, self.y)
|
|
783 |
## canvas.restoreState()
|
|
784 |
|
|
785 |
# D = dir()
|
|
786 |
g = globals()
|
|
787 |
Dprime = {}
|
|
788 |
from types import StringType
|
|
789 |
from string import strip
|
|
790 |
for (a,b) in g.items():
|
|
791 |
if a[:4]=="test" and type(b) is StringType:
|
|
792 |
#print 'for', a
|
|
793 |
#print b
|
|
794 |
b = strip(b)
|
|
795 |
exec(b+'\n')
|
|
796 |
|
|
797 |
platypussetup = """
|
|
798 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
|
799 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
800 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE
|
|
801 |
from reportlab.lib.units import inch
|
|
802 |
PAGE_HEIGHT=DEFAULT_PAGE_SIZE[1]; PAGE_WIDTH=DEFAULT_PAGE_SIZE[0]
|
|
803 |
styles = getSampleStyleSheet()
|
|
804 |
"""
|
|
805 |
platypusfirstpage = """
|
|
806 |
Title = "Hello world"
|
|
807 |
pageinfo = "platypus example"
|
|
808 |
def myFirstPage(canvas, doc):
|
|
809 |
canvas.saveState()
|
|
810 |
canvas.setFont('Times-Bold',16)
|
|
811 |
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
|
|
812 |
canvas.setFont('Times-Roman',9)
|
|
813 |
canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
|
|
814 |
canvas.restoreState()
|
|
815 |
"""
|
|
816 |
platypusnextpage = """
|
|
817 |
def myLaterPages(canvas, doc):
|
|
818 |
canvas.saveState()
|
|
819 |
canvas.setFont('Times-Roman',9)
|
|
820 |
canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
|
|
821 |
canvas.restoreState()
|
|
822 |
"""
|
|
823 |
platypusgo = """
|
|
824 |
def go():
|
|
825 |
doc = SimpleDocTemplate("phello.pdf")
|
|
826 |
Story = [Spacer(1,2*inch)]
|
|
827 |
style = styles["Normal"]
|
|
828 |
for i in range(100):
|
|
829 |
bogustext = ("This is Paragraph number %s. " % i) *20
|
|
830 |
p = Paragraph(bogustext, style)
|
|
831 |
Story.append(p)
|
|
832 |
Story.append(Spacer(1,0.2*inch))
|
|
833 |
doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
|
|
834 |
"""
|
|
835 |
|
|
836 |
if __name__=="__main__":
|
|
837 |
# then do the platypus hello world
|
|
838 |
for b in platypussetup, platypusfirstpage, platypusnextpage, platypusgo:
|
|
839 |
b = strip(b)
|
|
840 |
exec(b+'\n')
|
|
841 |
go()
|