author  robin 
Tue, 19 Nov 2013 13:50:34 +0000  
branch  py33 
changeset 3794  398ea04239b5 
parent 3781  df8b57380768 
child 3801  0c12fc9ce323 
permissions  rwxrxrx 
3617  1 
#Copyright ReportLab Europe Ltd. 20002012 
494  2 
#see license.txt for license details 
2332  3 
#history http://www.reportlab.co.uk/cgibin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfbase/pdfdoc.py 
4 
__version__=''' $Id$ ''' 

1577  5 
__doc__=""" 
0  6 
The module pdfdoc.py handles the 'outer structure' of PDF documents, ensuring that 
1577  7 
all objects are properly crossreferenced and indexed to the nearest byte. The 
8 
'inner structure'  the page descriptions  are presumed to be generated before 

0  9 
each page is saved. 
10 
pdfgen.py calls this and provides a 'canvas' object to handle page marking operators. 

11 
piddlePDF calls pdfgen and offers a highlevel interface. 

12 

873  13 
The classes within this generally mirror structures in the PDF file 
14 
and are not part of any public interface. Instead, canvas and font 

15 
classes are made available elsewhere for users to manipulate. 

0  16 
""" 
3794  17 
import types, binascii, codecs 
873  18 
from reportlab.pdfbase import pdfutils 
2067  19 
from reportlab.pdfbase.pdfutils import LINEEND # this constant needed in both 
873  20 
from reportlab import rl_config 
3781  21 
from reportlab.lib.utils import import_zlib, open_for_read, makeFileName, isSeq, isBytes, isUnicode, _digester 
22 
from reportlab.lib.rl_accel import escapePDF, fp_str, asciiBase85Encode, asciiBase85Decode 

2595  23 
from reportlab.pdfbase import pdfmetrics 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

24 
from hashlib import md5 
873  25 

1653
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

26 
from sys import platform 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

27 
from sys import version_info 
1653
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

28 

1622  29 
if platform[:4] == 'java' and version_info[:2] == (2, 1): 
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1653
diff
changeset

30 
# workaround for list()bug in Jython 2.1 (should be fixed in 2.2) 
1622  31 
def list(sequence): 
32 
def f(x): 

33 
return x 

3721  34 
return list(map(f, sequence)) 
1622  35 

2737  36 
def utf8str(x): 
3721  37 
if isinstance(x,str): 
2737  38 
return x.encode('utf8') 
39 
else: 

40 
return str(x) 

41 

873  42 
class PDFError(Exception): 
43 
pass 

44 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

45 
# set this flag to get more vertical whitespace (and larger files) 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1317
diff
changeset

46 
LongFormat = 1 
1062  47 
##if LongFormat: (doesn't work) 
48 
## pass 

49 
##else: 

50 
## LINEEND = "\n" # no wasteful carriage returns! 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

51 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

52 
# __InternalName__ is a special attribute that can only be set by the Document arbitrator 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

53 
__InternalName__ = "__InternalName__" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

54 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

55 
# __RefOnly__ marks reference only elements that must be formatted on top level 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

56 
__RefOnly__ = "__RefOnly__" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

57 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

58 
# __Comment__ provides a (one line) comment to inline with an object ref, if present 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

59 
# if it is more than one line then percentize it... 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

60 
__Comment__ = "__Comment__" 
1062  61 

62 
# If DoComments is set then add helpful (space wasting) comment lines to PDF files 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

63 
DoComments = 1 
1062  64 
if not LongFormat: 
65 
DoComments = 0 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

66 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

67 
# name for standard font dictionary 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

68 
BasicFonts = "BasicFonts" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

69 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

70 
# name for the pages object 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

71 
Pages = "Pages" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

72 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

73 
### generic utilities 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

74 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

75 
# for % substitutions 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

76 
LINEENDDICT = {"LINEEND": LINEEND, "PERCENT": "%"} 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

77 
PDF_VERSION_DEFAULT = (1, 3) 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

78 
PDF_SUPPORT_VERSION = dict( #map keyword to min version that supports it 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

79 
transparency = (1, 4), 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

80 
) 
0  81 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

82 
def format(element, document, toplevel=0): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

83 
"""Indirection step for formatting. 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

84 
Ensures that document parameters alter behaviour 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

85 
of formatting for all elements. 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

86 
""" 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

87 
if hasattr(element,'__PDFObject__'): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

88 
if not toplevel and hasattr(element, __RefOnly__): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

89 
# the object cannot be a component at non top level. 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

90 
# make a reference to it and return it's format 
2540  91 
return document.Reference(element).format(document) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

92 
else: 
2541  93 
f = element.format(document) 
2273
82f026361b70
Fix bug introduced during transparency/ImageReader changes
rgbecker
parents:
2234
diff
changeset

94 
if not rl_config.invariant and DoComments and hasattr(element, __Comment__): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

95 
f = "%s%s%s%s" % ("% ", element.__Comment__, LINEEND, f) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

96 
return f 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

97 
elif type(element) in (float, int): 
2067  98 
#use a controlled number formatting routine 
99 
#instead of str, so Jython/Python etc do not differ 

100 
return fp_str(element) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

101 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

102 
return str(element) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

103 

1552  104 
def xObjectName(externalname): 
1560
382bbd1f20ae
revert to older internal name for backward compatibility
rgbecker
parents:
1554
diff
changeset

105 
return "FormXob.%s" % externalname 
1552  106 

1605  107 
# backwards compatibility 
108 
formName = xObjectName 

109 

1062  110 
# no encryption 
111 
class NoEncryption: 

112 
def encode(self, t): 

113 
"encode a string, stream, text" 

114 
return t 

115 
def prepare(self, document): 

116 
# get ready to do encryption 

117 
pass 

118 
def register(self, objnum, version): 

119 
# enter a new direct object 

120 
pass 

121 
def info(self): 

122 
# the representation of self in file if any (should be None or PDFDict) 

123 
return None 

124 

125 
class DummyDoc: 

126 
"used to bypass encryption when required" 

3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

127 
__PDFObject__ = True 
1062  128 
encrypt = NoEncryption() 
1577  129 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

130 
### the global document structure manager 
0  131 
class PDFDocument: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

132 
__PDFObject__ = True 
1577  133 
# set this to define filters 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

134 
defaultStreamFilters = None 
1062  135 
encrypt = NoEncryption() # default no encryption 
1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

136 
def __init__(self, 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

137 
dummyoutline=0, 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

138 
compression=rl_config.pageCompression, 
2476
19fb0900fba1
pdfdoc.py: minor changes to simplify PDFInfoL/U in other apps
rgbecker
parents:
2378
diff
changeset

139 
invariant=rl_config.invariant, 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

140 
filename=None, 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

141 
pdfVersion=PDF_VERSION_DEFAULT, 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

142 
): 
3570
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

143 
self._ID = None 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

144 
self.objectcounter = 0 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

145 
self.shadingCounter = 0 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

146 
self.inObject = None 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

147 
self.pageCounter = 1 
1577  148 

1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

149 
# allow None value to be passed in to mean 'give system defaults' 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

150 
if invariant is None: 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

151 
self.invariant = rl_config.invariant 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

152 
else: 
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

153 
self.invariant = invariant 
1635  154 
self.setCompression(compression) 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

155 
self._pdfVersion = pdfVersion 
1062  156 
# signature for creating PDF ID 
2993  157 
sig = self.signature = md5() 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

158 
sig.update(b"a reportlab document") 
1977  159 
if not self.invariant: 
1987
b2401bcddd6d
Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents:
1985
diff
changeset

160 
cat = _getTimeStamp() 
b2401bcddd6d
Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents:
1985
diff
changeset

161 
else: 
b2401bcddd6d
Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents:
1985
diff
changeset

162 
cat = 946684800.0 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

163 
cat = repr(cat) 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

164 
import sys 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

165 
if sys.version_info[0] == 3: 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

166 
cat = bytes(repr(cat), 'utf8') 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

167 
sig.update(cat) # initialize with timestamp digest 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

168 
# mapping of internal identifier ("Page001") to PDF objectnumber and generation number (34, 0) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

169 
self.idToObjectNumberAndVersion = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

170 
# mapping of internal identifier ("Page001") to PDF object (PDFPage instance) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

171 
self.idToObject = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

172 
# internal id to file location 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

173 
self.idToOffset = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

174 
# number to id 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

175 
self.numberToId = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

176 
cat = self.Catalog = self._catalog = PDFCatalog() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

177 
pages = self.Pages = PDFPages() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

178 
cat.Pages = pages 
483
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

179 
if dummyoutline: 
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

180 
outlines = PDFOutlines0() 
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

181 
else: 
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

182 
outlines = PDFOutlines() 
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

183 
self.Outlines = self.outline = outlines 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

184 
cat.Outlines = outlines 
603  185 
self.info = PDFInfo() 
1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

186 
self.info.invariant = self.invariant 
591  187 
#self.Reference(self.Catalog) 
188 
#self.Reference(self.Info) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

189 
self.fontMapping = {} 
873  190 
#make an empty font dictionary 
191 
DD = PDFDictionary({}) 

192 
DD.__Comment__ = "The standard fonts dictionary" 

2944  193 
self.Reference(DD, BasicFonts) 
1635  194 
self.delayedFonts = [] 
195 

196 
def setCompression(self, onoff): 

197 
# XXX: maybe this should also set self.defaultStreamFilters? 

198 
self.compression = onoff 

873  199 

3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

200 
def ensureMinPdfVersion(self, *keys): 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

201 
"Ensure that the pdf version is greater than or equal to that specified by the keys" 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

202 
for k in keys: 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

203 
self._pdfVersion = max(self._pdfVersion, PDF_SUPPORT_VERSION[k]) 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

204 

1062  205 
def updateSignature(self, thing): 
206 
"add information to the signature" 

207 
if self._ID: return # but not if its used already! 

2737  208 
self.signature.update(utf8str(thing)) 
1577  209 

1062  210 
def ID(self): 
1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

211 
"A unique fingerprint for the file (unless in invariant mode)" 
1062  212 
if self._ID: 
213 
return self._ID 

1985  214 
digest = self.signature.digest() 
215 
doc = DummyDoc() 

2595  216 
ID = PDFString(digest,enc='raw') 
1985  217 
IDs = ID.format(doc) 
218 
self._ID = "%s %% ReportLab generated PDF document  digest (http://www.reportlab.com) %s [%s %s] %s" % ( 

1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

219 
LINEEND, LINEEND, IDs, IDs, LINEEND) 
1062  220 
return self._ID 
0  221 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

222 
def SaveToFile(self, filename, canvas): 
3326  223 
if hasattr(getattr(filename, "write",None),'__call__'): 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

224 
myfile = 0 
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

225 
f = filename 
3687  226 
filename = makeFileName(getattr(filename,'name','')) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

227 
else : 
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

228 
myfile = 1 
3687  229 
filename = makeFileName(filename) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

230 
f = open(filename, "wb") 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

231 
data = self.GetPDFData(canvas) 
3731  232 
if isUnicode(data): 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

233 
data = data.encode('utf8') 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

234 
f.write(data) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

235 
if myfile: 
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

236 
f.close() 
2010  237 
import os 
2009  238 
if os.name=='mac': 
239 
from reportlab.lib.utils import markfilename 

240 
markfilename(filename) # do platform specific file junk 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

241 
if getattr(canvas,'_verbosity',None): print('saved %s' % (filename,)) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

242 

02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

243 
def GetPDFData(self, canvas): 
1635  244 
# realize delayed fonts 
245 
for fnt in self.delayedFonts: 

246 
fnt.addObjects(self) 

1062  247 
# add info stuff to signature 
1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

248 
self.info.invariant = self.invariant 
1062  249 
self.info.digest(self.signature) 
250 
### later: maybe add more info to sig? 

483
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

251 
# prepare outline 
591  252 
self.Reference(self.Catalog) 
603  253 
self.Reference(self.info) 
483
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

254 
outline = self.outline 
c4c3969d1e30
undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents:
482
diff
changeset

255 
outline.prepare(self, canvas) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

256 
return self.format() 
1577  257 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

258 
def inPage(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

259 
"""specify the current object as a page (enables reference binding and other page features)""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

260 
if self.inObject is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

261 
if self.inObject=="page": return 
3721  262 
raise ValueError("can't go in page already in object %s" % self.inObject) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

263 
self.inObject = "page" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

264 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

265 
def inForm(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

266 
"""specify that we are in a form xobject (disable page features, etc)""" 
742  267 
# don't need this check anymore since going in a form pushes old context at canvas level. 
268 
#if self.inObject not in ["form", None]: 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

269 
# raise ValueError("can't go in form already in object %s" % self.inObject) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

270 
self.inObject = "form" 
1577  271 
# don't need to do anything else, I think... 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

272 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

273 
def getInternalFontName(self, psfontname): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

274 
fm = self.fontMapping 
3326  275 
if psfontname in fm: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

276 
return fm[psfontname] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

277 
else: 
873  278 
try: 
279 
# does pdfmetrics know about it? if so, add 

280 
fontObj = pdfmetrics.getFont(psfontname) 

2678
38d18a697cd0
reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents:
2666
diff
changeset

281 
if fontObj._dynamicFont: 
2915  282 
raise PDFError("getInternalFontName(%s) called for a dynamic font" % repr(psfontname)) 
1577  283 
fontObj.addObjects(self) 
873  284 
#self.addFont(fontObj) 
285 
return fm[psfontname] 

286 
except KeyError: 

2915  287 
raise PDFError("Font %s not known!" % repr(psfontname)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

288 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

289 
def thisPageName(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

290 
return "Page"+repr(self.pageCounter) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

291 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

292 
def thisPageRef(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

293 
return PDFObjectReference(self.thisPageName()) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

294 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

295 
def addPage(self, page): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

296 
name = self.thisPageName() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

297 
self.Reference(page, name) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

298 
self.Pages.addPage(page) 
2919  299 
self.pageCounter += 1 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

300 
self.inObject = None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

301 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

302 
def addForm(self, name, form): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

303 
"""add a Form XObject.""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

304 
# XXX should check that name is a legal PDF name 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

305 
if self.inObject != "form": 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

306 
self.inForm() 
1552  307 
self.Reference(form, xObjectName(name)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

308 
self.inObject = None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

309 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

310 
def annotationName(self, externalname): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

311 
return "Annot.%s"%externalname 
1577  312 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

313 
def addAnnotation(self, name, annotation): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

314 
self.Reference(annotation, self.annotationName(name)) 
1577  315 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

316 
def refAnnotation(self, name): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

317 
internalname = self.annotationName(name) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

318 
return PDFObjectReference(internalname) 
1577  319 

3570
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

320 
def addShading(self, shading): 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

321 
name = "Sh%d" % self.shadingCounter 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

322 
self.Reference(shading, name) 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

323 
self.shadingCounter += 1 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

324 
return name 
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

325 

3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

326 
def addColor(self,cmyk): 
3226
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

327 
sname = cmyk.spotName 
3588
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

328 
if not sname: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

329 
if cmyk.cyan==0 and cmyk.magenta==0 and cmyk.yellow==0: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

330 
sname = 'BLACK' 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

331 
elif cmyk.black==0 and cmyk.magenta==0 and cmyk.yellow==0: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

332 
sname = 'CYAN' 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

333 
elif cmyk.cyan==0 and cmyk.black==0 and cmyk.yellow==0: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

334 
sname = 'MAGENTA' 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

335 
elif cmyk.cyan==0 and cmyk.magenta==0 and cmyk.black==0: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

336 
sname = 'YELLOW' 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

337 
if not sname: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

338 
raise ValueError("CMYK colour %r used without a spotName" % cmyk) 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

339 
else: 
aed7c74d52a2
pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents:
3570
diff
changeset

340 
cmyk = cmyk.clone(spotName = sname) 
3226
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

341 
name = PDFName(sname)[1:] 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

342 
if name not in self.idToObject: 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

343 
sep = PDFSeparationCMYKColor(cmyk).value() #PDFArray([/Separation /name /DeviceCMYK tint_tf]) 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

344 
self.Reference(sep,name) 
3226
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

345 
return name,sname 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

346 

0  347 
def setTitle(self, title): 
348 
"embeds in PDF file" 

2666  349 
if title is None: 
350 
self.info.title = '(anonymous)' 

351 
else: 

352 
self.info.title = title 

353 

0  354 
def setAuthor(self, author): 
355 
"embedded in PDF file" 

2666  356 
#allow resetting to clear it 
357 
if author is None: 

358 
self.info.author = '(anonymous)' 

359 
else: 

360 
self.info.author = author 

1577  361 

0  362 
def setSubject(self, subject): 
363 
"embeds in PDF file" 

2666  364 

365 
#allow resetting to clear it 

366 
if subject is None: 

367 
self.info.subject = '(unspecified)' 

368 
else: 

369 
self.info.subject = subject 

370 

3456  371 
def setCreator(self, creator): 
372 
"embeds in PDF file" 

373 

374 
#allow resetting to clear it 

375 
if creator is None: 

376 
self.info.creator = '(unspecified)' 

377 
else: 

378 
self.info.creator = creator 

379 

2666  380 
def setKeywords(self, keywords): 
381 
"embeds a string containing keywords in PDF file" 

382 

383 
#allow resetting to clear it but ensure it's a string 

384 
if keywords is None: 

385 
self.info.keywords = '' 

386 
else: 

387 
self.info.keywords = keywords 

388 

2595  389 
def setDateFormatter(self, dateFormatter): 
390 
self.info._dateFormatter = dateFormatter 

391 

0  392 
def getAvailableFonts(self): 
3721  393 
fontnames = list(self.fontMapping.keys()) 
1151
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

394 
# the standard 14 are also always available! (even if not initialized yet) 
3721  395 
from . import _fontdata 
1151
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

396 
for name in _fontdata.standardFonts: 
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

397 
if name not in fontnames: 
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

398 
fontnames.append(name) 
0  399 
fontnames.sort() 
400 
return fontnames 

681  401 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

402 
def format(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

403 
# register the Catalog/INfo and then format the objects one by one until exhausted 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

404 
# (possible infinite loop if there is a bug that continually makes new objects/refs...) 
1062  405 
# Prepare encryption 
406 
self.encrypt.prepare(self) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

407 
cat = self.Catalog 
603  408 
info = self.info 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

409 
self.Reference(self.Catalog) 
603  410 
self.Reference(self.info) 
1062  411 
# register the encryption dictionary if present 
412 
encryptref = None 

413 
encryptinfo = self.encrypt.info() 

414 
if encryptinfo: 

415 
encryptref = self.Reference(encryptinfo) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

416 
# make std fonts (this could be made optional 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

417 
counter = 0 # start at first object (object 1 after preincrement) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

418 
ids = [] # the collection of object ids in object number order 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

419 
numbertoid = self.numberToId 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

420 
idToNV = self.idToObjectNumberAndVersion 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

421 
idToOb = self.idToObject 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

422 
idToOf = self.idToOffset 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

423 
### note that new entries may be "appended" DURING FORMATTING 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

424 
done = None 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

425 
File = PDFFile(self._pdfVersion) # output collector 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

426 
while done is None: 
2575  427 
counter += 1 # do next object... 
3326  428 
if counter in numbertoid: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

429 
id = numbertoid[counter] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

430 
#printidToOb 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

431 
obj = idToOb[id] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

432 
IO = PDFIndirectObject(id, obj) 
1062  433 
# register object number and version 
1577  434 
#encrypt.register(id, 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

435 
IOf = IO.format(self) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

436 
# add a comment to the PDF output 
2273
82f026361b70
Fix bug introduced during transparency/ImageReader changes
rgbecker
parents:
2234
diff
changeset

437 
if not rl_config.invariant and DoComments: 
1840
9c20e252dcd2
Fixed how object comments are formatted  could not always __class___.__name__
andy_robinson
parents:
1838
diff
changeset

438 
try: 
9c20e252dcd2
Fixed how object comments are formatted  could not always __class___.__name__
andy_robinson
parents:
1838
diff
changeset

439 
classname = obj.__class__.__name__ 
9c20e252dcd2
Fixed how object comments are formatted  could not always __class___.__name__
andy_robinson
parents:
1838
diff
changeset

440 
except: 
9c20e252dcd2
Fixed how object comments are formatted  could not always __class___.__name__
andy_robinson
parents:
1838
diff
changeset

441 
classname = repr(obj) 
9c20e252dcd2
Fixed how object comments are formatted  could not always __class___.__name__
andy_robinson
parents:
1838
diff
changeset

442 
File.add("%% %s: class %s %s" % (repr(id), classname[:50], LINEEND)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

443 
offset = File.add(IOf) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

444 
idToOf[id] = offset 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

445 
ids.append(id) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

446 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

447 
done = 1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

448 
# sanity checks (must happen AFTER formatting) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

449 
lno = len(numbertoid) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

450 
if counter1!=lno: 
3721  451 
raise ValueError("counter %s doesn't match number to id dictionary %s" %(counter, lno)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

452 
# now add the xref 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

453 
xref = PDFCrossReferenceTable() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

454 
xref.addsection(0, ids) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

455 
xreff = xref.format(self) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

456 
xrefoffset = File.add(xreff) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

457 
# now add the trailer 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

458 
trailer = PDFTrailer( 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

459 
startxref = xrefoffset, 
1309  460 
Size = lno+1, 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

461 
Root = self.Reference(cat), 
1062  462 
Info = self.Reference(info), 
463 
Encrypt = encryptref, 

464 
ID = self.ID(), 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

465 
) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

466 
trailerf = trailer.format(self) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

467 
File.add(trailerf) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

468 
# return string format for pdf file 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

469 
return File.format(self) 
1577  470 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

471 
def hasForm(self, name): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

472 
"""test for existence of named form""" 
1552  473 
internalname = xObjectName(name) 
3326  474 
return internalname in self.idToObject 
0  475 

2679  476 
def getFormBBox(self, name, boxType="MediaBox"): 
477 
"""get the declared bounding box of the form as a list. 

478 
If you specify a different PDF box definition (e.g. the 

479 
ArtBox) and it has one, that's what you'll get.""" 

1552  480 
internalname = xObjectName(name) 
3326  481 
if internalname in self.idToObject: 
1430  482 
theform = self.idToObject[internalname] 
2798  483 
if hasattr(theform,'_extra_pageCatcher_info'): 
484 
return theform._extra_pageCatcher_info[boxType] 

1430  485 
if isinstance(theform, PDFFormXObject): 
486 
# internally defined form 

487 
return theform.BBoxList() 

488 
elif isinstance(theform, PDFStream): 

489 
# externally defined form 

2679  490 
return list(theform.dictionary.dict[boxType].sequence) 
1430  491 
else: 
3721  492 
raise ValueError("I don't understand the form instance %s" % repr(name)) 
1430  493 

1552  494 
def getXObjectName(self, name): 
1395  495 
"""Lets canvas find out what form is called internally. 
496 
Never mind whether it is defined yet or not.""" 

1552  497 
return xObjectName(name) 
1577  498 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

499 
def xobjDict(self, formnames): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

500 
"""construct an xobject dict (for inclusion in a resource dict, usually) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

501 
from a list of form names (images not yet supported)""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

502 
D = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

503 
for name in formnames: 
1552  504 
internalname = xObjectName(name) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

505 
reference = PDFObjectReference(internalname) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

506 
D[internalname] = reference 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

507 
#print "xobjDict D", D 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

508 
return PDFDictionary(D) 
1577  509 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

510 
def Reference(self, object, name=None): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

511 
### note references may "grow" during the final formatting pass: don't use d.keys()! 
628
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

512 
# don't make references to other references, or non instances, unless they are named! 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

513 
#print"object type is ", type(object) 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

514 
iob = hasattr(object,'__PDFObject__') 
628
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

515 
idToObject = self.idToObject 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

516 
if name is None and (not iob or object.__class__ is PDFObjectReference): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

517 
return object 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

518 
if hasattr(object, __InternalName__): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

519 
# already registered 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

520 
intname = object.__InternalName__ 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

521 
if name is not None and name!=intname: 
3721  522 
raise ValueError("attempt to reregister object %s with new name %s" % ( 
523 
repr(intname), repr(name))) 

3326  524 
if intname not in idToObject: 
3721  525 
raise ValueError("object named but not registered") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

526 
return PDFObjectReference(intname) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

527 
# otherwise register the new object 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

528 
objectcounter = self.objectcounter = self.objectcounter+1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

529 
if name is None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

530 
name = "R"+repr(objectcounter) 
3326  531 
if name in idToObject: 
628
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

532 
other = idToObject[name] 
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

533 
if other!=object: 
3721  534 
raise ValueError("redefining named object: "+repr(name)) 
628
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

535 
return PDFObjectReference(name) 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

536 
if iob: 
628
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

537 
object.__InternalName__ = name 
591  538 
#print "name", name, "counter", objectcounter 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

539 
self.idToObjectNumberAndVersion[name] = (objectcounter, 0) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

540 
self.numberToId[objectcounter] = name 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

541 
idToObject[name] = object 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

542 
return PDFObjectReference(name) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

543 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

544 
### chapter 4 Objects 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

545 
PDFtrue = "true" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

546 
PDFfalse = "false" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

547 
PDFnull = "null" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

548 

1062  549 
class PDFText: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

550 
__PDFObject__ = True 
1062  551 
def __init__(self, t): 
552 
self.t = t 

553 
def format(self, document): 

3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

554 
t = self.t 
3731  555 
if isUnicode(t): 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

556 
t = t.encode('utf8') 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

557 
result = binascii.hexlify(document.encrypt.encode(t)) 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

558 
return b"<" + result + b">" 
1062  559 
def __str__(self): 
560 
dummydoc = DummyDoc() 

561 
return self.format(dummydoc) 

1014
ba7f9c1294ea
added missing PDFText() function for consistency (needed by pagecatcher)
aaron_watters
parents:
873
diff
changeset

562 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

563 
def PDFnumber(n): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

564 
return n 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

565 

2517
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

566 
import re 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

567 
_re_cleanparens=re.compile('[^()]') 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

568 
del re 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

569 
def _isbalanced(s): 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

570 
'''test whether a string is balanced in parens''' 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

571 
s = _re_cleanparens.sub('',s) 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

572 
n = 0 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

573 
for c in s: 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

574 
if c=='(': n+=1 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

575 
else: 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

576 
n = 1 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

577 
if n<0: return 0 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

578 
return not n and 1 or 0 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

579 

2595  580 
def _checkPdfdoc(utext): 
581 
'''return true if no Pdfdoc encoding errors''' 

582 
try: 

583 
utext.encode('pdfdoc') 

584 
return 1 

3721  585 
except UnicodeEncodeError as e: 
2595  586 
return 0 
587 

1062  588 
class PDFString: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

589 
__PDFObject__ = True 
2595  590 
def __init__(self, s, escape=1, enc='auto'): 
591 
'''s can be unicode/utf8 or a PDFString 

592 
if escape is true then the output will be passed through escape 

593 
if enc is raw then the string will be left alone 

594 
if enc is auto we'll try and automatically adapt to utf_16_be if the 

595 
effective string is not entirely in pdfdoc 

596 
''' 

597 
if isinstance(s,PDFString): 

598 
self.s = s.s 

599 
self.escape = s.escape 

600 
self.enc = s.enc 

601 
else: 

602 
self.s = s 

603 
self.escape = escape 

604 
self.enc = enc 

1062  605 
def format(self, document): 
2517
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

606 
s = self.s 
2595  607 
enc = getattr(self,'enc','auto') 
3731  608 
if (isBytes(s)): 
2595  609 
if enc is 'auto': 
610 
try: 

2922
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

611 
u = s.decode(s.startswith(codecs.BOM_UTF16_BE) and 'utf16' or 'utf8') 
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

612 
if _checkPdfdoc(u): 
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

613 
s = u.encode('pdfdoc') 
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

614 
else: 
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

615 
s = codecs.BOM_UTF16_BE+u.encode('utf_16_be') 
2595  616 
except: 
2915  617 
try: 
2922
168b91cbfefc
reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents:
2919
diff
changeset

618 
s.decode('pdfdoc') 
2915  619 
except: 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

620 
sys.stderr.write('Error in %s' % (repr(s),)) 
2915  621 
raise 
3731  622 
elif isUnicode(s): 
2595  623 
if enc is 'auto': 
624 
if _checkPdfdoc(s): 

625 
s = s.encode('pdfdoc') 

626 
else: 

627 
s = codecs.BOM_UTF16_BE+s.encode('utf_16_be') 

628 
else: 

629 
s = codecs.BOM_UTF16_BE+s.encode('utf_16_be') 

630 
else: 

631 
raise ValueError('PDFString argument must be str/unicode not %s' % type(s)) 

632 

2524
7e6da7c7fe2c
merge stable 2687:2688 pdfdoc.py: attempt to robustify escape attribute access
rgbecker
parents:
2517
diff
changeset

633 
escape = getattr(self,'escape',1) 
2517
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

634 
if not isinstance(document.encrypt,NoEncryption): 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

635 
s = document.encrypt.encode(s) 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

636 
escape = 1 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

637 
if escape: 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

638 
try: 
3781  639 
es = "(%s)" % escapePDF(s) 
2517
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

640 
except: 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

641 
raise ValueError("cannot escape %s %s" % (s, repr(s))) 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

642 
if escape&2: 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

643 
es = es.replace('\\012','\n') 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

644 
if escape&4 and _isbalanced(s): 
2517
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

645 
es = es.replace('\\(','(').replace('\\)',')') 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

646 
return es 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

647 
else: 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

648 
return b'(' + s + b')' 
1062  649 
def __str__(self): 
3781  650 
return "(%s)" % escapePDF(self.s) 
1577  651 

2540  652 
def PDFName(data,lo=chr(0x21),hi=chr(0x7e)): 
484  653 
# might need to change this to class for encryption 
1062  654 
# NOTE: RESULT MUST ALWAYS SUPPORT MEANINGFUL COMPARISONS (EQUALITY) AND HASH 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

655 
# first convert the name 
2540  656 
L = list(data) 
657 
for i,c in enumerate(L): 

658 
if c<lo or c>hi or c in "%()<>{}[]#": 

659 
L[i] = "#"+hex(ord(c))[2:] # forget the 0x thing... 

660 
return "/"+(''.join(L)) 

1577  661 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

662 
class PDFDictionary: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

663 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

664 
multiline = LongFormat 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

665 
def __init__(self, dict=None): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

666 
"""dict should be namestring to value eg "a": 122 NOT pdfname to value NOT "/a":122""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

667 
if dict is None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

668 
self.dict = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

669 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

670 
self.dict = dict.copy() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

671 
def __setitem__(self, name, value): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

672 
self.dict[name] = value 
2919  673 
def __getitem__(self, a): 
674 
return self.dict[a] 

3326  675 
def __contains__(self,a): 
676 
return a in self.dict 

2730  677 
def Reference(self, name, document): 
2541  678 
self.dict[name] = document.Reference(self.dict[name]) 
679 
def format(self, document,IND=LINEEND+' '): 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

680 
dict = self.dict 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

681 
try: 
3721  682 
keys = list(dict.keys()) 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

683 
except: 
3721  684 
print(repr(dict)) 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

685 
raise 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

686 
keys.sort() 
2540  687 
L = [(format(PDFName(k),document)+" "+format(dict[k],document)) for k in keys] 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

688 
if self.multiline: 
2541  689 
L = IND.join(L) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

690 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

691 
# break up every 6 elements anyway 
2540  692 
t=L.insert 
3721  693 
for i in range(6, len(L), 6): 
2540  694 
t(i,LINEEND) 
695 
L = " ".join(L) 

696 
return "<< %s >>" % L 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

697 

2666  698 
def copy(self): 
699 
return PDFDictionary(self.dict) 

2778  700 

3630  701 
def normalize(self): 
702 
#normalize the names to use RL standard ie Name not /Name 

703 
D = self.dict 

3721  704 
K = [k for k in D.keys() if k.startswith('/')] 
3630  705 
for k in K: 
706 
D[k[1:]] = D.pop(k) 

707 

2869
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

708 
class checkPDFNames: 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

709 
def __init__(self,*names): 
3721  710 
self.names = list(map(PDFName,names)) 
2869
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

711 
def __call__(self,value): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

712 
if not value.startswith('/'): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

713 
value=PDFName(value) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

714 
if value in self.names: 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

715 
return value 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

716 

912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

717 
def checkPDFBoolean(value): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

718 
if value in ('true','false'): return value 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

719 

912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

720 
class CheckedPDFDictionary(PDFDictionary): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

721 
validate = {} 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

722 
def __init__(self,dict=None,validate=None): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

723 
PDFDictionary.__init__(self,dict) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

724 
if validate: self.validate = validate 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

725 

912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

726 
def __setitem__(self,name,value): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

727 
if name not in self.validate: 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

728 
raise ValueError('invalid key, %r' % name) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

729 
cvalue = self.validate[name](value) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

730 
if cvalue is None: 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

731 
raise ValueError('Bad value %r for key %r' % (value,name)) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

732 
PDFDictionary.__setitem__(self,name,cvalue) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

733 

912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

734 
class ViewerPreferencesPDFDictionary(CheckedPDFDictionary): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

735 
validate=dict( 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

736 
HideToolbar=checkPDFBoolean, 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

737 
HideMenubar=checkPDFBoolean, 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

738 
HideWindowUI=checkPDFBoolean, 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

739 
FitWindow=checkPDFBoolean, 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

740 
CenterWindow=checkPDFBoolean, 
3460
bbfe1f84fc80
pdfdoc.py: added DisplayDocTitle to viewer settings, contributed by Mark Erbaugh
rgbecker
parents:
3456
diff
changeset

741 
DisplayDocTitle=checkPDFBoolean, #contributed by mark Erbaugh 
2869
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

742 
NonFullScreenPageMode=checkPDFNames(*'UseNone UseOutlines UseThumbs UseOC'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

743 
Direction=checkPDFNames(*'L2R R2L'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

744 
ViewArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

745 
ViewClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

746 
PrintArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

747 
PrintClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

748 
PrintScaling=checkPDFNames(*'None AppDefault'.split()), 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

749 
) 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

750 

484  751 
# stream filters are objects to support round trip and 
752 
# possibly in the future also support parameters 

753 
class PDFStreamFilterZCompress: 

754 
pdfname = "FlateDecode" 

755 
def encode(self, text): 

674  756 
from reportlab.lib.utils import import_zlib 
757 
zlib = import_zlib() 

3721  758 
if not zlib: raise ImportError("cannot zcompress zlib unavailable") 
3731  759 
if isUnicode(text): 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

760 
text = text.encode('utf8') 
674  761 
return zlib.compress(text) 
484  762 
def decode(self, encoded): 
674  763 
from reportlab.lib.utils import import_zlib 
764 
zlib = import_zlib() 

3721  765 
if not zlib: raise ImportError("cannot zdecompress zlib unavailable") 
674  766 
return zlib.decompress(encoded) 
484  767 

768 
# need only one of these, unless we implement parameters later 

1577  769 
PDFZCompress = PDFStreamFilterZCompress() 
484  770 

771 
class PDFStreamFilterBase85Encode: 

772 
pdfname = "ASCII85Decode" 

773 
def encode(self, text): 

3781  774 
from .pdfutils import _wrap 
775 
text = asciiBase85Encode(text) 

2767
2ba5a1d26ad0
reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents:
2740
diff
changeset

776 
if rl_config.wrapA85: 
2ba5a1d26ad0
reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents:
2740
diff
changeset

777 
text = _wrap(text) 
2ba5a1d26ad0
reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents:
2740
diff
changeset

778 
return text 
484  779 
def decode(self, text): 
3781  780 
return asciiBase85Decode(text) 
1577  781 

484  782 
# need only one of these too 
783 
PDFBase85Encode = PDFStreamFilterBase85Encode() 

784 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

785 
STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

786 
"stream" # stream keyword 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

787 
"%(LINEEND)s" # a line end (could be just a \n) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

788 
"%(content)s" # the content, with no lineend 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

789 
"endstream%(LINEEND)s" # the endstream keyword 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

790 
) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

791 
class PDFStream: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

792 
'''set dictionary elements explicitly stream.dictionary[name]=value''' 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

793 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

794 
### compression stuff not implemented yet 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

795 
__RefOnly__ = 1 # must be at top level 
3144
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

796 
def __init__(self, dictionary=None, content=None, filters=None): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

797 
if dictionary is None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

798 
dictionary = PDFDictionary() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

799 
self.dictionary = dictionary 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

800 
self.content = content 
3144
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

801 
self.filters = filters 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

802 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

803 
dictionary = self.dictionary 
591  804 
# copy it for modification 
805 
dictionary = PDFDictionary(dictionary.dict.copy()) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

806 
content = self.content 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

807 
filters = self.filters 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

808 
if self.content is None: 
3721  809 
raise ValueError("stream content not set") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

810 
if filters is None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

811 
filters = document.defaultStreamFilters 
591  812 
# only apply filters if they haven't been applied elsewhere 
3326  813 
if filters is not None and "Filter" not in dictionary.dict: 
484  814 
# apply filters in reverse order listed 
815 
rf = list(filters) 

816 
rf.reverse() 

817 
fnames = [] 

818 
for f in rf: 

819 
#print "*****************content:"; print repr(content[:200]) 

820 
#print "*****************filter", f.pdfname 

821 
content = f.encode(content) 

822 
fnames.insert(0, PDFName(f.pdfname)) 

823 
#print "*****************finally:"; print content[:200] 

824 
#print "****** FILTERS", fnames 

825 
#stop 

826 
dictionary["Filter"] = PDFArray(fnames) 

1062  827 
# "stream encoding is done after all filters have been applied" 
828 
content = document.encrypt.encode(content) 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

829 
fc = format(content, document) 
591  830 
#print "type(content)", type(content), len(content), type(self.dictionary) 
831 
lc = len(content) 

484  832 
#if fc!=content: burp 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

833 
# set dictionary length parameter 
591  834 
dictionary["Length"] = lc 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

835 
fd = format(dictionary, document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

836 
sdict = LINEENDDICT.copy() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

837 
sdict["dictionary"] = fd 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

838 
sdict["content"] = fc 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

839 
return STREAMFMT % sdict 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

840 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

841 
def teststream(content=None): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

842 
#content = "" # test 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

843 
if content is None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

844 
content = teststreamcontent 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

845 
content = content.strip() 
3794  846 
content = content.replace("\n", LINEEND) + LINEEND 
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3327
diff
changeset

847 
S = PDFStream(content = content, 
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3327
diff
changeset

848 
filters=rl_config.useA85 and [PDFBase85Encode,PDFZCompress] or [PDFZCompress]) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

849 
# nothing else needed... 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

850 
S.__Comment__ = "test stream" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

851 
return S 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

852 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

853 
teststreamcontent = """ 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

854 
1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

855 
1.00 0.00 1.00 rg 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

856 
n 72.00 72.00 432.00 648.00 re B* 
1577  857 
""" 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

858 
class PDFArray: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

859 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

860 
multiline = LongFormat 
3144
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

861 
_ZLIST = list(9*' ')+[LINEEND] 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

862 
def __init__(self, sequence): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

863 
self.sequence = list(sequence) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

864 
def References(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

865 
"""make all objects in sequence references""" 
3721  866 
self.sequence = list(map(document.Reference, self.sequence)) 
2541  867 
def format(self, document, IND=LINEEND+' '): 
868 
L = [format(e, document) for e in self.sequence] 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

869 
if self.multiline: 
2541  870 
L = IND.join(L) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

871 
else: 
3144
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

872 
n=len(L) 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

873 
if n>10: 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

874 
# break up every 10 elements anyway 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

875 
m,r = divmod(n,10) 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

876 
L = ''.join([l+z for l,z in zip(L,m*self._ZLIST+list(r*' '))]) 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

877 
L = L.strip() 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

878 
else: 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

879 
L = ' '.join(L) 
2541  880 
return "[ %s ]" % L 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

881 

3144
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

882 
class PDFArrayCompact(PDFArray): 
a5d6bb711691
pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents:
3112
diff
changeset

883 
multiline=False 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

884 

3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

885 
INDIRECTOBFMT = "%(n)s %(v)s obj%(LINEEND)s%(content)s%(CLINEEND)sendobj%(LINEEND)s" 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

886 
class PDFIndirectObject: 
3318
59aae14a6010
pdfdoc.py: add __PDFObject__ to PDFIndirectObject class (patch from Arnaud Fontaine <arnaud@andesi.org>)
rgbecker
parents:
3251
diff
changeset

887 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

888 
__RefOnly__ = 1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

889 
def __init__(self, name, content): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

890 
self.name = name 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

891 
self.content = content 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

892 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

893 
name = self.name 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

894 
n, v = document.idToObjectNumberAndVersion[name] 
1062  895 
# set encryption parameters 
896 
document.encrypt.register(n, v) 

3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

897 
fcontent = format(self.content, document, toplevel=1) # yes this is at top level 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

898 
D = LINEENDDICT.copy() 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

899 
D["n"] = n 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

900 
D["v"] = v 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

901 
D["content"] = fcontent 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

902 
D['CLINEEND'] = (LINEEND,'')[fcontent.endswith(LINEEND)] 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

903 
return INDIRECTOBFMT % D 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

904 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

905 
class PDFObjectReference: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

906 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

907 
def __init__(self, name): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

908 
self.name = name 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

909 
def format(self, document): 
1317
2c0575047463
allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents:
1309
diff
changeset

910 
try: 
2541  911 
return "%s %s R" % document.idToObjectNumberAndVersion[self.name] 
1317
2c0575047463
allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents:
1309
diff
changeset

912 
except: 
3721  913 
raise KeyError("forward reference to %s not resolved upon final formatting" % repr(self.name)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

914 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

915 
### chapter 5 
681  916 
# Following Ken Lunde's advice and the PDF spec, this includes 
917 
# some highorder bytes. I chose the characters for Tokyo 

918 
# in ShiftJIS encoding, as these cannot be mistaken for 

919 
# any other encoding, and we'll be able to tell if something 

920 
# has run our PDF files through a dodgy Unicode conversion. 

1063  921 
PDFHeader = ( 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

922 
"%%PDF%s.%s"+LINEEND+ 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

923 
"%%\223\214\213\236 ReportLab Generated PDF document http://www.reportlab.com"+LINEEND) 
0  924 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

925 
class PDFFile: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

926 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

927 
### just accumulates strings: keeps track of current offset 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

928 
def __init__(self,pdfVersion=PDF_VERSION_DEFAULT): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

929 
self.strings = [] 
2477
704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

930 
self.write = self.strings.append 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

931 
self.offset = 0 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

932 
self.add(PDFHeader % pdfVersion) 
2477
704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

933 

704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

934 
def closeOrReset(self): 
704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

935 
pass 
704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

936 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

937 
def add(self, s): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

938 
"""should be constructed as late as possible, return position where placed""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

939 
result = self.offset 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

940 
self.offset = result+len(s) 
2477
704e05bad378
pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents:
2476
diff
changeset

941 
self.write(s) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

942 
return result 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

943 
def format(self, document): 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

944 
strings = map(str, self.strings) # final conversion, in case of lazy objects 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

945 
return "".join(strings) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

946 

1577  947 
XREFFMT = '%0.10d %0.5d n' 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

948 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

949 
class PDFCrossReferenceSubsection: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

950 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

951 
def __init__(self, firstentrynumber, idsequence): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

952 
self.firstentrynumber = firstentrynumber 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

953 
self.idsequence = idsequence 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

954 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

955 
"""id sequence should represent contiguous object nums else error. free numbers not supported (yet)""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

956 
firstentrynumber = self.firstentrynumber 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

957 
idsequence = self.idsequence 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

958 
entries = list(idsequence) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

959 
nentries = len(idsequence) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

960 
# special case: object number 0 is always free 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

961 
taken = {} 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

962 
if firstentrynumber==0: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

963 
taken[0] = "standard free entry" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

964 
nentries = nentries+1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

965 
entries.insert(0, "0000000000 65535 f") 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

966 
idToNV = document.idToObjectNumberAndVersion 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

967 
idToOffset = document.idToOffset 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

968 
lastentrynumber = firstentrynumber+nentries1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

969 
for id in idsequence: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

970 
(num, version) = idToNV[id] 
3326  971 
if num in taken: 
3721  972 
raise ValueError("object number collision %s %s %s" % (num, repr(id), repr(taken[id]))) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

973 
if num>lastentrynumber or num<firstentrynumber: 
3721  974 
raise ValueError("object number %s not in range %s..%s" % (num, firstentrynumber, lastentrynumber)) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

975 
# compute position in list 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

976 
rnum = numfirstentrynumber 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

977 
taken[num] = id 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

978 
offset = idToOffset[id] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

979 
entries[num] = XREFFMT % (offset, version) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

980 
# now add the initial line 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

981 
firstline = "%s %s" % (firstentrynumber, nentries) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

982 
entries.insert(0, firstline) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

983 
# make sure it ends with a LINEEND 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

984 
entries.append("") 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

985 
if LINEEND=="\n" or LINEEND=="\r": 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

986 
reflineend = " "+LINEEND # as per spec 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

987 
elif LINEEND=="\r\n": 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

988 
reflineend = LINEEND 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

989 
else: 
3721  990 
raise ValueError("bad end of line! %s" % repr(LINEEND)) 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

991 
return LINEEND.join(entries) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

992 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

993 
class PDFCrossReferenceTable: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

994 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

995 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

996 
def __init__(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

997 
self.sections = [] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

998 
def addsection(self, firstentry, ids): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

999 
section = PDFCrossReferenceSubsection(firstentry, ids) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1000 
self.sections.append(section) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1001 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1002 
sections = self.sections 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1003 
if not sections: 
3721  1004 
raise ValueError("no crossref sections") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1005 
L = ["xref"+LINEEND] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1006 
for s in self.sections: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1007 
fs = format(s, document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1008 
L.append(fs) 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1009 
return "".join(L) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1010 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1011 
TRAILERFMT = ("trailer%(LINEEND)s" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1012 
"%(dict)s%(LINEEND)s" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1013 
"startxref%(LINEEND)s" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1014 
"%(startxref)s%(LINEEND)s" 
873  1015 
"%(PERCENT)s%(PERCENT)sEOF%(LINEEND)s") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1016 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1017 
class PDFTrailer: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

1018 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1019 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1020 
def __init__(self, startxref, Size=None, Prev=None, Root=None, Info=None, ID=None, Encrypt=None): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1021 
self.startxref = startxref 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1022 
if Size is None or Root is None: 
3721  1023 
raise ValueError("Size and Root keys required") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1024 
dict = self.dict = PDFDictionary() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1025 
for (n,v) in [("Size", Size), ("Prev", Prev), ("Root", Root), 
1062  1026 
("Info", Info), ("ID", ID), ("Encrypt", Encrypt)]: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1027 
if v is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1028 
dict[n] = v 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1029 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1030 
fdict = format(self.dict, document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1031 
D = LINEENDDICT.copy() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1032 
D["dict"] = fdict 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1033 
D["startxref"] = self.startxref 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1034 
return TRAILERFMT % D 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1035 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1036 
#### XXXX skipping incremental update, 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1037 
#### encryption 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1038 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1039 
#### chapter 6, doc structure 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1040 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1041 
class PDFCatalog: 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

1042 
__PDFObject__ = True 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1043 
__Comment__ = "Document Root" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1044 
__RefOnly__ = 1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1045 
# to override, set as attributes 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1046 
__Defaults__ = {"Type": PDFName("Catalog"), 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1047 
"PageMode": PDFName("UseNone"), 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1048 
} 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1049 
__NoDefault__ = """ 
3629
f148d4473631
pdfdoc.py: fix openeactions>openaction bug fix contributed by Andrew Cutler
rgbecker
parents:
3617
diff
changeset

1050 
Dests Outlines Pages Threads AcroForm Names OpenAction PageMode URI 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1051 
ViewerPreferences PageLabels PageLayout JavaScript StructTreeRoot SpiderInfo""".split() 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1052 
__Refs__ = __NoDefault__ # make these all into references, if present 
1577  1053 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1054 
def format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1055 
self.check_format(document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1056 
defaults = self.__Defaults__ 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1057 
Refs = self.__Refs__ 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1058 
D = {} 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1059 
for k in defaults.keys(): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1060 
default = defaults[k] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1061 
v = None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1062 
if hasattr(self, k) and getattr(self,k) is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1063 
v = getattr(self, k) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1064 
elif default is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1065 
v = default 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1066 
if v is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1067 
D[k] = v 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1068 
for k in self.__NoDefault__: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1069 
if hasattr(self, k): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1070 
v = getattr(self,k) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1071 
if v is not None: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1072 
D[k] = v 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1073 
# force objects to be references where required 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1074 
for k in Refs: 
3326  1075 
if k in D: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1076 
#print"k is", k, "value", D[k] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1077 
D[k] = document.Reference(D[k]) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1078 
dict = PDFDictionary(D) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1079 
return format(dict, document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1080 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1081 
def showOutline(self): 
2852  1082 
self.setPageMode("UseOutlines") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1083 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1084 
def showFullScreen(self): 
2852  1085 
self.setPageMode("FullScreen") 
1086 

1087 
def setPageLayout(self,layout): 

1088 
if layout: 

1089 
self.PageLayout = PDFName(layout) 

1090 

1091 
def setPageMode(self,mode): 

1092 
if mode: 

1093 
self.PageMode = PDFName(mode) 

1577  1094 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1095 
def check_format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1096 
"""for use in subclasses""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1097 
pass 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1098 

136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1099 
class PDFPages(PDFCatalog): 
2229  1100 
"""PAGES TREE WITH ONE INTERNAL NODE, FOR "BALANCING" CHANGE IMPLEMENTATION""" 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1101 
__Comment__ = "page tree" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1102 
__RefOnly__ = 1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1103 
# note: could implement page attribute inheritance... 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1104 
__Defaults__ = {"Type": PDFName("Pages"), 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1105 
} 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1106 
__NoDefault__ = "Kids Count Parent".split() 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1107 
__Refs__ = ["Parent"] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1108 
def __init__(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1109 
self.pages = [] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1110 
def __getitem__(self, item): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1111 
return self.pages[item] 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1112 
def addPage(self, page): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1113 
self.pages.append(page) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1114 
def check_format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1115 
# convert all pages to page references 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1116 
pages = self.pages 
2229  1117 
kids = PDFArray(pages) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1118 
# make sure all pages are references 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1119 
kids.References(document) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1120 
self.Kids = kids 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1121 
self.Count = len(pages) 
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

1122 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1123 
class PDFPage(PDFCatalog): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1124 
__Comment__ = "Page dictionary" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1125 
# all PDF attributes can be set explicitly 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1126 
# if this flag is set, the "usual" behavior will be suppressed 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1127 
Override_default_compilation = 0 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1128 
__RefOnly__ = 1 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1129 
__Defaults__ = {"Type": PDFName("Page"), 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1130 
# "Parent": PDFObjectReference(Pages), # no! use document.Pages 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1131 
} 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1132 
__NoDefault__ = """Parent 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1133 
MediaBox Resources Contents CropBox Rotate Thumb Annots B Dur Hid Trans AA 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1134 
PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1135 
Trans""".split() 
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1136 
__Refs__ = """Contents Parent ID""".split() 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1137 
pagewidth = 595 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1138 
pageheight = 842 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1139 
stream = None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1140 
hasImages = 0 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1141 
compression = 0 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1142 
XObjects = None 
3226
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

1143 
_colorsUsed = {} 
3570
494980a689ff
reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents:
3470
diff
changeset

1144 
_shadingsUsed = {} 
500  1145 
Trans = None 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1146 
# transitionstring? 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1147 
# xobjects? 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1148 
# annotations 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1149 
def __init__(self): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1150 
# set all nodefaults to None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1151 
for name in self.__NoDefault__: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1152 
setattr(self, name, None) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1153 
def setCompression(self, onoff): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1154 
self.compression = onoff 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1155 
def setStream(self, code): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1156 
if self.Override_default_compilation: 
3721  1157 
raise ValueError("overridden! must set stream explicitly") 
3731  1158 
if isSeq(code): 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1159 
code = LINEEND.join(code)+LINEEND 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1160 
self.stream = code 
500  1161 

1162 
def setPageTransition(self, tranDict): 

1163 
self.Trans = PDFDictionary(tranDict) 

1577  1164 

481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1165 
def check_format(self, document): 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1166 
# set up parameters unless usual behaviour is suppressed 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1167 
if self.Override_default_compilation: 
1577  1168 
return 
2798  1169 
self.MediaBox = self.MediaBox or PDFArray(self.Rotate in (90,270) and [0,0,self.pageheight,self.pagewidth] or [0, 0, self.pagewidth, self.pageheight]) 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1170 
if not self.Annots: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1171 
self.Annots = None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1172 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1173 
#print self.Annots 
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset

1174 
#raise ValueError("annotations not reimplemented yet") 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

1175 
if not hasattr(self.Annots,'__PDFObject__'): 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1176 
self.Annots = PDFArray(self.Annots) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1177 
if not self.Contents: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1178 
stream = self.stream 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1179 
if not stream: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1180 
self.Contents = teststream() 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1181 
else: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1182 
S = PDFStream() 
484 