jbig2pdf
view src/pdf.py @ 4:0ed3a4d9bbc6
Removed unused function.
| author | VanL <van.lindberg@gmail.com> |
|---|---|
| date | Wed Oct 28 15:28:09 2009 +0000 (3 months ago) |
| parents | 9e13329cb9f5 |
| children |
line source
1 """
2 pdf.py
4 From jbig2enc by Adam Langley
5 See http://github.com/agl/jbig2enc/tree/
6 """
7 import sys
8 import re
9 import struct
10 import glob
11 import os
13 # This is a very simple script to make a PDF file out of the output of a
14 # multipage symbol compression.
15 # Run ./jbig2 -s -p <other options> image1.jpeg image1.jpeg ...
16 # python pdf.py output > out.pdf
18 class Ref:
19 def __init__(self, x):
20 self.x = x
21 def __str__(self):
22 return "%d 0 R" % self.x
24 class Dict:
25 def __init__(self, values = {}):
26 self.d = {}
27 self.d.update(values)
29 def __str__(self):
30 s = ['<< ']
31 for (x, y) in self.d.items():
32 s.append('/%s ' % x)
33 s.append(str(y))
34 s.append("\n")
35 s.append(">>\n")
37 return ''.join(s)
39 global_next_id = 1
41 class Obj:
42 next_id = 1
43 def __init__(self, d = {}, stream = None):
44 global global_next_id
46 if stream is not None:
47 d['Length'] = str(len(stream))
48 self.d = Dict(d)
49 self.stream = stream
50 self.id = global_next_id
51 global_next_id += 1
53 def __str__(self):
54 s = []
55 s.append(str(self.d))
56 if self.stream is not None:
57 s.append('stream\n')
58 s.append(self.stream)
59 s.append('\nendstream\n')
60 s.append('endobj\n')
62 return ''.join(s)
64 class Doc:
65 def __init__(self):
66 self.objs = []
67 self.pages = []
69 def add_object(self, o):
70 self.objs.append(o)
71 return o
73 def add_page(self, o):
74 self.pages.append(o)
75 return self.add_object(o)
77 def __str__(self):
78 a = []
79 j = [0]
80 offsets = []
82 def add(x):
83 a.append(x)
84 j[0] += len(x) + 1
85 add('%PDF-1.4')
86 for o in self.objs:
87 offsets.append(j[0])
88 add('%d 0 obj' % o.id)
89 add(str(o))
90 xrefstart = j[0]
91 a.append('xref')
92 a.append('0 %d' % (len(offsets) + 1))
93 a.append('0000000000 65535 f ')
94 for o in offsets:
95 a.append('%010d 00000 n ' % o)
96 a.append('')
97 a.append('trailer')
98 a.append('<< /Size %d\n/Root 1 0 R >>' % (len(offsets) + 1))
99 a.append('startxref')
100 a.append(str(xrefstart))
101 a.append('%%EOF')
103 # sys.stderr.write(str(offsets) + "\n")
105 return '\n'.join(a)
107 def ref(x):
108 return '%d 0 R' % x
110 def main(symboltable='symboltable', pagefiles=glob.glob('page-*')):
111 doc = Doc()
112 doc.add_object(Obj({'Type' : '/Catalog', 'Outlines' : ref(2), 'Pages' : ref(3)}))
113 doc.add_object(Obj({'Type' : '/Outlines', 'Count': '0'}))
114 pages = Obj({'Type' : '/Pages'})
115 doc.add_object(pages)
116 symd = doc.add_object(Obj({}, file(symboltable, 'rb').read()))
117 page_objs = []
119 for p in pagefiles:
120 try:
121 contents = file(p).read()
122 except IOError:
123 sys.stderr.write("error reading page file %s\n"% p)
124 continue
125 (width, height) = struct.unpack('>II', contents[11:19])
126 xobj = Obj({'Type': '/XObject', 'Subtype': '/Image', 'Width':
127 str(width), 'Height': str(height), 'ColorSpace': '/DeviceGray',
128 'BitsPerComponent': '1', 'Filter': '/JBIG2Decode', 'DecodeParms':
129 ' << /JBIG2Globals %d 0 R >>' % symd.id}, contents)
130 contents = Obj({}, 'q %d 0 0 %d 0 0 cm /Im1 Do Q' % (width, height))
131 resources = Obj({'ProcSet': '[/PDF /ImageB]',
132 'XObject': '<< /Im1 %d 0 R >>' % xobj.id})
133 page = Obj({'Type': '/Page', 'Parent': '3 0 R',
134 'MediaBox': '[ 0 0 %d %d ]' % (width, height),
135 'Contents': ref(contents.id),
136 'Resources': ref(resources.id)})
137 [doc.add_object(x) for x in [xobj, contents, resources, page]]
138 page_objs.append(page)
140 pages.d.d['Count'] = str(len(page_objs))
141 pages.d.d['Kids'] = '[' + ' '.join([ref(x.id) for x in page_objs]) + ']'
143 return str(doc)
146 def usage(script, msg):
147 if msg:
148 sys.stderr.write("%s: %s\n"% (script, msg))
149 sys.stderr.write("Usage: %s [file_basename] > out.pdf\n"% script)
150 sys.exit(1)
153 if __name__ == '__main__':
155 if len(sys.argv) == 2:
156 sym = sys.argv[1] + '.sym'
157 pages = glob.glob(sys.argv[1] + '.[0-9]*')
158 elif len(sys.argv) == 1:
159 sym = 'symboltable'
160 pages = glob.glob('page-*')
161 else:
162 usage(sys.argv[0])
164 if not os.path.exists(sym):
165 usage("symbol table %s not found!"% sym)
166 elif len(pages) == 0:
167 usage("no pages found!")
169 print main(sym, pages)
