author  andy 
Wed, 29 Sep 2010 19:37:05 +0000  
changeset 3456  3143bbebbefe 
parent 3359  171fa15695a8 
child 3460  bbfe1f84fc80 
permissions  rwxrxrx 
2332  1 
#Copyright ReportLab Europe Ltd. 20002004 
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 
""" 
2575  17 
import string, 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 
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2869
diff
changeset

21 
from reportlab.lib.utils import import_zlib, open_for_read, fp_str, _digester 
2595  22 
from reportlab.pdfbase import pdfmetrics 
2993  23 
try: 
24 
from hashlib import md5 

25 
except ImportError: 

26 
from md5 import md5 

873  27 

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

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

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

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

31 
except: # pre2.0 
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

32 
# may be inaccurate but will at least 
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

33 
#work in anything which seeks to format 
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

34 
# version_info into a string 
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

35 
version_info = (1,5,2,'unknown',0) 
87b869faf50f
Fixed 1.5breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents:
1635
diff
changeset

36 

1622  37 
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

38 
# workaround for list()bug in Jython 2.1 (should be fixed in 2.2) 
1622  39 
def list(sequence): 
40 
def f(x): 

41 
return x 

42 
return map(f, sequence) 

43 

2737  44 
def utf8str(x): 
45 
if isinstance(x,unicode): 

46 
return x.encode('utf8') 

47 
else: 

48 
return str(x) 

49 

873  50 
class PDFError(Exception): 
51 
pass 

52 

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

53 
# 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

54 
LongFormat = 1 
1062  55 
##if LongFormat: (doesn't work) 
56 
## pass 

57 
##else: 

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

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

59 

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

60 
# __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

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

62 

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

63 
# __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

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

65 

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

66 
# __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

67 
# 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

68 
__Comment__ = "__Comment__" 
1062  69 

70 
# 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

71 
DoComments = 1 
1062  72 
if not LongFormat: 
73 
DoComments = 0 

481
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 
# name for standard font dictionary 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

77 

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

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

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

80 

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

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

82 

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

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

84 
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

85 
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

86 
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

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

88 
) 
0  89 

2540  90 
from types import InstanceType 
91 
def format(element, document, toplevel=0, InstanceType=InstanceType): 

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

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

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

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

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

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

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

98 
# 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

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

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

103 
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

104 
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

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

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

109 
return fp_str(element) 

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

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

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

112 

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

114 
return "FormXob.%s" % externalname 
1552  115 

1605  116 
# backwards compatibility 
117 
formName = xObjectName 

118 

1062  119 
# no encryption 
120 
class NoEncryption: 

121 
def encode(self, t): 

122 
"encode a string, stream, text" 

123 
return t 

124 
def prepare(self, document): 

125 
# get ready to do encryption 

126 
pass 

127 
def register(self, objnum, version): 

128 
# enter a new direct object 

129 
pass 

130 
def info(self): 

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

132 
return None 

133 

134 
class DummyDoc: 

135 
"used to bypass encryption when required" 

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

136 
__PDFObject__ = True 
1062  137 
encrypt = NoEncryption() 
1577  138 

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

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

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

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

144 
inObject = None 
1577  145 
# set this to define filters 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

146 
defaultStreamFilters = None 
1062  147 
encrypt = NoEncryption() # default no encryption 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

148 
pageCounter = 1 
1838
f7eeee67832c
Changes to allow invariant documents when needed
andy_robinson
parents:
1799
diff
changeset

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

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

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

152 
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

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

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

155 
): 
1577  156 

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

157 
# 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

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

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

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

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

163 
self._pdfVersion = pdfVersion 
1062  164 
# signature for creating PDF ID 
2993  165 
sig = self.signature = md5() 
1062  166 
sig.update("a reportlab document") 
1977  167 
if not self.invariant: 
1987
b2401bcddd6d
Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents:
1985
diff
changeset

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

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

170 
cat = 946684800.0 
b2401bcddd6d
Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents:
1985
diff
changeset

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

172 
# 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

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

174 
# 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

190 
self.info.invariant = self.invariant 
591  191 
#self.Reference(self.Catalog) 
192 
#self.Reference(self.Info) 

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

193 
self.fontMapping = {} 
873  194 
#make an empty font dictionary 
195 
DD = PDFDictionary({}) 

196 
DD.__Comment__ = "The standard fonts dictionary" 

2944  197 
self.Reference(DD, BasicFonts) 
1635  198 
self.delayedFonts = [] 
199 

200 
def setCompression(self, onoff): 

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

202 
self.compression = onoff 

873  203 

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

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

205 
"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

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

207 
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

208 

1062  209 
def updateSignature(self, thing): 
210 
"add information to the signature" 

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

2737  212 
self.signature.update(utf8str(thing)) 
1577  213 

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

215 
"A unique fingerprint for the file (unless in invariant mode)" 
1062  216 
if self._ID: 
217 
return self._ID 

1985  218 
digest = self.signature.digest() 
219 
doc = DummyDoc() 

2595  220 
ID = PDFString(digest,enc='raw') 
1985  221 
IDs = ID.format(doc) 
222 
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

223 
LINEEND, LINEEND, IDs, IDs, LINEEND) 
1062  224 
return self._ID 
0  225 

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

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

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

229 
f = filename 
2737  230 
filename = utf8str(getattr(filename,'name','')) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

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

232 
myfile = 1 
2737  233 
filename = utf8str(filename) 
1965
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

234 
f = open(filename, "wb") 
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

235 
f.write(self.GetPDFData(canvas)) 
02ef6a007f0b
David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents:
1903
diff
changeset

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

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

241 
markfilename(filename) # do platform specific file junk 

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

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

243 

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

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

247 
fnt.addObjects(self) 

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

249 
self.info.invariant = self.invariant 
1062  250 
self.info.digest(self.signature) 
251 
### 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

252 
# prepare outline 
591  253 
self.Reference(self.Catalog) 
603  254 
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

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

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

257 
return self.format() 
1577  258 

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

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

260 
"""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

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

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

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

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

265 

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

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

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

270 
# 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

271 
self.inObject = "form" 
1577  272 
# 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

273 

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

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

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

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

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

281 
fontObj = pdfmetrics.getFont(psfontname) 

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

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

287 
except KeyError: 

2915  288 
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

289 

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

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

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

292 

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

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

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

295 

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

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

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

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

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

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

302 

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

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

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

305 
# 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

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

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

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

310 

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

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

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

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

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

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

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

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

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

319 
return PDFObjectReference(internalname) 
1577  320 

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

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

322 
sname = cmyk.spotName 
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

323 
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

324 
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

325 
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

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

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

328 

0  329 
def setTitle(self, title): 
330 
"embeds in PDF file" 

2666  331 
if title is None: 
332 
self.info.title = '(anonymous)' 

333 
else: 

334 
self.info.title = title 

335 

0  336 
def setAuthor(self, author): 
337 
"embedded in PDF file" 

2666  338 
#allow resetting to clear it 
339 
if author is None: 

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

341 
else: 

342 
self.info.author = author 

1577  343 

0  344 
def setSubject(self, subject): 
345 
"embeds in PDF file" 

2666  346 

347 
#allow resetting to clear it 

348 
if subject is None: 

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

350 
else: 

351 
self.info.subject = subject 

352 

3456  353 
def setCreator(self, creator): 
354 
"embeds in PDF file" 

355 

356 
#allow resetting to clear it 

357 
if creator is None: 

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

359 
else: 

360 
self.info.creator = creator 

361 

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

364 

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

366 
if keywords is None: 

367 
self.info.keywords = '' 

368 
else: 

369 
self.info.keywords = keywords 

370 

2595  371 
def setDateFormatter(self, dateFormatter): 
372 
self.info._dateFormatter = dateFormatter 

373 

0  374 
def getAvailableFonts(self): 
375 
fontnames = self.fontMapping.keys() 

1151
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

376 
# the standard 14 are also always available! (even if not initialized yet) 
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

377 
import _fontdata 
431ef9e78aae
"the 14 std fonts" should always be "available"
aaron_watters
parents:
1063
diff
changeset

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

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

380 
fontnames.append(name) 
0  381 
fontnames.sort() 
382 
return fontnames 

681  383 

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

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

385 
# 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

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

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

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

391 
self.Reference(self.Catalog) 
603  392 
self.Reference(self.info) 
1062  393 
# register the encryption dictionary if present 
394 
encryptref = None 

395 
encryptinfo = self.encrypt.info() 

396 
if encryptinfo: 

397 
encryptref = self.Reference(encryptinfo) 

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

398 
# 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

399 
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

400 
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

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

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

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

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

405 
### 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

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

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

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

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

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

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

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

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

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

419 
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

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

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

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

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

424 
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

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

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

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

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

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

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

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

432 
if counter1!=lno: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

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

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

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

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

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

443 
Root = self.Reference(cat), 
1062  444 
Info = self.Reference(info), 
445 
Encrypt = encryptref, 

446 
ID = self.ID(), 

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

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

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

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

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

451 
return File.format(self) 
1577  452 

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

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

454 
"""test for existence of named form""" 
1552  455 
internalname = xObjectName(name) 
3326  456 
return internalname in self.idToObject 
0  457 

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

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

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

1552  462 
internalname = xObjectName(name) 
3326  463 
if internalname in self.idToObject: 
1430  464 
theform = self.idToObject[internalname] 
2798  465 
if hasattr(theform,'_extra_pageCatcher_info'): 
466 
return theform._extra_pageCatcher_info[boxType] 

1430  467 
if isinstance(theform, PDFFormXObject): 
468 
# internally defined form 

469 
return theform.BBoxList() 

470 
elif isinstance(theform, PDFStream): 

471 
# externally defined form 

2679  472 
return list(theform.dictionary.dict[boxType].sequence) 
1430  473 
else: 
474 
raise ValueError, "I don't understand the form instance %s" % repr(name) 

475 

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

1552  479 
return xObjectName(name) 
1577  480 

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

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

482 
"""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

483 
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

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

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

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

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

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

490 
return PDFDictionary(D) 
1577  491 

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

493 
### 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

494 
# 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

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

496 
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

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

498 
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

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

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

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

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

503 
if name is not None and name!=intname: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

504 
raise ValueError, "attempt to reregister object %s with new name %s" % ( 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

505 
repr(intname), repr(name)) 
3326  506 
if intname not in idToObject: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

507 
raise ValueError, "object named but not registered" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

512 
name = "R"+repr(objectcounter) 
3326  513 
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

514 
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

515 
if other!=object: 
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

516 
raise ValueError, "redefining named object: "+repr(name) 
7f5cd20a02f1
very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents:
603
diff
changeset

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

518 
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

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

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

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

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

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

525 

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

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

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

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

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

530 

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

532 
__PDFObject__ = True 
1062  533 
def __init__(self, t): 
534 
self.t = t 

535 
def format(self, document): 

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

536 
result = binascii.hexlify(document.encrypt.encode(self.t)) 
1062  537 
return "<%s>" % result 
538 
def __str__(self): 

539 
dummydoc = DummyDoc() 

540 
return self.format(dummydoc) 

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

541 

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

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

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

544 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

558 

2595  559 
def _checkPdfdoc(utext): 
560 
'''return true if no Pdfdoc encoding errors''' 

561 
try: 

562 
utext.encode('pdfdoc') 

563 
return 1 

564 
except UnicodeEncodeError, e: 

565 
return 0 

566 

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

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

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

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

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

574 
effective string is not entirely in pdfdoc 

575 
''' 

576 
if isinstance(s,PDFString): 

577 
self.s = s.s 

578 
self.escape = s.escape 

579 
self.enc = s.enc 

580 
else: 

581 
self.s = s 

582 
self.escape = escape 

583 
self.enc = enc 

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

585 
s = self.s 
2595  586 
enc = getattr(self,'enc','auto') 
587 
if type(s) is str: 

588 
if enc is 'auto': 

589 
try: 

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

590 
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

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

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

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

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

597 
s.decode('pdfdoc') 
2915  598 
except: 
599 
import sys 

600 
print >>sys.stderr, 'Error in',repr(s) 

601 
raise 

2595  602 
elif type(s) is unicode: 
603 
if enc is 'auto': 

604 
if _checkPdfdoc(s): 

605 
s = s.encode('pdfdoc') 

606 
else: 

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

608 
else: 

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

610 
else: 

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

612 

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

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

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

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

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

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

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

619 
es = "(%s)" % pdfutils._escape(s) 
c8f07edb561d
pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents:
2487
diff
changeset

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

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

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

623 
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

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

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

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

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

628 
return '(%s)' % s 
1062  629 
def __str__(self): 
630 
return "(%s)" % pdfutils._escape(self.s) 

1577  631 

2540  632 
def PDFName(data,lo=chr(0x21),hi=chr(0x7e)): 
484  633 
# might need to change this to class for encryption 
1062  634 
# 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

635 
# first convert the name 
2540  636 
L = list(data) 
637 
for i,c in enumerate(L): 

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

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

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

1577  641 

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

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

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

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

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

646 
"""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

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

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

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

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

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

652 
self.dict[name] = value 
2919  653 
def __getitem__(self, a): 
654 
return self.dict[a] 

3326  655 
def __contains__(self,a): 
656 
return a in self.dict 

2730  657 
def Reference(self, name, document): 
2541  658 
self.dict[name] = document.Reference(self.dict[name]) 
659 
def format(self, document,IND=LINEEND+' '): 

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

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

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

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

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

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

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

666 
keys.sort() 
2540  667 
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

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

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

671 
# break up every 6 elements anyway 
2540  672 
t=L.insert 
673 
for i in xrange(6, len(L), 6): 

674 
t(i,LINEEND) 

675 
L = " ".join(L) 

676 
return "<< %s >>" % L 

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

677 

2666  678 
def copy(self): 
679 
return PDFDictionary(self.dict) 

2778  680 

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

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

682 
def __init__(self,*names): 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

683 
self.names = map(PDFName,names) 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

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

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

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

687 
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

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

689 

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

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

691 
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

692 

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

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

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

695 
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

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

697 
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

698 

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

699 
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

700 
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

701 
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

702 
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

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

704 
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

705 
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

706 

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

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

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

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

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

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

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

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

714 
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

715 
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

716 
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

717 
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

718 
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

719 
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

720 
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

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

722 

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

725 
class PDFStreamFilterZCompress: 

726 
pdfname = "FlateDecode" 

727 
def encode(self, text): 

674  728 
from reportlab.lib.utils import import_zlib 
729 
zlib = import_zlib() 

730 
if not zlib: raise ImportError, "cannot zcompress zlib unavailable" 

731 
return zlib.compress(text) 

484  732 
def decode(self, encoded): 
674  733 
from reportlab.lib.utils import import_zlib 
734 
zlib = import_zlib() 

735 
if not zlib: raise ImportError, "cannot zdecompress zlib unavailable" 

736 
return zlib.decompress(encoded) 

484  737 

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

1577  739 
PDFZCompress = PDFStreamFilterZCompress() 
484  740 

741 
class PDFStreamFilterBase85Encode: 

742 
pdfname = "ASCII85Decode" 

743 
def encode(self, text): 

744 
from pdfutils import _AsciiBase85Encode, _wrap 

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

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

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

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

748 
return text 
484  749 
def decode(self, text): 
750 
from pdfutils import _AsciiBase85Decode 

751 
return _AsciiBase85Decode(text) 

1577  752 

484  753 
# need only one of these too 
754 
PDFBase85Encode = PDFStreamFilterBase85Encode() 

755 

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

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

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

758 
"%(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

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

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

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

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

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

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

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

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

767 
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

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

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

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

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

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

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

774 
dictionary = self.dictionary 
591  775 
# copy it for modification 
776 
dictionary = PDFDictionary(dictionary.dict.copy()) 

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

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

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

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

780 
raise ValueError, "stream content not set" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

782 
filters = document.defaultStreamFilters 
591  783 
# only apply filters if they haven't been applied elsewhere 
3326  784 
if filters is not None and "Filter" not in dictionary.dict: 
484  785 
# apply filters in reverse order listed 
786 
rf = list(filters) 

787 
rf.reverse() 

788 
fnames = [] 

789 
for f in rf: 

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

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

792 
content = f.encode(content) 

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

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

795 
#print "****** FILTERS", fnames 

796 
#stop 

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

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

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

800 
fc = format(content, document) 
591  801 
#print "type(content)", type(content), len(content), type(self.dictionary) 
802 
lc = len(content) 

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

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

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

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

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

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

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

811 

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

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

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

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

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

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

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

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

819 
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

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

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

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

823 

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

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

825 
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

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

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

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

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

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

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

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

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

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

836 
"""make all objects in sequence references""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

837 
self.sequence = map(document.Reference, self.sequence) 
2541  838 
def format(self, document, IND=LINEEND+' '): 
839 
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

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

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

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

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

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

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

847 
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

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

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

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

852 

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

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

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

855 

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

856 
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

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

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

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

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

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

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

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

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

865 
n, v = document.idToObjectNumberAndVersion[name] 
1062  866 
# set encryption parameters 
867 
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

868 
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

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

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

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

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

873 
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

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

875 

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

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

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

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

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

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

881 
try: 
2541  882 
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

883 
except: 
2541  884 
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

885 

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

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

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

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

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

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

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

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

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

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

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

898 
### 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

899 
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

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

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

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

903 
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

904 

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

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

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

907 

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

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

909 
"""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

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

911 
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

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

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

914 
def format(self, document): 
1062  915 
strings = map(str, self.strings) # final conversion, in case of lazy objects 
2359  916 
return string.join(strings, "") 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

917 

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

919 

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

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

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

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

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

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

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

926 
"""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

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

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

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

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

931 
# 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

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

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

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

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

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

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

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

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

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

941 
(num, version) = idToNV[id] 
3326  942 
if num in taken: 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

943 
raise ValueError, "object number collision %s %s %s" % (num, repr(id), repr(taken[id])) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

944 
if num>lastentrynumber or num<firstentrynumber: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

945 
raise ValueError, "object number %s not in range %s..%s" % (num, firstentrynumber, lastentrynumber) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

961 
raise ValueError, "bad end of line! %s" % repr(LINEEND) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

963 

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

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

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

966 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

981 

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

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

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

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

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

987 

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

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

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

990 

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

991 
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

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

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

994 
raise ValueError, "Size and Root keys required" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

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

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

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

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

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

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

1006 

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

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

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

1009 

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

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

1011 

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

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

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

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

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

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

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

1018 
"PageMode": PDFName("UseNone"), 
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 
__NoDefault__ = string.split(""" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1021 
Dests Outlines Pages Threads AcroForm Names OpenActions PageMode URI 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

1024 
__Refs__ = __NoDefault__ # make these all into references, if present 
1577  1025 

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

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

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

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

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

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

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

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

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

1034 
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1052 

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

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

1055 

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

1056 
def showFullScreen(self): 
2852  1057 
self.setPageMode("FullScreen") 
1058 

1059 
def setPageLayout(self,layout): 

1060 
if layout: 

1061 
self.PageLayout = PDFName(layout) 

1062 

1063 
def setPageMode(self,mode): 

1064 
if mode: 

1065 
self.PageMode = PDFName(mode) 

1577  1066 

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

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

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

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

1070 

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

1071 
class PDFPages(PDFCatalog): 
2229  1072 
"""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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1093 
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

1094 

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

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

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

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

1098 
# 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

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

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

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

1102 
# "Parent": PDFObjectReference(Pages), # no! use document.Pages 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

1105 
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

1106 
PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ 
500  1107 
Trans 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

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

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

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

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

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

1117 
XObjects = None 
3226
cd06c1172678
pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents:
3198
diff
changeset

1118 
_colorsUsed = {} 
500  1119 
Trans = None 
481
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

1124 
# set all nodefaults to None 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

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

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

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

1131 
raise ValueError, "overridden! must set stream explicitly" 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

1133 
if type(code) is ListType: 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1134 
code = string.join(code, LINEEND)+LINEEND 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1135 
self.stream = code 
500  1136 

1137 
def setPageTransition(self, tranDict): 

1138 
self.Trans = PDFDictionary(tranDict) 

1577  1139 

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

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

1141 
# 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

1142 
if self.Override_default_compilation: 
1577  1143 
return 
2798  1144 
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

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

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

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

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

1149 
#raise ValueError, "annotations not reimplemented yet" 
3101
ec18b8a44cd2
reportlab:attempt to make PDF objects more explicit
rgbecker
parents:
3031
diff
changeset

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

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

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

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

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

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

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

1157 
S = PDFStream() 
484  1158 
if self.compression: 
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3327
diff
changeset

1159 
S.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

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

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

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

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

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

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

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

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

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

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

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

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

1172 
#print "XObjects", self.XObjects.dict 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

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

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

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

1176 
resources.setColorSpace(self._colorsUsed) 
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3172
diff
changeset

1177 

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

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

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

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

1181 
self.Parent = document.Reference(pages) 
136669babedf
complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents:
479
diff
changeset

1182 

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

1183 
#this code contributed by Christian Jacobs <cljacobsen@gmail.com> 
2869
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

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

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

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

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

1188 
__NoDefault__ = ["Nums"] 
912456caa112
reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents:
2852
diff
changeset

1189 
__Refs__ = [] 