author | rgbecker |
Thu, 01 Jun 2000 09:41:12 +0000 | |
changeset 251 | 6f6cd14069f6 |
parent 72 | 2b88c2668c26 |
child 494 | 54257447cfe9 |
permissions | -rw-r--r-- |
4 | 1 |
############################################################################### |
2 |
# |
|
3 |
# ReportLab Public License Version 1.0 |
|
4 |
# |
|
5 |
# Except for the change of names the spirit and intention of this |
|
6 |
# license is the same as that of Python |
|
7 |
# |
|
8 |
# (C) Copyright ReportLab Inc. 1998-2000. |
|
9 |
# |
|
10 |
# |
|
11 |
# All Rights Reserved |
|
12 |
# |
|
13 |
# Permission to use, copy, modify, and distribute this software and its |
|
14 |
# documentation for any purpose and without fee is hereby granted, provided |
|
15 |
# that the above copyright notice appear in all copies and that both that |
|
16 |
# copyright notice and this permission notice appear in supporting |
|
7 | 17 |
# documentation, and that the name of ReportLab not be used |
4 | 18 |
# in advertising or publicity pertaining to distribution of the software |
19 |
# without specific, written prior permission. |
|
20 |
# |
|
21 |
# |
|
22 |
# Disclaimer |
|
23 |
# |
|
24 |
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
|
25 |
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, |
|
26 |
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT |
|
27 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|
28 |
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
|
29 |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
30 |
# PERFORMANCE OF THIS SOFTWARE. |
|
31 |
# |
|
32 |
############################################################################### |
|
33 |
# $Log: odyssey.py,v $ |
|
251 | 34 |
# Revision 1.8 2000/06/01 09:41:12 rgbecker |
35 |
# test filename case fix |
|
36 |
# |
|
72 | 37 |
# Revision 1.7 2000/04/06 08:51:06 rgbecker |
38 |
# Fix to timing page count, try to read full text |
|
251 | 39 |
# |
42
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
40 |
# Revision 1.6 2000/03/08 13:06:39 andy_robinson |
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
41 |
# Moved inch and cm definitions to reportlab.lib.units and amended all demos |
72 | 42 |
# |
19 | 43 |
# Revision 1.5 2000/02/18 11:00:57 rgbecker |
44 |
# trailing text/Odyssey fix |
|
42
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
45 |
# |
16 | 46 |
# Revision 1.4 2000/02/17 02:05:25 rgbecker |
47 |
# Docstring & other fixes |
|
19 | 48 |
# |
10 | 49 |
# Revision 1.3 2000/02/16 09:42:50 rgbecker |
50 |
# Conversion to reportlab package |
|
16 | 51 |
# |
7 | 52 |
# Revision 1.2 2000/02/15 17:55:59 rgbecker |
53 |
# License text fixes |
|
10 | 54 |
# |
7 | 55 |
# Revision 1.1.1.1 2000/02/15 15:09:29 rgbecker |
56 |
# Initial setup of demos directory and contents. |
|
57 |
# |
|
251 | 58 |
__version__=''' $Id: odyssey.py,v 1.8 2000/06/01 09:41:12 rgbecker Exp $ ''' |
16 | 59 |
___doc__='' |
4 | 60 |
#odyssey.py |
61 |
# |
|
62 |
#Demo/benchmark of PDFgen rendering Homer's Odyssey. |
|
63 |
||
64 |
||
65 |
||
66 |
#results on my humble P266 with 64MB: |
|
67 |
# Without page compression: |
|
68 |
# 239 pages in 3.76 seconds = 77 pages per second |
|
69 |
||
70 |
# With textOut rather than textLine, i.e. computing width |
|
71 |
# of every word as we would for wrapping: |
|
72 |
# 239 pages in 10.83 seconds = 22 pages per second |
|
73 |
||
74 |
# With page compression and textLine(): |
|
75 |
# 239 pages in 39.39 seconds = 6 pages per second |
|
76 |
||
10 | 77 |
from reportlab.pdfgen import canvas |
72 | 78 |
import time, os |
4 | 79 |
|
80 |
||
42
ba8c232f544e
Moved inch and cm definitions to reportlab.lib.units and amended all demos
andy_robinson
parents:
19
diff
changeset
|
81 |
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
|
82 |
from reportlab.lib.pagesizes import A4 |
4 | 83 |
|
84 |
#precalculate some basics |
|
85 |
top_margin = A4[1] - inch |
|
86 |
bottom_margin = inch |
|
87 |
left_margin = inch |
|
88 |
right_margin = A4[0] - inch |
|
89 |
frame_width = right_margin - left_margin |
|
90 |
||
91 |
||
92 |
def drawPageFrame(canv): |
|
93 |
canv.line(left_margin, top_margin, right_margin, top_margin) |
|
94 |
canv.setFont('Times-Italic',12) |
|
95 |
canv.drawString(left_margin, top_margin + 2, "Homer's Odyssey") |
|
96 |
canv.line(left_margin, top_margin, right_margin, top_margin) |
|
97 |
||
98 |
||
99 |
canv.line(left_margin, bottom_margin, right_margin, bottom_margin) |
|
100 |
canv.drawCentredString(0.5*A4[0], 0.5 * inch, |
|
101 |
"Page %d" % canv.getPageNumber()) |
|
102 |
||
103 |
||
104 |
||
105 |
def run(): |
|
106 |
started = time.time() |
|
107 |
canv = canvas.Canvas('odyssey.pdf') |
|
108 |
canv.setPageCompression(0) |
|
109 |
drawPageFrame(canv) |
|
110 |
||
111 |
#do some title page stuff |
|
112 |
canv.setFont("Times-Bold", 36) |
|
113 |
canv.drawCentredString(0.5 * A4[0], 7 * inch, "Homer's Odyssey") |
|
114 |
||
115 |
canv.setFont("Times-Bold", 18) |
|
116 |
canv.drawCentredString(0.5 * A4[0], 5 * inch, "Translated by Samuel Burton") |
|
117 |
||
118 |
canv.setFont("Times-Bold", 12) |
|
119 |
tx = canv.beginText(left_margin, 3 * inch) |
|
120 |
tx.textLine("This is a demo-cum-benchmark for PDFgen. It renders the complete text of Homer's Odyssey") |
|
121 |
tx.textLine("from a text file. On my humble P266, it does 77 pages per secondwhile creating a 238 page") |
|
122 |
tx.textLine("document. If it is asked to computer text metrics, measuring the width of each word as ") |
|
123 |
tx.textLine("one would for paragraph wrapping, it still manages 22 pages per second.") |
|
124 |
tx.textLine("") |
|
125 |
tx.textLine("Andy Robinson, Robinson Analytics Ltd.") |
|
126 |
canv.drawText(tx) |
|
127 |
||
128 |
canv.showPage() |
|
129 |
#on with the text... |
|
130 |
drawPageFrame(canv) |
|
131 |
||
132 |
canv.setFont('Times-Roman', 12) |
|
133 |
tx = canv.beginText(left_margin, top_margin - 0.5*inch) |
|
134 |
||
251 | 135 |
for fn in ('odyssey.full.txt','odyssey.txt'): |
72 | 136 |
if os.path.isfile(fn): |
137 |
break |
|
138 |
||
139 |
data = open(fn,'r').readlines() |
|
4 | 140 |
for line in data: |
141 |
#this just does it the fast way... |
|
142 |
tx.textLine(line) |
|
143 |
#this forces it to do text metrics, which would be the slow |
|
144 |
#part if we were wrappng paragraphs. |
|
145 |
#canv.textOut(line) |
|
146 |
#canv.textLine('') |
|
147 |
||
148 |
#page breaking |
|
149 |
y = tx.getY() #get y coordinate |
|
150 |
if y < bottom_margin + 0.5*inch: |
|
151 |
canv.drawText(tx) |
|
152 |
canv.showPage() |
|
153 |
drawPageFrame(canv) |
|
154 |
canv.setFont('Times-Roman', 12) |
|
155 |
tx = canv.beginText(left_margin, top_margin - 0.5*inch) |
|
156 |
||
157 |
#page |
|
158 |
pg = canv.getPageNumber() |
|
159 |
if pg % 10 == 0: |
|
160 |
print 'formatted page %d' % canv.getPageNumber() |
|
161 |
||
19 | 162 |
if tx: |
163 |
canv.drawText(tx) |
|
164 |
canv.showPage() |
|
165 |
drawPageFrame(canv) |
|
166 |
||
4 | 167 |
print 'about to write to disk...' |
168 |
||
169 |
canv.save() |
|
170 |
||
171 |
finished = time.time() |
|
172 |
elapsed = finished - started |
|
72 | 173 |
pages = canv.getPageNumber()-1 |
4 | 174 |
speed = pages / elapsed |
175 |
print '%d pages in %0.2f seconds = %0.2f pages per second' % ( |
|
176 |
pages, elapsed, speed) |
|
251 | 177 |
|
4 | 178 |
if __name__=='__main__': |
179 |
run() |