1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# A part of pdfrw (https://github.com/pmaupin/pdfrw)
# Copyright (C) 2006-2015 Patrick Maupin, Austin, Texas
# MIT license -- See LICENSE.txt for details
'''
The PdfWriter class writes an entire PDF file out to disk.
The writing process is not at all optimized or organized.
An instance of the PdfWriter class has two methods:
addpage(page)
and
write(fname)
addpage() assumes that the pages are part of a valid
tree/forest of PDF objects.
'''
import gc
from .objects import (PdfName, PdfArray, PdfDict, IndirectPdfDict,
PdfObject, PdfString)
from .compress import compress as do_compress
from .errors import PdfOutputError, log
from .py23_diffs import iteritems, convert_store
NullObject = PdfObject('null')
NullObject.indirect = True
NullObject.Type = 'Null object'
def user_fmt(obj, isinstance=isinstance, float=float, str=str,
basestring=str, encode=PdfString.encode):
''' This function may be replaced by the user for
specialized formatting requirements.
'''
if isinstance(obj, basestring):
return encode(obj)
# PDFs don't handle exponent notation
if isinstance(obj, float):
return ('%.9f' % obj).rstrip('0').rstrip('.')
return str(obj)
def FormatObjects(f, trailer, version='1.3', compress=True, killobj=(),
user_fmt=user_fmt, do_compress=do_compress,
convert_store=convert_store, iteritems=iteritems,
id=id, isinstance=isinstance, getattr=getattr, len=len,
sum=sum, set=set, str=str, hasattr=hasattr, repr=repr,
enumerate=enumerate, list=list, dict=dict, tuple=tuple,
PdfArray=PdfArray, PdfDict=PdfDict, PdfObject=PdfObject):
''' FormatObjects performs the actual formatting and disk write.
Should be a class, was a class, turned into nested functions
for performace (to reduce attribute lookups).
'''
def f_write(s):
f.write(convert_store(s))
def add(obj):
''' Add an object to our list, if it's an indirect
object. Just format it if not.
'''
# Can't hash dicts, so just hash the object ID
objid = id(obj)
# Automatically set stream objects to indirect
if isinstance(obj, PdfDict):
indirect = obj.indirect or (obj.stream is not None)
else:
indirect = getattr(obj, 'indirect', False)
if not indirect:
if objid in visited:
log.warning('Replicating direct %s object, '
'should be indirect for optimal file size' %
type(obj))
obj = type(obj)(obj)
objid = id(obj)
visiting(objid)
result = format_obj(obj)
leaving(objid)
return result
objnum = indirect_dict_get(objid)
# If we haven't seen the object yet, we need to
# add it to the indirect object list.
if objnum is None:
swapped = swapobj(objid)
if swapped is not None:
old_id = objid
obj = swapped
objid = id(obj)
objnum = indirect_dict_get(objid)
if objnum is not None:
indirect_dict[old_id] = objnum
return '%s 0 R' % objnum
objnum = len(objlist) + 1
objlist_append(None)
indirect_dict[objid] = objnum
deferred.append((objnum - 1, obj))
return '%s 0 R' % objnum
def format_array(myarray, formatter):
# Format array data into semi-readable ASCII
if sum([len(x) for x in myarray]) <= 70:
return formatter % space_join(myarray)
return format_big(myarray, formatter)
def format_big(myarray, formatter):
bigarray = []
count = 1000000
for x in myarray:
lenx = len(x) + 1
count += lenx
if count > 71:
subarray = []
bigarray.append(subarray)
count = lenx
subarray.append(x)
return formatter % lf_join([space_join(x) for x in bigarray])
def format_obj(obj):
''' format PDF object data into semi-readable ASCII.
May mutually recurse with add() -- add() will
return references for indirect objects, and add
the indirect object to the list.
'''
while 1:
if isinstance(obj, (list, dict, tuple)):
if isinstance(obj, PdfArray):
myarray = [add(x) for x in obj]
return format_array(myarray, '[%s]')
elif isinstance(obj, PdfDict):
if compress and obj.stream:
do_compress([obj])
pairs = sorted((x, y, getattr(x, 'encoded', x))
for (x, y) in obj.iteritems())
myarray = []
for key, value, encoding in pairs:
myarray.append(encoding)
myarray.append(add(value))
result = format_array(myarray, '<<%s>>')
stream = obj.stream
if stream is not None:
result = ('%s\nstream\n%s\nendstream' %
(result, stream))
return result
obj = (PdfArray, PdfDict)[isinstance(obj, dict)](obj)
continue
# We assume that an object with an indirect
# attribute knows how to represent itself to us.
if hasattr(obj, 'indirect'):
return str(getattr(obj, 'encoded', obj))
return user_fmt(obj)
def format_deferred():
while deferred:
index, obj = deferred.pop()
objlist[index] = format_obj(obj)
indirect_dict = {}
indirect_dict_get = indirect_dict.get
objlist = []
objlist_append = objlist.append
visited = set()
visiting = visited.add
leaving = visited.remove
space_join = ' '.join
lf_join = '\n '.join
deferred = []
# Don't reference old catalog or pages objects --
# swap references to new ones.
swapobj = {PdfName.Catalog: trailer.Root,
PdfName.Pages: trailer.Root.Pages, None: trailer}.get
swapobj = [(objid, swapobj(obj.Type))
for objid, obj in iteritems(killobj)]
swapobj = dict((objid, obj is None and NullObject or obj)
for objid, obj in swapobj).get
for objid in killobj:
assert swapobj(objid) is not None
# The first format of trailer gets all the information,
# but we throw away the actual trailer formatting.
format_obj(trailer)
# Keep formatting until we're done.
# (Used to recurse inside format_obj for this, but
# hit system limit.)
format_deferred()
# Now we know the size, so we update the trailer dict
# and get the formatted data.
trailer.Size = PdfObject(len(objlist) + 1)
trailer = format_obj(trailer)
# Now we have all the pieces to write out to the file.
# Keep careful track of the counts while we do it so
# we can correctly build the cross-reference.
header = '%%PDF-%s\n%%\xe2\xe3\xcf\xd3\n' % version
f_write(header)
offset = len(header)
offsets = [(0, 65535, 'f')]
offsets_append = offsets.append
for i, x in enumerate(objlist):
objstr = '%s 0 obj\n%s\nendobj\n' % (i + 1, x)
offsets_append((offset, 0, 'n'))
offset += len(objstr)
f_write(objstr)
f_write('xref\n0 %s\n' % len(offsets))
for x in offsets:
f_write('%010d %05d %s\r\n' % x)
f_write('trailer\n\n%s\nstartxref\n%s\n%%%%EOF\n' % (trailer, offset))
class PdfWriter(object):
_trailer = None
canonicalize = False
def __init__(self, version='1.3', compress=False):
self.pagearray = PdfArray()
self.compress = compress
self.version = version
self.killobj = {}
def addpage(self, page):
self._trailer = None
if page.Type != PdfName.Page:
raise PdfOutputError('Bad /Type: Expected %s, found %s'
% (PdfName.Page, page.Type))
inheritable = page.inheritable # searches for resources
self.pagearray.append(
IndirectPdfDict(
page,
Resources=inheritable.Resources,
MediaBox=inheritable.MediaBox,
CropBox=inheritable.CropBox,
Rotate=inheritable.Rotate,
)
)
# Add parents in the hierarchy to objects we
# don't want to output
killobj = self.killobj
obj = page.Parent
while obj is not None:
objid = id(obj)
if objid in killobj:
break
killobj[objid] = obj
obj = obj.Parent
return self
addPage = addpage # for compatibility with pyPdf
def addpages(self, pagelist):
for page in pagelist:
self.addpage(page)
return self
def _get_trailer(self):
trailer = self._trailer
if trailer is not None:
return trailer
if self.canonicalize:
self.make_canonical()
# Create the basic object structure of the PDF file
trailer = PdfDict(
Root=IndirectPdfDict(
Type=PdfName.Catalog,
Pages=IndirectPdfDict(
Type=PdfName.Pages,
Count=PdfObject(len(self.pagearray)),
Kids=self.pagearray
)
)
)
# Make all the pages point back to the page dictionary and
# ensure they are indirect references
pagedict = trailer.Root.Pages
for page in pagedict.Kids:
page.Parent = pagedict
page.indirect = True
self._trailer = trailer
return trailer
def _set_trailer(self, trailer):
self._trailer = trailer
trailer = property(_get_trailer, _set_trailer)
def write(self, fname, trailer=None, user_fmt=user_fmt,
disable_gc=True):
trailer = trailer or self.trailer
# Dump the data. We either have a filename or a preexisting
# file object.
preexisting = hasattr(fname, 'write')
f = preexisting and fname or open(fname, 'wb')
if disable_gc:
gc.disable()
try:
FormatObjects(f, trailer, self.version, self.compress,
self.killobj, user_fmt=user_fmt)
finally:
if not preexisting:
f.close()
if disable_gc:
gc.enable()
def make_canonical(self):
''' Canonicalizes a PDF. Assumes everything
is a Pdf object already.
'''
visited = set()
workitems = list(self.pagearray)
while workitems:
obj = workitems.pop()
objid = id(obj)
if objid in visited:
continue
visited.add(objid)
obj.indirect = False
if isinstance(obj, (PdfArray, PdfDict)):
obj.indirect = True
if isinstance(obj, PdfArray):
workitems += obj
else:
workitems += obj.values()
|