Author: zvezdochiot Summary: changes for Python 3 URL: https://github.com/agl/jbig2enc/issues/72 ---- --- a/pdf.py +++ b/pdf.py @@ -70,11 +70,15 @@ class Obj: s.append(str(self.d)) if self.stream is not None: s.append('stream\n') - s.append(self.stream) +# s.append(self.stream) + if(type(self.stream)==str): + s.append(self.stream) + else: + s.append(self.stream.decode(encoding="cp437")) s.append('\nendstream\n') s.append('endobj\n') - return ''.join(s) + return (''.join(s)) class Doc: def __init__(self): @@ -117,24 +121,24 @@ class Doc: # sys.stderr.write(str(offsets) + "\n") - return '\n'.join(a) + return ('\n'.join(a)) def ref(x): - return '%d 0 R' % x + return ('%d 0 R' % x) -def main(symboltable='symboltable', pagefiles=glob.glob('page-*')): +def main(symboltable='symboltable', pagefiles=glob.glob('page-*'), out='out.pdf'): doc = Doc() doc.add_object(Obj({'Type' : '/Catalog', 'Outlines' : ref(2), 'Pages' : ref(3)})) doc.add_object(Obj({'Type' : '/Outlines', 'Count': '0'})) pages = Obj({'Type' : '/Pages'}) doc.add_object(pages) - symd = doc.add_object(Obj({}, file(symboltable, 'rb').read())) + symd = doc.add_object(Obj({}, open(symboltable, 'rb').read())) page_objs = [] pagefiles.sort() for p in pagefiles: try: - contents = file(p, mode='rb').read() + contents = open(p, mode='rb').read() except IOError: sys.stderr.write("error reading page file %s\n"% p) continue @@ -162,8 +166,10 @@ def main(symboltable='symboltable', page pages.d.d['Count'] = str(len(page_objs)) pages.d.d['Kids'] = '[' + ' '.join([ref(x.id) for x in page_objs]) + ']' - print str(doc) - +# print(str(doc)) + fout = open(out, mode='wb') + fout.write(str(doc).encode(encoding="cp437")) + fout.close() def usage(script, msg): if msg: