reportlab/pdfbase/pdfdoc.py
author gmcm
Thu, 20 Jan 2000 23:33:04 +0000
changeset 0 f19e0a2433ab
child 6 eb791971b252
permissions -rwxr-xr-x
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     1
#pdfdoc.py
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     2
""" 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     3
PDFgen is a library to generate PDF files containing text and graphics.  It is the 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     4
foundation for a complete reporting solution in Python.  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     5
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     6
The module pdfdoc.py handles the 'outer structure' of PDF documents, ensuring that
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     7
all objects are properly cross-referenced and indexed to the nearest byte.  The 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     8
'inner structure' - the page descriptions - are presumed to be generated before 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
each page is saved.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    10
pdfgen.py calls this and provides a 'canvas' object to handle page marking operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    11
piddlePDF calls pdfgen and offers a high-level interface.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    12
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    13
(C) Copyright Andy Robinson 1998-1999
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    14
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    15
2000-10-13 gmcm Packagize
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    16
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    17
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    18
import os
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    19
import sys
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    20
import string
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    21
import time
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    22
import tempfile
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    23
import cStringIO
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    24
from types import *
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    25
from math import sin, cos, pi, ceil
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    26
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    27
try:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    28
    import zlib
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    29
except:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    30
    print "zlib not available, page compression not available"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    31
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    32
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    33
from pdfgen.pdfgeom import bezierArc
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    34
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    35
import pdfutils
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    36
from pdfutils import LINEEND   # this constant needed in both
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    37
import pdfmetrics
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    38
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    39
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    40
#            Constants and declarations
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    41
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    42
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    44
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    45
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    46
StandardEnglishFonts = [
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    47
    'Courier', 'Courier-Bold', 'Courier-Oblique', 'Courier-BoldOblique',  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    48
    'Helvetica', 'Helvetica-Bold', 'Helvetica-Oblique', 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    49
    'Helvetica-BoldOblique',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    50
    'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    51
    'Symbol','ZapfDingbats']
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    52
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    53
PDFError = 'PDFError'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
AFMDIR = '.'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
A4 = (595.27,841.89)   #default page size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
class PDFDocument:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
    """Responsible for linking and writing out the whole document.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
    Builds up a list of objects using add(key, object).  Each of these
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
    must inherit from PDFObject and be able to write itself into the file.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
    For cross-linking, it provides getPosition(key) which tells you where
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
    another object is, or raises a KeyError if not found.  The rule is that
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
    objects should only refer ones previously written to file.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
    """
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
        self.objects = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
        self.objectPositions = {}
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
        self.fonts = MakeType1Fonts()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    71
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    72
        #mapping of Postscriptfont names to internal ones;
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    73
        #needs to be dynamically built once we start adding
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    74
        #fonts in.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    75
        self.fontMapping = {}
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    76
        for i in range(len(StandardEnglishFonts)):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    77
            psname = StandardEnglishFonts[i]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    78
            pdfname = '/F%d' % (i+1)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    79
            self.fontMapping[psname] = pdfname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    80
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    81
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    82
        self.pages = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    83
        self.pagepositions = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    84
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    85
        # position 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    86
        cat = PDFCatalog()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    87
        cat.RefPages = 3
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    88
        cat.RefOutlines = 2
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    89
        self.add('Catalog', cat)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    90
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    91
        # position 2 - outlines
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    92
        outl = PDFOutline()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    93
        self.add('Outline', outl)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    94
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    95
        # position 3 - pages collection
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    96
        self.PageCol = PDFPageCollection()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    97
        self.add('PagesTreeRoot',self.PageCol)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    98
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    99
        # positions 4-16 - fonts
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   100
        fontstartpos = len(self.objects) + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   101
        for font in self.fonts:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   102
            self.add('Font.'+font.keyname, font)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   103
        self.fontdict = MakeFontDictionary(fontstartpos, len(self.fonts))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   104
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   105
        # position 17 - Info
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   106
        self.info = PDFInfo()  #hang onto it!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   107
        self.add('Info', self.info)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   108
        self.infopos = len(self.objects)  #1-based, this gives its position
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   109
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   110
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   111
    def add(self, key, obj):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   112
        self.objectPositions[key] = len(self.objects)  # its position
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
        self.objects.append(obj)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   114
        obj.doc = self
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   115
        return len(self.objects) - 1  # give its position
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   116
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   117
    def getPosition(self, key):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   118
        """Tell you where the given object is in the file - used for
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   119
        cross-linking; an object can call self.doc.getPosition("Page001")
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   120
        to find out where the object keyed under "Page001" is stored."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   121
        return self.objectPositions[key]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   122
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   123
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   124
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   125
        self.info.title = title
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   126
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   127
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   128
        "embedded in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   129
        self.info.author = author
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   130
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   131
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   132
        "embeds in PDF file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   133
        self.info.subject = subject
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   134
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   135
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   136
    def printXref(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   137
        self.startxref = sys.stdout.tell()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   138
        print 'xref'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   139
        print 0,len(self.objects) + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   140
        print '0000000000 65535 f'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   141
        for pos in self.xref:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   142
            print '%0.10d 00000 n' % pos
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   143
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   144
    def writeXref(self, f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   145
        self.startxref = f.tell()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   146
        f.write('xref' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   147
        f.write('0 %d' % (len(self.objects) + 1) + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   148
        f.write('0000000000 65535 f' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   149
        for pos in self.xref:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   150
            f.write('%0.10d 00000 n' % pos + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   151
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   152
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   153
    def printTrailer(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   154
        print 'trailer'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   155
        print '<< /Size %d /Root %d 0 R /Info %d 0 R>>' % (len(self.objects) + 1, 1, self.infopos)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   156
        print 'startxref'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   157
        print self.startxref
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   158
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   159
    def writeTrailer(self, f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   160
        f.write('trailer' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   161
        f.write('<< /Size %d /Root %d 0 R /Info %d 0 R>>' % (len(self.objects) + 1, 1, self.infopos)  + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   162
        f.write('startxref' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   163
        f.write(str(self.startxref)  + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   164
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   165
    def SaveToFile(self, filename):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   166
        """Open a file, and ask each object in turn to write itself to
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   167
        the file.  Keep track of the file position at each point for
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   168
        use in the index at the end"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   169
        f = open(filename, 'wb')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   170
        i = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
        self.xref = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   172
        f.write("%PDF-1.2" + LINEEND)  # for CID support
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
        f.write("%춾" + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   174
        for obj in self.objects:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
            pos = f.tell()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   176
            self.xref.append(pos)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   177
            f.write(str(i) + ' 0 obj' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   178
            obj.save(f)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   179
            f.write('endobj' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   180
            i = i + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   181
        self.writeXref(f)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   182
        self.writeTrailer(f)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   183
        f.write('%%EOF')  # no lineend needed on this one!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   184
        f.close()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   185
        # with the Mac, we need to tag the file in a special
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   186
        #way so the system knows it is a PDF file.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   187
        #This supplied by Joe Strout
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   188
        if os.name == 'mac':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   189
            import macfs
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   190
            try: 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   191
                macfs.FSSpec(filename).SetCreatorType('CARO','PDF ')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   192
            except:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   193
                pass
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   194
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   195
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   196
    def printPDF(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   197
        "prints it to standard output.  Logs positions for doing trailer"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   198
        print "%PDF-1.0"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   199
        print "%춾"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   200
        i = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   201
        self.xref = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
        for obj in self.objects:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   203
            pos = sys.stdout.tell()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   204
            self.xref.append(pos)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   205
            print i, '0 obj'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   206
            obj.printPDF()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   207
            print 'endobj'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   208
            i = i + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   209
        self.printXref()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   210
        self.printTrailer()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   211
        print "%%EOF",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   212
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   213
    def addPage(self, page):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   214
        """adds page and stream at end.  Maintains pages list"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   215
        #page.buildstream()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   216
        pos = len(self.objects) # work out where added
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   217
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   218
        page.ParentPos = 3   #pages collection
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   219
        page.info = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   220
            'parentpos':3,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   221
            'fontdict':self.fontdict,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   222
            'contentspos':pos + 2,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   223
            }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   224
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   225
        self.PageCol.PageList.append(pos+1)  
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   226
        self.add('Page%06d'% len(self.PageCol.PageList), page)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   227
        #self.objects.append(page)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   228
        self.add('PageStream%06d'% len(self.PageCol.PageList), page.stream)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   229
        #self.objects.append(page.stream)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   230
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   231
    def hasFont(self, psfontname):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   232
        return self.fontMapping.has_key(psfontname)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   233
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   234
    def getInternalFontName(self, psfontname):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   235
        try:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   236
            return self.fontMapping[psfontname]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   237
        except:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   238
            raise PDFError, "Font %s not available in document" % psfontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   239
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   240
    def getAvailableFonts(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   241
        fontnames = self.fontMapping.keys()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   242
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   243
        return fontnames
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   244
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   245
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   247
#            Utilities
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   248
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   249
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   250
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   251
class OutputGrabber:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   252
    """At times we need to put something in the place of standard
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   253
    output.  This grabs stdout, keeps the data, and releases stdout
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   254
    when done.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   255
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   256
    NOT working well enough!"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   257
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   258
        self.oldoutput = sys.stdout
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   259
        sys.stdout = self
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   260
        self.closed = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   261
        self.data = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
    def write(self, x):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   263
        if not self.closed:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
            self.data.append(x)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
    def getData(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   267
        return string.join(self.data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   268
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   269
    def close(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   270
        sys.stdout = self.oldoutput
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   271
        self.closed = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
    def __del__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
        if not self.closed:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
            self.close()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
def testOutputGrabber():
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
    gr = OutputGrabber()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   280
    for i in range(10):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   281
        print 'line',i
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   282
    data = gr.getData()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   283
    gr.close()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   284
    print 'Data...',data
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   285
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   286
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   287
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   288
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   289
#            PDF Object Hierarchy
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   290
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   291
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   292
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   293
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   294
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   295
class PDFObject:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   296
    """Base class for all PDF objects.  In PDF, precise measurement
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   297
    of file offsets is essential, so the usual trick of just printing
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   298
    and redirecting output has proved to give different behaviour on
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   299
    Mac and Windows.  While it might be soluble, I'm taking charge
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   300
    of line ends at the binary level and explicitly writing to a file.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   301
    The LINEEND constant lets me try CR, LF and CRLF easily to help
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   302
    pin down the problem."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   303
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
        "Save its content to an open file"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   305
        file.write('% base PDF object' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   306
    def printPDF(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   307
        self.save(sys.stdout)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   308
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   309
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   310
class PDFLiteral(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
    " a ready-made one you wish to quote"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
    def __init__(self, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
        self.text = text
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   315
        file.write(self.text + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   316
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   317
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   318
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   319
class PDFCatalog(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   320
    "requires RefPages and RefOutlines set"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   321
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   322
        self.template = string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   323
                        '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   324
                        '/Type /Catalog',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   325
                        '/Pages %d 0 R',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   326
                        '/Outlines %d 0 R',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   327
                        '>>'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   328
                        ],LINEEND
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   329
                        )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   330
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   331
        file.write(self.template % (self.RefPages, self.RefOutlines) + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   332
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   333
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   334
class PDFInfo(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   335
    """PDF documents can have basic information embedded, viewable from
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   336
    File | Document Info in Acrobat Reader.  If this is wrong, you get
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   337
    Postscript errors while printing, even though it does not print."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   338
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   339
        self.title = "untitled"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   340
        self.author = "anonymous"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   341
        self.subject = "unspecified"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   342
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   343
        now = time.localtime(time.time())
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   344
        self.datestr = '%04d%02d%02d%02d%02d%02d' % tuple(now[0:6])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   345
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   346
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   347
        file.write(string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   348
                "<</Title (%s)",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   349
                "/Author (%s)",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   350
                "/CreationDate (D:%s)",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   351
                "/Producer (PDFgen)",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   352
                "/Subject (%s)",
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   353
                ">>"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   354
                ], LINEEND
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   355
            ) % ( 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   356
    pdfutils._escape(self.title), 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   357
    pdfutils._escape(self.author), 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   358
    self.datestr, 
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   359
    pdfutils._escape(self.subject)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   360
    ) + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   361
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   362
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   363
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   364
class PDFOutline(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   365
    "null outline, does nothing yet"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   366
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   367
        self.template = string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   368
                '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   369
                '/Type /Outlines',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   370
                '/Count 0',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   371
                '>>'],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   372
                LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   373
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   374
        file.write(self.template + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   375
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   376
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   377
class PDFPageCollection(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   378
    "presumes PageList attribute set (list of integers)"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   379
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   380
        self.PageList = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   381
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   382
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   383
        lines = [ '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   384
                '/Type /Pages',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   385
                '/Count %d' % len(self.PageList),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   386
                '/Kids ['
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   387
                ]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   388
        for page in self.PageList:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   389
            lines.append(str(page) + ' 0 R ')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   390
        lines.append(']')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   391
        lines.append('>>')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   392
        text = string.join(lines, LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   393
        file.write(text + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   394
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   395
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   396
class PDFPage(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   397
    """The Bastard.  Needs list of Resources etc. Use a standard one for now.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   398
    It manages a PDFStream object which must be added to the document's list
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   399
    of objects as well."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   400
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   401
        self.drawables = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   402
        self.pagewidth = 595  #these are overridden by piddlePDF
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   403
        self.pageheight = 842
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   404
        self.stream = PDFStream()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   405
        self.hasImages = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   406
        self.pageTransitionString = ''  # presentation effects
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   407
        # editors on different systems may put different things in the line end
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   408
        # without me noticing.  No triple-quoted strings allowed!
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   409
        self.template = string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   410
                '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   411
                '/Type /Page',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   412
                '/Parent %(parentpos)d 0 R',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   413
                '/Resources',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   414
                '   <<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   415
                '   /Font %(fontdict)s',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   416
                '   /ProcSet %(procsettext)s',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   417
                '   >>',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   418
                '/MediaBox [0 0 %(pagewidth)d %(pageheight)d]',  #A4 by default
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   419
                '/Contents %(contentspos)d 0 R',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   420
                '%(transitionString)s',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   421
                '>>'],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   422
            LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   423
    def setCompression(self, onoff=0):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   424
        "Turns page compression on or off"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   425
        assert onoff in [0,1], "Page compression options are 1=on, 2=off"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   426
        self.stream.compression = onoff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   427
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   428
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   429
        self.info['pagewidth'] = self.pagewidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   430
        self.info['pageheight'] = self.pageheight
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   431
        # check for image support
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   432
        if self.hasImages:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   433
            self.info['procsettext'] = '[/PDF /Text /ImageC]'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   434
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   435
            self.info['procsettext'] = '[/PDF /Text]'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   436
        self.info['transitionString'] = self.pageTransitionString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   437
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   438
        file.write(self.template % self.info + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   439
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   440
    def clear(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   441
        self.drawables = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   442
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   443
    def setStream(self, data):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   444
        if type(data) is ListType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   445
            data = string.join(data, LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   446
        self.stream.setStream(data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   447
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   448
TestStream = "BT /F6 24 Tf 80 672 Td 24 TL (   ) Tj T* ET"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   449
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   450
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   451
class PDFStream(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   452
    "Used for the contents of a page"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   453
    def __init__(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   454
        self.data = None
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   455
        self.compression = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   456
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   457
    def setStream(self, data):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   458
        self.data = data
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   459
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   460
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   461
        #avoid crashes if they wrote nothing in the page
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   462
        if self.data == None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   463
             self.data = TestStream
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   464
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   465
        if self.compression == 1:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   466
            comp = zlib.compress(self.data)   #this bit is very fast...
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   467
            base85 = pdfutils._AsciiBase85Encode(comp) #...sadly this isn't
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   468
            wrapped = pdfutils._wrap(base85)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   469
            data_to_write = wrapped
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   470
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   471
            data_to_write = self.data
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   472
        # the PDF length key should contain the length including
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   473
        # any extra LF pairs added by Print on DOS.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   474
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   475
        #lines = len(string.split(self.data,'\n'))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   476
        #length = len(self.data) + lines   # one extra LF each
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   477
        length = len(data_to_write) + len(LINEEND)    #AR 19980202
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   478
        if self.compression:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   479
            file.write('<< /Length %d /Filter [/ASCII85Decode /FlateDecode]>>' % length + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   480
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   481
            file.write('<< /Length %d >>' % length + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   482
        file.write('stream' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   483
        file.write(data_to_write + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   484
        file.write('endstream' + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   485
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   486
class PDFImage(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   487
    # sample one while developing.  Currently, images go in a literals
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   488
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   489
        file.write(string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   490
                '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   491
                '/Type /XObject',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   492
                '/Subtype /Image',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   493
                '/Name /Im0',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   494
                '/Width 24',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   495
                '/Height 23',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   496
                '/BitsPerComponent 1',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   497
                '/ColorSpace /DeviceGray',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   498
                '/Filter /ASCIIHexDecode',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   499
                '/Length 174',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   500
                '>>',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   501
                'stream',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   502
                '003B00 002700 002480 0E4940 114920 14B220 3CB650',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   503
                '75FE88 17FF8C 175F14 1C07E2 3803C4 703182 F8EDFC',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   504
                'B2BBC2 BB6F84 31BFC2 18EA3C 0E3E00 07FC00 03F800',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   505
                '1E1800 1FF800>',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   506
                'endstream',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   507
                'endobj'
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   508
                ], LINEEND) + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   509
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   510
class PDFType1Font(PDFObject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   511
    def __init__(self, key, font):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   512
        self.fontname = font
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   513
        self.keyname = key
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   514
        self.template = string.join([
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   515
                    '<<',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   516
                    '/Type /Font',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   517
                    '/Subtype /Type1',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   518
                    '/Name /%s',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   519
                    '/BaseFont /%s',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   520
                    '/Encoding /MacRomanEncoding',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   521
                    '>>'],
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   522
                    LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   523
    def save(self, file):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   524
        file.write(self.template % (self.keyname, self.fontname) + LINEEND)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   525
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   526
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   527
       
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   528
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   529
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   530
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   531
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   532
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   533
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   534
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   535
#            some helpers
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   536
#
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   537
##############################################################
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   538
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   539
def MakeType1Fonts():
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   540
    "returns a list of all the standard font objects"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   541
    fonts = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   542
    pos = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   543
    for fontname in StandardEnglishFonts:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   544
        font = PDFType1Font('F'+str(pos), fontname)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   545
        fonts.append(font)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   546
        pos = pos + 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   547
    return fonts
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   548
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   549
def MakeFontDictionary(startpos, count):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   550
    "returns a font dictionary assuming they are all in the file from startpos"    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   551
    dict = "  <<" + LINEEND
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   552
    pos = startpos
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   553
    for i in range(count):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   554
        dict = dict + '\t\t/F%d %d 0 R ' % (i + 1, startpos + i) + LINEEND
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   555
    dict = dict + "\t\t>>" + LINEEND
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   556
    return dict
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   557
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   558
if __name__ == '__main__':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   559
    print 'For test scripts, run test1.py to test6.py'