diff --git a/server/annotate_pypdf.py b/server/annotate_pypdf.py index 86ff812..bc4e5e4 100644 --- a/server/annotate_pypdf.py +++ b/server/annotate_pypdf.py @@ -18,10 +18,18 @@ ## You should have received a copy of the GNU Affero General Public License ## along with this program. If not, see . ## -from cStringIO import StringIO +from __future__ import print_function + +from io import BytesIO import sys import time +try: + basestring +except NameError: + # Python 3 + basestring = str + try: from PyPDF2.generic import ArrayObject, BooleanObject, DictionaryObject, FloatObject, NameObject, RectangleObject, TextStringObject from PyPDF2.pdf import PdfFileReader, PdfFileWriter @@ -49,7 +57,7 @@ def annotate(fp_in, annotations): try: pdfpage = pdf.getPage(page) except IndexError: - print >> sys.stderr, 'Page %d not found in pdf, not adding annotations %r' % (page, annotation) + print('Page %d not found in pdf, not adding annotation %r' % (page, annotation), file=sys.stderr) continue size = pdfpage.mediaBox @@ -64,19 +72,19 @@ def annotate(fp_in, annotations): else: x = float(x) y = float(y) - print >> sys.stderr, 'Page rotated by %d degrees not implemented yet' % (angle) + print('Page rotated by %d degrees not implemented yet' % (angle), file=sys.stderr) color = annotation.get('color', None) if isinstance(color, basestring): if color[:1] != '#': - print >> sys.stderr, 'Unsupported color format: %s' % (color) + print('Unsupported color format: %s' % (color), file=sys.stderr) color = None else: # Assume HTML color with format "#RRGGBB". try: color = int(color[1:], 16) except ValueError as e: - print >> sys.stderr, 'Unsupported color format: %s (%s)' % (color, e) + print('Unsupported color format: %s (%s)' % (color, e), file=sys.stderr) color = None if color is not None: @@ -113,7 +121,6 @@ def annotate(fp_in, annotations): else: annots.append(annoRef) - fp_out = StringIO() + fp_out = BytesIO() pdf.write(fp_out) return fp_out.getvalue() - diff --git a/server/pdfannotate b/server/pdfannotate index 8e53178..f13a534 100755 --- a/server/pdfannotate +++ b/server/pdfannotate @@ -20,9 +20,10 @@ ## You should have received a copy of the GNU Affero General Public License ## along with this program. If not, see . ## +from __future__ import print_function import argparse -from cStringIO import StringIO +from io import BytesIO import json import sys @@ -42,21 +43,21 @@ def main(): args = parser.parse_args() try: - annotations = json.loads(file(args.text, 'rb').read()) - except Exception, e: + annotations = json.loads(open(args.text, 'rb').read()) + except Exception as e: parser.error('Could not open/read text annotations: %s' % (e)) if args.input == '-': - fp_in = StringIO(sys.stdin.read()) + fp_in = BytesIO(sys.stdin.read()) else: - fp_in = file(args.input, 'rb') + fp_in = open(args.input, 'rb') pdf_data = annotate(fp_in, annotations) if args.output == '-': sys.stdout.write(pdf_data) else: - with file(args.output, 'wb') as fp: + with open(args.output, 'wb') as fp: fp.write(pdf_data) if __name__ == '__main__':