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