summaryrefslogtreecommitdiff
path: root/lib/pdfrw/pdfcompress.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pdfrw/pdfcompress.py')
-rw-r--r--lib/pdfrw/pdfcompress.py57
1 files changed, 0 insertions, 57 deletions
diff --git a/lib/pdfrw/pdfcompress.py b/lib/pdfrw/pdfcompress.py
deleted file mode 100644
index 1c11970..0000000
--- a/lib/pdfrw/pdfcompress.py
+++ /dev/null
@@ -1,57 +0,0 @@
1# A part of pdfrw (pdfrw.googlecode.com)
2# Copyright (C) 2006-2009 Patrick Maupin, Austin, Texas
3# MIT license -- See LICENSE.txt for details
4
5'''
6Currently, this sad little file only knows how to decompress
7using the flate (zlib) algorithm. Maybe more later, but it's
8not a priority for me...
9'''
10
11from __future__ import generators
12
13try:
14 set
15except NameError:
16 from sets import Set as set
17
18import zlib
19from pdfobjects import PdfDict, PdfName
20
21
22def streamobjects(mylist):
23 for obj in mylist:
24 if isinstance(obj, PdfDict) and obj.stream is not None:
25 yield obj
26
27def uncompress(mylist, warnings=set()):
28 flate = PdfName.FlateDecode
29 for obj in streamobjects(mylist):
30 ftype = obj.Filter
31 if ftype is None:
32 continue
33 if isinstance(ftype, list) and len(ftype) == 1:
34 # todo: multiple filters
35 ftype = ftype[0]
36 parms = obj.DecodeParms
37 if ftype != flate or parms is not None:
38 msg = 'Not decompressing: cannot use filter %s with parameters %s' % (repr(ftype), repr(parms))
39 if msg not in warnings:
40 warnings.add(msg)
41 print msg
42 else:
43 obj.stream = zlib.decompress(obj.stream)
44 obj.Filter = None
45
46def compress(mylist):
47 flate = PdfName.FlateDecode
48 for obj in streamobjects(mylist):
49 ftype = obj.Filter
50 if ftype is not None:
51 continue
52 oldstr = obj.stream
53 newstr = zlib.compress(oldstr)
54 if len(newstr) < len(oldstr) + 30:
55 obj.stream = newstr
56 obj.Filter = flate
57 obj.DecodeParms = None