author | rgbecker |
Wed, 25 Oct 2000 08:57:46 +0000 | |
changeset 494 | 54257447cfe9 |
parent 251 | 6f6cd14069f6 |
child 1677 | 1450177dd19e |
permissions | -rw-r--r-- |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/demos/odyssey/odyssey.py?cvsroot=reportlab |
|
4 |
#$Header: /tmp/reportlab/reportlab/demos/odyssey/odyssey.py,v 1.9 2000/10/25 08:57:44 rgbecker Exp $ |
|
5 |
__version__=''' $Id: odyssey.py,v 1.9 2000/10/25 08:57:44 rgbecker Exp $ ''' |
|
16 | 6 |
___doc__='' |
4 | 7 |
#odyssey.py |
8 |
# |
|
9 |
#Demo/benchmark of PDFgen rendering Homer's Odyssey. |
|
10 |
||
11 |
||
12 |
||
13 |
#results on my humble P266 with 64MB: |
|
14 |
# Without page compression: |
|
15 |
# 239 pages in 3.76 seconds = 77 pages per second |
|
16 |
||
17 |
# With textOut rather than textLine, i.e. computing width |
|
18 |
# of every word as we would for wrapping: |
|
19 |
# 239 pages in 10.83 seconds = 22 pages per second |
|
20 |
||
21 |
# With page compression and textLine(): |
|
22 |
# 239 pages in 39.39 seconds = 6 pages per second |
|
23 |
||
10 | 24 |
from reportlab.pdfgen import canvas |
72 | 25 |
import time, os |
4 | 26 |
|
27 |
||
42
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
28 |
from reportlab.lib.units import inch, cm |
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
29 |
from reportlab.lib.pagesizes import A4 |
4 | 30 |
|
31 |
#precalculate some basics |
|
32 |
top_margin = A4[1] - inch |
|
33 |
bottom_margin = inch |
|
34 |
left_margin = inch |
|
35 |
right_margin = A4[0] - inch |
|
36 |
frame_width = right_margin - left_margin |
|
37 |
||
38 |
||
39 |
def drawPageFrame(canv): |
|
40 |
canv.line(left_margin, top_margin, right_margin, top_margin) |
|
41 |
canv.setFont('Times-Italic',12) |
|
42 |
canv.drawString(left_margin, top_margin + 2, "Homer's Odyssey") |
|
43 |
canv.line(left_margin, top_margin, right_margin, top_margin) |
|
44 |
||
45 |
||
46 |
canv.line(left_margin, bottom_margin, right_margin, bottom_margin) |
|
47 |
canv.drawCentredString(0.5*A4[0], 0.5 * inch, |
|
48 |
"Page %d" % canv.getPageNumber()) |
|
49 |
||
50 |
||
51 |
||
52 |
def run(): |
|
53 |
started = time.time() |
|
54 |
canv = canvas.Canvas('odyssey.pdf') |
|
55 |
canv.setPageCompression(0) |
|
56 |
drawPageFrame(canv) |
|
57 |
||
58 |
#do some title page stuff |
|
59 |
canv.setFont("Times-Bold", 36) |
|
60 |
canv.drawCentredString(0.5 * A4[0], 7 * inch, "Homer's Odyssey") |
|
61 |
||
62 |
canv.setFont("Times-Bold", 18) |
|
63 |
canv.drawCentredString(0.5 * A4[0], 5 * inch, "Translated by Samuel Burton") |
|
64 |
||
65 |
canv.setFont("Times-Bold", 12) |
|
66 |
tx = canv.beginText(left_margin, 3 * inch) |
|
67 |
tx.textLine("This is a demo-cum-benchmark for PDFgen. It renders the complete text of Homer's Odyssey") |
|
68 |
tx.textLine("from a text file. On my humble P266, it does 77 pages per secondwhile creating a 238 page") |
|
69 |
tx.textLine("document. If it is asked to computer text metrics, measuring the width of each word as ") |
|
70 |
tx.textLine("one would for paragraph wrapping, it still manages 22 pages per second.") |
|
71 |
tx.textLine("") |
|
72 |
tx.textLine("Andy Robinson, Robinson Analytics Ltd.") |
|
73 |
canv.drawText(tx) |
|
74 |
||
75 |
canv.showPage() |
|
76 |
#on with the text... |
|
77 |
drawPageFrame(canv) |
|
78 |
||
79 |
canv.setFont('Times-Roman', 12) |
|
80 |
tx = canv.beginText(left_margin, top_margin - 0.5*inch) |
|
81 |
||
251 | 82 |
for fn in ('odyssey.full.txt','odyssey.txt'): |
72 | 83 |
if os.path.isfile(fn): |
84 |
break |
|
85 |
||
86 |
data = open(fn,'r').readlines() |
|
4 | 87 |
for line in data: |
88 |
#this just does it the fast way... |
|
89 |
tx.textLine(line) |
|
90 |
#this forces it to do text metrics, which would be the slow |
|
91 |
#part if we were wrappng paragraphs. |
|
92 |
#canv.textOut(line) |
|
93 |
#canv.textLine('') |
|
94 |
||
95 |
#page breaking |
|
96 |
y = tx.getY() #get y coordinate |
|
97 |
if y < bottom_margin + 0.5*inch: |
|
98 |
canv.drawText(tx) |
|
99 |
canv.showPage() |
|
100 |
drawPageFrame(canv) |
|
101 |
canv.setFont('Times-Roman', 12) |
|
102 |
tx = canv.beginText(left_margin, top_margin - 0.5*inch) |
|
103 |
||
104 |
#page |
|
105 |
pg = canv.getPageNumber() |
|
106 |
if pg % 10 == 0: |
|
107 |
print 'formatted page %d' % canv.getPageNumber() |
|
108 |
||
19 | 109 |
if tx: |
110 |
canv.drawText(tx) |
|
111 |
canv.showPage() |
|
112 |
drawPageFrame(canv) |
|
113 |
||
4 | 114 |
print 'about to write to disk...' |
115 |
||
116 |
canv.save() |
|
117 |
||
118 |
finished = time.time() |
|
119 |
elapsed = finished - started |
|
72 | 120 |
pages = canv.getPageNumber()-1 |
4 | 121 |
speed = pages / elapsed |
122 |
print '%d pages in %0.2f seconds = %0.2f pages per second' % ( |
|
123 |
pages, elapsed, speed) |
|
251 | 124 |
|
4 | 125 |
if __name__=='__main__': |
126 |
run() |