2963
|
1 |
#!/bin/env python
|
2966
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2008
|
2963
|
3 |
#see license.txt for license details
|
|
4 |
__version__=''' $Id$ '''
|
|
5 |
__doc__='testscript for reportlab.pdfgen'
|
|
6 |
#tests and documents new low-level canvas and the pycanvas module to output Python source code.
|
2987
|
7 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
|
2984
|
8 |
setOutDir(__name__)
|
2966
|
9 |
import string, os, unittest
|
2963
|
10 |
from reportlab.pdfgen import pycanvas # gmcm 2000/10/13, pdfgen now a package
|
|
11 |
from reportlab.lib.units import inch, cm
|
|
12 |
from reportlab.lib import colors
|
2966
|
13 |
from reportlab.lib.utils import haveImages, rl_isfile
|
2963
|
14 |
|
|
15 |
#################################################################
|
|
16 |
#
|
|
17 |
# first some drawing utilities
|
|
18 |
#
|
|
19 |
#
|
|
20 |
################################################################
|
|
21 |
|
|
22 |
BASEFONT = ('Times-Roman', 10)
|
|
23 |
def framePageForm(c):
|
|
24 |
c.beginForm("frame")
|
|
25 |
c.saveState()
|
|
26 |
# forms can't do non-constant operations
|
|
27 |
#canvas.setFont('Times-BoldItalic',20)
|
|
28 |
#canvas.drawString(inch, 10.5 * inch, title)
|
|
29 |
|
|
30 |
#c.setFont('Times-Roman',10)
|
|
31 |
#c.drawCentredString(4.135 * inch, 0.75 * inch,
|
|
32 |
# 'Page %d' % c.getPageNumber())
|
|
33 |
|
|
34 |
#draw a border
|
|
35 |
c.setFillColor(colors.ReportLabBlue)
|
|
36 |
c.rect(0.3*inch, inch, 0.5*inch, 10*inch, fill=1)
|
|
37 |
from reportlab.lib import corp
|
|
38 |
c.translate(0.8*inch, 9.6*inch)
|
|
39 |
c.rotate(90)
|
|
40 |
logo = corp.ReportLabLogo(width=1.3*inch, height=0.5*inch, powered_by=1)
|
|
41 |
c.setFillColorRGB(1,1,1)
|
|
42 |
c.setStrokeColorRGB(1,1,1)
|
|
43 |
logo.draw(c)
|
|
44 |
#c.setStrokeColorRGB(1,0,0)
|
|
45 |
#c.setLineWidth(5)
|
|
46 |
#c.line(0.8 * inch, inch, 0.8 * inch, 10.75 * inch)
|
|
47 |
#reset carefully afterwards
|
|
48 |
#canvas.setLineWidth(1)
|
|
49 |
#canvas.setStrokeColorRGB(0,0,0)\
|
|
50 |
c.restoreState()
|
|
51 |
c.endForm()
|
|
52 |
|
|
53 |
titlelist = []
|
|
54 |
closeit = 0
|
|
55 |
|
|
56 |
|
|
57 |
def framePage(canvas, title):
|
|
58 |
global closeit
|
|
59 |
titlelist.append(title)
|
|
60 |
#canvas._inPage0() # do we need this at all? would be good to eliminate it
|
|
61 |
canvas.saveState()
|
|
62 |
canvas.setFont('Times-BoldItalic',20)
|
|
63 |
|
|
64 |
canvas.drawString(inch, 10.5 * inch, title)
|
|
65 |
canvas.bookmarkHorizontalAbsolute(title, 10.8*inch)
|
|
66 |
#newsection(title)
|
|
67 |
canvas.addOutlineEntry(title+" section", title, level=0, closed=closeit)
|
|
68 |
closeit = not closeit # close every other one
|
|
69 |
canvas.setFont('Times-Roman',10)
|
|
70 |
canvas.drawCentredString(4.135 * inch, 0.75 * inch,
|
|
71 |
'Page %d' % canvas.getPageNumber())
|
|
72 |
canvas.restoreState()
|
|
73 |
canvas.doForm("frame")
|
|
74 |
|
|
75 |
|
|
76 |
def makesubsection(canvas, title, horizontal):
|
|
77 |
canvas.bookmarkHorizontalAbsolute(title, horizontal)
|
|
78 |
#newsubsection(title)
|
|
79 |
canvas.addOutlineEntry(title+" subsection", title, level=1)
|
|
80 |
|
|
81 |
|
|
82 |
# outline helpers
|
|
83 |
#outlinenametree = []
|
|
84 |
#def newsection(name):
|
|
85 |
# outlinenametree.append(name)
|
|
86 |
|
|
87 |
|
|
88 |
#def newsubsection(name):
|
|
89 |
# from types import TupleType
|
|
90 |
# thissection = outlinenametree[-1]
|
|
91 |
# if type(thissection) is not TupleType:
|
|
92 |
# subsectionlist = []
|
|
93 |
# thissection = outlinenametree[-1] = (thissection, subsectionlist)
|
|
94 |
# else:
|
|
95 |
# (sectionname, subsectionlist) = thissection
|
|
96 |
# subsectionlist.append(name)
|
|
97 |
|
|
98 |
|
|
99 |
class DocBlock:
|
|
100 |
"""A DocBlock has a chunk of commentary and a chunk of code.
|
|
101 |
It prints the code and commentary, then executes the code,
|
|
102 |
which is presumed to draw in a region reserved for it.
|
|
103 |
"""
|
|
104 |
def __init__(self):
|
|
105 |
self.comment1 = "A doc block"
|
|
106 |
self.code = "canvas.setTextOrigin(cm, cm)\ncanvas.textOut('Hello World')"
|
|
107 |
self.comment2 = "That was a doc block"
|
|
108 |
self.drawHeight = 0
|
|
109 |
|
|
110 |
def _getHeight(self):
|
|
111 |
"splits into lines"
|
|
112 |
self.comment1lines = string.split(self.comment1, '\n')
|
|
113 |
self.codelines = string.split(self.code, '\n')
|
|
114 |
self.comment2lines = string.split(self.comment2, '\n')
|
|
115 |
textheight = (len(self.comment1lines) +
|
|
116 |
len(self.code) +
|
|
117 |
len(self.comment2lines) +
|
|
118 |
18)
|
|
119 |
return max(textheight, self.drawHeight)
|
|
120 |
|
|
121 |
def draw(self, canvas, x, y):
|
|
122 |
#specifies top left corner
|
|
123 |
canvas.saveState()
|
|
124 |
height = self._getHeight()
|
|
125 |
canvas.rect(x, y-height, 6*inch, height)
|
|
126 |
#first draw the text
|
|
127 |
canvas.setTextOrigin(x + 3 * inch, y - 12)
|
|
128 |
canvas.setFont('Times-Roman',10)
|
|
129 |
canvas.textLines(self.comment1)
|
|
130 |
drawCode(canvas, self.code)
|
|
131 |
canvas.textLines(self.comment2)
|
|
132 |
|
|
133 |
#now a box for the drawing, slightly within rect
|
|
134 |
canvas.rect(x + 9, y - height + 9, 198, height - 18)
|
|
135 |
#boundary:
|
|
136 |
self.namespace = {'canvas':canvas,'cm': cm,'inch':inch}
|
|
137 |
canvas.translate(x+9, y - height + 9)
|
|
138 |
codeObj = compile(self.code, '<sample>','exec')
|
|
139 |
exec codeObj in self.namespace
|
|
140 |
|
|
141 |
canvas.restoreState()
|
|
142 |
|
|
143 |
|
|
144 |
def drawAxes(canvas, label):
|
|
145 |
"""draws a couple of little rulers showing the coords -
|
|
146 |
uses points as units so you get an imperial ruler
|
|
147 |
one inch on each side"""
|
|
148 |
#y axis
|
|
149 |
canvas.line(0,0,0,72)
|
|
150 |
for y in range(9):
|
|
151 |
tenths = (y+1) * 7.2
|
|
152 |
canvas.line(-6,tenths,0,tenths)
|
|
153 |
canvas.line(-6, 66, 0, 72) #arrow...
|
|
154 |
canvas.line(6, 66, 0, 72) #arrow...
|
|
155 |
|
|
156 |
canvas.line(0,0,72,0)
|
|
157 |
for x in range(9):
|
|
158 |
tenths = (x+1) * 7.2
|
|
159 |
canvas.line(tenths,-6,tenths, 0)
|
|
160 |
canvas.line(66, -6, 72, 0) #arrow...
|
|
161 |
canvas.line(66, +6, 72, 0) #arrow...
|
|
162 |
|
|
163 |
canvas.drawString(18, 30, label)
|
|
164 |
|
|
165 |
|
|
166 |
def drawCrossHairs(canvas, x, y):
|
|
167 |
"""just a marker for checking text metrics - blue for fun"""
|
|
168 |
|
|
169 |
canvas.saveState()
|
|
170 |
canvas.setStrokeColorRGB(0,1,0)
|
|
171 |
canvas.line(x-6,y,x+6,y)
|
|
172 |
canvas.line(x,y-6,x,y+6)
|
|
173 |
canvas.restoreState()
|
|
174 |
|
|
175 |
|
|
176 |
def drawCode(canvas, code):
|
|
177 |
"""Draws a block of text at current point, indented and in Courier"""
|
|
178 |
canvas.addLiteral('36 0 Td')
|
|
179 |
canvas.setFillColor(colors.blue)
|
|
180 |
canvas.setFont('Courier',10)
|
|
181 |
|
|
182 |
t = canvas.beginText()
|
|
183 |
t.textLines(code)
|
|
184 |
c.drawText(t)
|
|
185 |
|
|
186 |
canvas.setFillColor(colors.black)
|
|
187 |
canvas.addLiteral('-36 0 Td')
|
|
188 |
canvas.setFont('Times-Roman',10)
|
|
189 |
|
|
190 |
|
|
191 |
def makeDocument(filename, pageCallBack=None):
|
|
192 |
#the extra arg is a hack added later, so other
|
|
193 |
#tests can get hold of the canvas just before it is
|
|
194 |
#saved
|
|
195 |
|
|
196 |
c = pycanvas.Canvas(filename)
|
|
197 |
c.setPageCompression(0)
|
|
198 |
c.setPageCallBack(pageCallBack)
|
|
199 |
framePageForm(c) # define the frame form
|
|
200 |
c.showOutline()
|
|
201 |
|
|
202 |
framePage(c, 'PDFgen graphics API test script')
|
|
203 |
makesubsection(c, "PDFgen and PIDDLE", 10*inch)
|
|
204 |
|
|
205 |
t = c.beginText(inch, 10*inch)
|
|
206 |
t.setFont('Times-Roman', 10)
|
|
207 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
208 |
t.textLines("""
|
|
209 |
The ReportLab library permits you to create PDF documents directly from
|
|
210 |
your Python code. The "pdfgen" subpackage is the lowest level exposed
|
|
211 |
to the user and lets you directly position test and graphics on the
|
|
212 |
page, with access to almost the full range of PDF features.
|
|
213 |
The API is intended to closely mirror the PDF / Postscript imaging
|
|
214 |
model. There is an almost one to one correspondence between commands
|
|
215 |
and PDF operators. However, where PDF provides several ways to do a job,
|
|
216 |
we have generally only picked one.
|
|
217 |
The test script attempts to use all of the methods exposed by the Canvas
|
|
218 |
class, defined in reportlab/pdfgen/canvas.py
|
|
219 |
First, let's look at text output. There are some basic commands
|
|
220 |
to draw strings:
|
|
221 |
- canvas.setFont(fontname, fontsize [, leading])
|
|
222 |
- canvas.drawString(x, y, text)
|
|
223 |
- canvas.drawRightString(x, y, text)
|
|
224 |
- canvas.drawCentredString(x, y, text)
|
|
225 |
|
|
226 |
The coordinates are in points starting at the bottom left corner of the
|
|
227 |
page. When setting a font, the leading (i.e. inter-line spacing)
|
|
228 |
defaults to 1.2 * fontsize if the fontsize is not provided.
|
|
229 |
|
|
230 |
For more sophisticated operations, you can create a Text Object, defined
|
|
231 |
in reportlab/pdfgen/testobject.py. Text objects produce tighter PDF, run
|
|
232 |
faster and have many methods for precise control of spacing and position.
|
|
233 |
Basic usage goes as follows:
|
|
234 |
- tx = canvas.beginText(x, y)
|
|
235 |
- tx.textOut('Hello') # this moves the cursor to the right
|
|
236 |
- tx.textLine('Hello again') # prints a line and moves down
|
|
237 |
- y = tx.getY() # getX, getY and getCursor track position
|
|
238 |
- canvas.drawText(tx) # all gets drawn at the end
|
|
239 |
|
|
240 |
The green crosshairs below test whether the text cursor is working
|
|
241 |
properly. They should appear at the bottom left of each relevant
|
|
242 |
substring.
|
|
243 |
""")
|
|
244 |
|
|
245 |
t.setFillColorRGB(1,0,0)
|
|
246 |
t.setTextOrigin(inch, 4*inch)
|
|
247 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
248 |
t.textOut('textOut moves across:')
|
|
249 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
250 |
t.textOut('textOut moves across:')
|
|
251 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
252 |
t.textOut('textOut moves across:')
|
|
253 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
254 |
t.textLine('')
|
|
255 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
256 |
t.textLine('textLine moves down')
|
|
257 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
258 |
t.textLine('textLine moves down')
|
|
259 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
260 |
t.textLine('textLine moves down')
|
|
261 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
262 |
|
|
263 |
t.setTextOrigin(4*inch,3.25*inch)
|
|
264 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
265 |
t.textLines('This is a multi-line\nstring with embedded newlines\ndrawn with textLines().\n')
|
|
266 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
267 |
t.textLines(['This is a list of strings',
|
|
268 |
'drawn with textLines().'])
|
|
269 |
c.drawText(t)
|
|
270 |
|
|
271 |
t = c.beginText(2*inch,2*inch)
|
|
272 |
t.setFont('Times-Roman',10)
|
|
273 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
274 |
t.textOut('Small text.')
|
|
275 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
276 |
t.setFont('Courier',14)
|
|
277 |
t.textOut('Bigger fixed width text.')
|
|
278 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
279 |
t.setFont('Times-Roman',10)
|
|
280 |
t.textOut('Small text again.')
|
|
281 |
drawCrossHairs(c, t.getX(),t.getY())
|
|
282 |
c.drawText(t)
|
|
283 |
|
|
284 |
#mark the cursor where it stopped
|
|
285 |
c.showPage()
|
|
286 |
|
|
287 |
|
|
288 |
##############################################################
|
|
289 |
#
|
|
290 |
# page 2 - line styles
|
|
291 |
#
|
|
292 |
###############################################################
|
|
293 |
|
|
294 |
#page 2 - lines and styles
|
|
295 |
framePage(c, 'Line Drawing Styles')
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
# three line ends, lines drawn the hard way
|
|
300 |
#firt make some vertical end markers
|
|
301 |
c.setDash(4,4)
|
|
302 |
c.setLineWidth(0)
|
|
303 |
c.line(inch,9.2*inch,inch, 7.8*inch)
|
|
304 |
c.line(3*inch,9.2*inch,3*inch, 7.8*inch)
|
|
305 |
c.setDash() #clears it
|
|
306 |
|
|
307 |
c.setLineWidth(5)
|
|
308 |
c.setLineCap(0)
|
|
309 |
p = c.beginPath()
|
|
310 |
p.moveTo(inch, 9*inch)
|
|
311 |
p.lineTo(3*inch, 9*inch)
|
|
312 |
c.drawPath(p)
|
|
313 |
c.drawString(4*inch, 9*inch, 'the default - butt caps project half a width')
|
|
314 |
makesubsection(c, "caps and joins", 8.5*inch)
|
|
315 |
|
|
316 |
c.setLineCap(1)
|
|
317 |
p = c.beginPath()
|
|
318 |
p.moveTo(inch, 8.5*inch)
|
|
319 |
p.lineTo(3*inch, 8.5*inch)
|
|
320 |
c.drawPath(p)
|
|
321 |
c.drawString(4*inch, 8.5*inch, 'round caps')
|
|
322 |
|
|
323 |
c.setLineCap(2)
|
|
324 |
p = c.beginPath()
|
|
325 |
p.moveTo(inch, 8*inch)
|
|
326 |
p.lineTo(3*inch, 8*inch)
|
|
327 |
c.drawPath(p)
|
|
328 |
c.drawString(4*inch, 8*inch, 'square caps')
|
|
329 |
|
|
330 |
c.setLineCap(0)
|
|
331 |
|
|
332 |
# three line joins
|
|
333 |
c.setLineJoin(0)
|
|
334 |
p = c.beginPath()
|
|
335 |
p.moveTo(inch, 7*inch)
|
|
336 |
p.lineTo(2*inch, 7*inch)
|
|
337 |
p.lineTo(inch, 6.7*inch)
|
|
338 |
c.drawPath(p)
|
|
339 |
c.drawString(4*inch, 6.8*inch, 'Default - mitered join')
|
|
340 |
|
|
341 |
c.setLineJoin(1)
|
|
342 |
p = c.beginPath()
|
|
343 |
p.moveTo(inch, 6.5*inch)
|
|
344 |
p.lineTo(2*inch, 6.5*inch)
|
|
345 |
p.lineTo(inch, 6.2*inch)
|
|
346 |
c.drawPath(p)
|
|
347 |
c.drawString(4*inch, 6.3*inch, 'round join')
|
|
348 |
|
|
349 |
c.setLineJoin(2)
|
|
350 |
p = c.beginPath()
|
|
351 |
p.moveTo(inch, 6*inch)
|
|
352 |
p.lineTo(2*inch, 6*inch)
|
|
353 |
p.lineTo(inch, 5.7*inch)
|
|
354 |
c.drawPath(p)
|
|
355 |
c.drawString(4*inch, 5.8*inch, 'bevel join')
|
|
356 |
|
|
357 |
c.setDash(6,6)
|
|
358 |
p = c.beginPath()
|
|
359 |
p.moveTo(inch, 5*inch)
|
|
360 |
p.lineTo(3*inch, 5*inch)
|
|
361 |
c.drawPath(p)
|
|
362 |
c.drawString(4*inch, 5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(0)')
|
|
363 |
makesubsection(c, "dash patterns", 5*inch)
|
|
364 |
|
|
365 |
c.setLineCap(1)
|
|
366 |
p = c.beginPath()
|
|
367 |
p.moveTo(inch, 4.5*inch)
|
|
368 |
p.lineTo(3*inch, 4.5*inch)
|
|
369 |
c.drawPath(p)
|
|
370 |
c.drawString(4*inch, 4.5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(1)')
|
|
371 |
|
|
372 |
c.setLineCap(0)
|
|
373 |
c.setDash([1,2,3,4,5,6],0)
|
|
374 |
p = c.beginPath()
|
|
375 |
p.moveTo(inch, 4.0*inch)
|
|
376 |
p.lineTo(3*inch, 4.0*inch)
|
|
377 |
c.drawPath(p)
|
|
378 |
c.drawString(4*inch, 4*inch, 'dash growing - setDash([1,2,3,4,5,6],0) setLineCap(0)')
|
|
379 |
|
|
380 |
c.setLineCap(1)
|
|
381 |
c.setLineJoin(1)
|
|
382 |
c.setDash(32,12)
|
|
383 |
p = c.beginPath()
|
|
384 |
p.moveTo(inch, 3.0*inch)
|
|
385 |
p.lineTo(2.5*inch, 3.0*inch)
|
|
386 |
p.lineTo(inch, 2*inch)
|
|
387 |
c.drawPath(p)
|
|
388 |
c.drawString(4*inch, 3*inch, 'dash pattern, join and cap style interacting - ')
|
|
389 |
c.drawString(4*inch, 3*inch - 12, 'round join & miter results in sausages')
|
|
390 |
|
|
391 |
c.showPage()
|
|
392 |
|
|
393 |
|
|
394 |
##############################################################
|
|
395 |
#
|
|
396 |
# higher level shapes
|
|
397 |
#
|
|
398 |
###############################################################
|
|
399 |
framePage(c, 'Shape Drawing Routines')
|
|
400 |
|
|
401 |
t = c.beginText(inch, 10*inch)
|
|
402 |
t.textLines("""
|
|
403 |
Rather than making your own paths, you have access to a range of shape routines.
|
|
404 |
These are built in pdfgen out of lines and bezier curves, but use the most compact
|
|
405 |
set of operators possible. We can add any new ones that are of general use at no
|
|
406 |
cost to performance.""")
|
|
407 |
t.textLine()
|
|
408 |
|
|
409 |
#line demo
|
|
410 |
makesubsection(c, "lines", 10*inch)
|
|
411 |
c.line(inch, 8*inch, 3*inch, 8*inch)
|
|
412 |
t.setTextOrigin(4*inch, 8*inch)
|
|
413 |
t.textLine('canvas.line(x1, y1, x2, y2)')
|
|
414 |
|
|
415 |
#bezier demo - show control points
|
|
416 |
makesubsection(c, "bezier curves", 7.5*inch)
|
|
417 |
(x1, y1, x2, y2, x3, y3, x4, y4) = (
|
|
418 |
inch, 6.5*inch,
|
|
419 |
1.2*inch, 7.5 * inch,
|
|
420 |
3*inch, 7.5 * inch,
|
|
421 |
3.5*inch, 6.75 * inch
|
|
422 |
)
|
|
423 |
c.bezier(x1, y1, x2, y2, x3, y3, x4, y4)
|
|
424 |
c.setDash(3,3)
|
|
425 |
c.line(x1,y1,x2,y2)
|
|
426 |
c.line(x3,y3,x4,y4)
|
|
427 |
c.setDash()
|
|
428 |
t.setTextOrigin(4*inch, 7 * inch)
|
|
429 |
t.textLine('canvas.bezier(x1, y1, x2, y2, x3, y3, x4, y4)')
|
|
430 |
|
|
431 |
#rectangle
|
|
432 |
makesubsection(c, "rectangles", 7*inch)
|
|
433 |
c.rect(inch, 5.25 * inch, 2 * inch, 0.75 * inch)
|
|
434 |
t.setTextOrigin(4*inch, 5.5 * inch)
|
|
435 |
t.textLine('canvas.rect(x, y, width, height) - x,y is lower left')
|
|
436 |
|
|
437 |
#wedge
|
|
438 |
makesubsection(c, "wedges", 5*inch)
|
|
439 |
c.wedge(inch, 5*inch, 3*inch, 4*inch, 0, 315)
|
|
440 |
t.setTextOrigin(4*inch, 4.5 * inch)
|
|
441 |
t.textLine('canvas.wedge(x1, y1, x2, y2, startDeg, extentDeg)')
|
|
442 |
t.textLine('Note that this is an elliptical arc, not just circular!')
|
|
443 |
|
|
444 |
#wedge the other way
|
|
445 |
c.wedge(inch, 4*inch, 3*inch, 3*inch, 0, -45)
|
|
446 |
t.setTextOrigin(4*inch, 3.5 * inch)
|
|
447 |
t.textLine('Use a negative extent to go clockwise')
|
|
448 |
|
|
449 |
#circle
|
|
450 |
makesubsection(c, "circles", 3.5*inch)
|
|
451 |
c.circle(1.5*inch, 2*inch, 0.5 * inch)
|
|
452 |
c.circle(3*inch, 2*inch, 0.5 * inch)
|
|
453 |
t.setTextOrigin(4*inch, 2 * inch)
|
|
454 |
t.textLine('canvas.circle(x, y, radius)')
|
|
455 |
c.drawText(t)
|
|
456 |
|
|
457 |
c.showPage()
|
|
458 |
|
|
459 |
##############################################################
|
|
460 |
#
|
|
461 |
# Page 4 - fonts
|
|
462 |
#
|
|
463 |
###############################################################
|
|
464 |
framePage(c, "Font Control")
|
|
465 |
|
|
466 |
c.drawString(inch, 10*inch, 'Listing available fonts...')
|
|
467 |
|
|
468 |
y = 9.5*inch
|
|
469 |
for fontname in c.getAvailableFonts():
|
|
470 |
c.setFont(fontname,24)
|
|
471 |
c.drawString(inch, y, 'This should be %s' % fontname)
|
|
472 |
y = y - 28
|
|
473 |
makesubsection(c, "fonts and colors", 4*inch)
|
|
474 |
|
|
475 |
c.setFont('Times-Roman', 12)
|
|
476 |
t = c.beginText(inch, 4*inch)
|
|
477 |
t.textLines("""Now we'll look at the color functions and how they interact
|
|
478 |
with the text. In theory, a word is just a shape; so setFillColorRGB()
|
|
479 |
determines most of what you see. If you specify other text rendering
|
|
480 |
modes, an outline color could be defined by setStrokeColorRGB() too""")
|
|
481 |
c.drawText(t)
|
|
482 |
|
|
483 |
t = c.beginText(inch, 2.75 * inch)
|
|
484 |
t.setFont('Times-Bold',36)
|
|
485 |
t.setFillColor(colors.green) #green
|
|
486 |
t.textLine('Green fill, no stroke')
|
|
487 |
|
|
488 |
#t.setStrokeColorRGB(1,0,0) #ou can do this in a text object, or the canvas.
|
|
489 |
t.setStrokeColor(colors.red) #ou can do this in a text object, or the canvas.
|
|
490 |
t.setTextRenderMode(2) # fill and stroke
|
|
491 |
t.textLine('Green fill, red stroke - yuk!')
|
|
492 |
|
|
493 |
t.setTextRenderMode(0) # back to default - fill only
|
|
494 |
t.setFillColorRGB(0,0,0) #back to default
|
|
495 |
t.setStrokeColorRGB(0,0,0) #ditto
|
|
496 |
c.drawText(t)
|
|
497 |
c.showPage()
|
|
498 |
|
|
499 |
#########################################################################
|
|
500 |
#
|
|
501 |
# Page 5 - coord transforms
|
|
502 |
#
|
|
503 |
#########################################################################
|
|
504 |
framePage(c, "Coordinate Transforms")
|
|
505 |
c.setFont('Times-Roman', 12)
|
|
506 |
t = c.beginText(inch, 10 * inch)
|
|
507 |
t.textLines("""This shows coordinate transformations. We draw a set of axes,
|
|
508 |
moving down the page and transforming space before each one.
|
|
509 |
You can use saveState() and restoreState() to unroll transformations.
|
|
510 |
Note that functions which track the text cursor give the cursor position
|
|
511 |
in the current coordinate system; so if you set up a 6 inch high frame
|
|
512 |
2 inches down the page to draw text in, and move the origin to its top
|
|
513 |
left, you should stop writing text after six inches and not eight.""")
|
|
514 |
c.drawText(t)
|
|
515 |
|
|
516 |
drawAxes(c, "0. at origin")
|
|
517 |
c.addLiteral('%about to translate space')
|
|
518 |
c.translate(2*inch, 7 * inch)
|
|
519 |
drawAxes(c, '1. translate near top of page')
|
|
520 |
|
|
521 |
c.saveState()
|
|
522 |
c.translate(1*inch, -2 * inch)
|
|
523 |
drawAxes(c, '2. down 2 inches, across 1')
|
|
524 |
c.restoreState()
|
|
525 |
|
|
526 |
c.saveState()
|
|
527 |
c.translate(0, -3 * inch)
|
|
528 |
c.scale(2, -1)
|
|
529 |
drawAxes(c, '3. down 3 from top, scale (2, -1)')
|
|
530 |
c.restoreState()
|
|
531 |
|
|
532 |
c.saveState()
|
|
533 |
c.translate(0, -5 * inch)
|
|
534 |
c.rotate(-30)
|
|
535 |
drawAxes(c, "4. down 5, rotate 30' anticlockwise")
|
|
536 |
c.restoreState()
|
|
537 |
|
|
538 |
c.saveState()
|
|
539 |
c.translate(3 * inch, -5 * inch)
|
|
540 |
c.skew(0,30)
|
|
541 |
drawAxes(c, "5. down 5, 3 across, skew beta 30")
|
|
542 |
c.restoreState()
|
|
543 |
|
|
544 |
c.showPage()
|
|
545 |
|
|
546 |
#########################################################################
|
|
547 |
#
|
|
548 |
# Page 6 - clipping
|
|
549 |
#
|
|
550 |
#########################################################################
|
|
551 |
framePage(c, "Clipping")
|
|
552 |
c.setFont('Times-Roman', 12)
|
|
553 |
t = c.beginText(inch, 10 * inch)
|
|
554 |
t.textLines("""This shows clipping at work. We draw a chequerboard of rectangles
|
|
555 |
into a path object, and clip it. This then forms a mask which limits the region of
|
|
556 |
the page on which one can draw. This paragraph was drawn after setting the clipping
|
|
557 |
path, and so you should only see part of the text.""")
|
|
558 |
c.drawText(t)
|
|
559 |
|
|
560 |
c.saveState()
|
|
561 |
#c.setFillColorRGB(0,0,1)
|
|
562 |
p = c.beginPath()
|
|
563 |
#make a chesboard effect, 1 cm squares
|
|
564 |
for i in range(14):
|
|
565 |
x0 = (3 + i) * cm
|
|
566 |
for j in range(7):
|
|
567 |
y0 = (16 + j) * cm
|
|
568 |
p.rect(x0, y0, 0.85*cm, 0.85*cm)
|
|
569 |
c.addLiteral('%Begin clip path')
|
|
570 |
c.clipPath(p)
|
|
571 |
c.addLiteral('%End clip path')
|
|
572 |
t = c.beginText(3 * cm, 22.5 * cm)
|
|
573 |
t.textLines("""This shows clipping at work. We draw a chequerboard of rectangles
|
|
574 |
into a path object, and clip it. This then forms a mask which limits the region of
|
|
575 |
the page on which one can draw. This paragraph was drawn after setting the clipping
|
|
576 |
path, and so you should only see part of the text.
|
|
577 |
This shows clipping at work. We draw a chequerboard of rectangles
|
|
578 |
into a path object, and clip it. This then forms a mask which limits the region of
|
|
579 |
the page on which one can draw. This paragraph was drawn after setting the clipping
|
|
580 |
path, and so you should only see part of the text.
|
|
581 |
This shows clipping at work. We draw a chequerboard of rectangles
|
|
582 |
into a path object, and clip it. This then forms a mask which limits the region of
|
|
583 |
the page on which one can draw. This paragraph was drawn after setting the clipping
|
|
584 |
path, and so you should only see part of the text.""")
|
|
585 |
c.drawText(t)
|
|
586 |
|
|
587 |
c.restoreState()
|
|
588 |
|
|
589 |
t = c.beginText(inch, 5 * inch)
|
|
590 |
t.textLines("""You can also use text as an outline for clipping with the text render mode.
|
|
591 |
The API is not particularly clean on this and one has to follow the right sequence;
|
|
592 |
this can be optimized shortly.""")
|
|
593 |
c.drawText(t)
|
|
594 |
|
|
595 |
#first the outline
|
|
596 |
c.saveState()
|
|
597 |
t = c.beginText(inch, 3.0 * inch)
|
|
598 |
t.setFont('Helvetica-BoldOblique',108)
|
|
599 |
t.setTextRenderMode(5) #stroke and add to path
|
|
600 |
t.textLine('Python!')
|
|
601 |
t.setTextRenderMode(0)
|
|
602 |
c.drawText(t) #this will make a clipping mask
|
|
603 |
|
|
604 |
#now some small stuff which wil be drawn into the current clip mask
|
|
605 |
t = c.beginText(inch, 4 * inch)
|
|
606 |
t.setFont('Times-Roman',6)
|
|
607 |
t.textLines((('spam ' * 40) + '\n') * 15)
|
|
608 |
c.drawText(t)
|
|
609 |
|
|
610 |
#now reset canvas to get rid of the clipping mask
|
|
611 |
c.restoreState()
|
|
612 |
|
|
613 |
c.showPage()
|
|
614 |
|
|
615 |
|
|
616 |
#########################################################################
|
|
617 |
#
|
|
618 |
# Page 7 - images
|
|
619 |
#
|
|
620 |
#########################################################################
|
|
621 |
framePage(c, "Images")
|
|
622 |
c.setFont('Times-Roman', 12)
|
|
623 |
t = c.beginText(inch, 10 * inch)
|
|
624 |
if not haveImages:
|
|
625 |
c.drawString(inch, 11*inch,
|
|
626 |
"Python Imaging Library not found! Below you see rectangles instead of images.")
|
|
627 |
|
|
628 |
t.textLines("""PDFgen uses the Python Imaging Library to process a very wide variety of image formats.
|
|
629 |
This page shows image capabilities. If I've done things right, the bitmap should have
|
|
630 |
its bottom left corner aligned with the crosshairs.
|
|
631 |
There are two methods for drawing images. The recommended use is to call drawImage.
|
|
632 |
This produces the smallest PDFs and the fastest generation times as each image's binary data is
|
|
633 |
only embedded once in the file. Also you can use advanced features like transparency masks.
|
|
634 |
You can also use drawInlineImage, which puts images in the page stream directly.
|
|
635 |
This is slightly faster for Acrobat to render or for very small images, but wastes
|
|
636 |
space if you use images more than once.""")
|
|
637 |
|
|
638 |
c.drawText(t)
|
2966
|
639 |
import tests
|
2987
|
640 |
from reportlab.lib.testutils import testsFolder
|
2966
|
641 |
gif = os.path.join(testsFolder,'pythonpowered.gif')
|
2963
|
642 |
if haveImages and rl_isfile(gif):
|
|
643 |
c.drawInlineImage(gif,2*inch, 7*inch)
|
|
644 |
else:
|
|
645 |
c.rect(2*inch, 7*inch, 110, 44)
|
|
646 |
|
|
647 |
c.line(1.5*inch, 7*inch, 4*inch, 7*inch)
|
|
648 |
c.line(2*inch, 6.5*inch, 2*inch, 8*inch)
|
|
649 |
c.drawString(4.5 * inch, 7.25*inch, 'inline image drawn at natural size')
|
|
650 |
|
|
651 |
if haveImages and rl_isfile(gif):
|
|
652 |
c.drawInlineImage(gif,2*inch, 5*inch, inch, inch)
|
|
653 |
else:
|
|
654 |
c.rect(2*inch, 5*inch, inch, inch)
|
|
655 |
|
|
656 |
c.line(1.5*inch, 5*inch, 4*inch, 5*inch)
|
|
657 |
c.line(2*inch, 4.5*inch, 2*inch, 6*inch)
|
|
658 |
c.drawString(4.5 * inch, 5.25*inch, 'inline image distorted to fit box')
|
|
659 |
|
|
660 |
c.drawString(1.5 * inch, 4*inch, 'Image XObjects can be defined once in the file and drawn many times.')
|
|
661 |
c.drawString(1.5 * inch, 3.75*inch, 'This results in faster generation and much smaller files.')
|
|
662 |
|
|
663 |
for i in range(5):
|
|
664 |
if haveImages:
|
|
665 |
(w, h) = c.drawImage(gif, (1.5 + i)*inch, 3*inch)
|
|
666 |
else:
|
|
667 |
c.rect((1.5 + i)*inch, 3*inch, 110, 44)
|
|
668 |
|
|
669 |
myMask = [254,255,222,223,0,1]
|
|
670 |
c.drawString(1.5 * inch, 2.5*inch, "The optional 'mask' parameter lets you define transparent colors. We used a color picker")
|
|
671 |
c.drawString(1.5 * inch, 2.3*inch, "to determine that the yellow in the image above is RGB=(225,223,0). We then define a mask")
|
|
672 |
c.drawString(1.5 * inch, 2.1*inch, "spanning these RGB values: %s. The background vanishes!!" % myMask)
|
|
673 |
c.drawString(2.5*inch, 1.2*inch, 'This would normally be obscured')
|
|
674 |
if haveImages:
|
|
675 |
c.drawImage(gif, 3*inch, 1.2*inch, w, h, mask=myMask)
|
|
676 |
else:
|
|
677 |
c.rect(3*inch, 1.2*inch, 110, 44)
|
|
678 |
|
|
679 |
c.showPage()
|
|
680 |
|
|
681 |
|
|
682 |
#########################################################################
|
|
683 |
#
|
|
684 |
# Page 8 - Forms and simple links
|
|
685 |
#
|
|
686 |
#########################################################################
|
|
687 |
framePage(c, "Forms and Links")
|
|
688 |
c.setFont('Times-Roman', 12)
|
|
689 |
t = c.beginText(inch, 10 * inch)
|
|
690 |
t.textLines("""Forms are sequences of text or graphics operations
|
|
691 |
which are stored only once in a PDF file and used as many times
|
|
692 |
as desired. The blue logo bar to the left is an example of a form
|
|
693 |
in this document. See the function framePageForm in this demo script
|
|
694 |
for an example of how to use canvas.beginForm(name, ...) ... canvas.endForm().
|
|
695 |
|
|
696 |
Documents can also contain cross references where (for example) a rectangle
|
|
697 |
on a page may be bound to a position on another page. If the user clicks
|
|
698 |
on the rectangle the PDF viewer moves to the bound position on the other
|
|
699 |
page. There are many other types of annotations and links supported by PDF.
|
|
700 |
|
|
701 |
For example, there is a bookmark to each page in this document and below
|
|
702 |
is a browsable index that jumps to those pages. In addition we show two
|
|
703 |
URL hyperlinks; for these, you specify a rectangle but must draw the contents
|
|
704 |
or any surrounding rectangle yourself.
|
|
705 |
""")
|
|
706 |
c.drawText(t)
|
|
707 |
|
|
708 |
nentries = len(titlelist)
|
|
709 |
xmargin = 3*inch
|
|
710 |
xmax = 7*inch
|
|
711 |
ystart = 6.54*inch
|
|
712 |
ydelta = 0.4*inch
|
|
713 |
for i in range(nentries):
|
|
714 |
yposition = ystart - i*ydelta
|
|
715 |
title = titlelist[i]
|
|
716 |
c.drawString(xmargin, yposition, title)
|
|
717 |
c.linkAbsolute(title, title, (xmargin-ydelta/4, yposition-ydelta/4, xmax, yposition+ydelta/2))
|
|
718 |
|
|
719 |
|
|
720 |
# test URLs
|
|
721 |
r1 = (inch, 3*inch, 5*inch, 3.25*inch) # this is x1,y1,x2,y2
|
|
722 |
c.linkURL('http://www.reportlab.com/', r1, thickness=1, color=colors.green)
|
|
723 |
c.drawString(inch+3, 3*inch+6, 'Hyperlink to www.reportlab.com, with green border')
|
|
724 |
|
|
725 |
r1 = (inch, 2.5*inch, 5*inch, 2.75*inch) # this is x1,y1,x2,y2
|
|
726 |
c.linkURL('mailto:reportlab-users@egroups.com', r1) #, border=0)
|
|
727 |
c.drawString(inch+3, 2.5*inch+6, 'mailto: hyperlink, without border')
|
|
728 |
|
|
729 |
r1 = (inch, 2*inch, 5*inch, 2.25*inch) # this is x1,y1,x2,y2
|
|
730 |
c.linkURL('http://www.reportlab.com/', r1,
|
|
731 |
thickness=2,
|
|
732 |
dashArray=[2,4],
|
|
733 |
color=colors.magenta)
|
|
734 |
c.drawString(inch+3, 2*inch+6, 'Hyperlink with custom border style')
|
|
735 |
|
|
736 |
### now do stuff for the outline
|
|
737 |
#for x in outlinenametree: print x
|
|
738 |
#stop
|
3326
|
739 |
#c.setOutlineNames0(*outlinenametree)
|
2963
|
740 |
return c
|
|
741 |
|
|
742 |
|
|
743 |
def run(filename):
|
|
744 |
c = makeDocument(filename)
|
|
745 |
c.save()
|
|
746 |
source = str(c)
|
|
747 |
open(outputfile("test_pdfgen_pycanvas_out.txt"),"w").write(source)
|
|
748 |
import reportlab.rl_config
|
|
749 |
if reportlab.rl_config.verbose:
|
|
750 |
print source
|
|
751 |
|
|
752 |
|
|
753 |
def pageShapes(c):
|
|
754 |
"""Demonstrates the basic lines and shapes"""
|
|
755 |
|
|
756 |
c.showPage()
|
|
757 |
framePage(c, "Basic line and shape routines""")
|
|
758 |
c.setTextOrigin(inch, 10 * inch)
|
|
759 |
c.setFont('Times-Roman', 12)
|
|
760 |
c.textLines("""pdfgen provides some basic routines for drawing straight and curved lines,
|
|
761 |
and also for solid shapes.""")
|
|
762 |
|
|
763 |
y = 9 * inch
|
|
764 |
d = DocBlock()
|
|
765 |
d.comment1 = 'Lesson one'
|
|
766 |
d.code = "canvas.textOut('hello, world')"
|
|
767 |
print d.code
|
|
768 |
|
|
769 |
d.comment2 = 'Lesson two'
|
|
770 |
|
|
771 |
d.draw(c, inch, 9 * inch)
|
|
772 |
|
|
773 |
|
|
774 |
class PdfgenTestCase(unittest.TestCase):
|
|
775 |
"Make documents with lots of Pdfgen features"
|
|
776 |
|
|
777 |
def test0(self):
|
|
778 |
"Make a PDFgen document with most graphics features"
|
|
779 |
run(outputfile('test_pdfgen_pycanvas.pdf'))
|
|
780 |
|
|
781 |
def makeSuite():
|
|
782 |
return makeSuiteForClasses(PdfgenTestCase)
|
|
783 |
|
|
784 |
|
|
785 |
#noruntests
|
|
786 |
if __name__ == "__main__":
|
|
787 |
unittest.TextTestRunner().run(makeSuite())
|
|
788 |
printLocation()
|