From 3f176ef6176e0c479ce1aaa7923c9fb0428a7c58 Mon Sep 17 00:00:00 2001 From: Tom Van Doorsselaere Date: Wed, 11 Nov 2015 22:22:23 +0100 Subject: [PATCH 1/2] I modified the code to use PyPDF2 instead of pypdf (which seems to be unmaintained). I also increased the python recursive limit, because it was necessary to work correctly for one of my documents. I also removed the initialize() statement, because this is not needed (anymore?). --- getannotations | 3 ++- prsannots/generic.py | 7 ++++--- prsannots/pagetext.py | 1 - prsannots/pdfannotation.py | 2 +- prsannots/pdfcontent.py | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/getannotations b/getannotations index 548e17a..456873c 100755 --- a/getannotations +++ b/getannotations @@ -27,12 +27,13 @@ highlights, and highlights with text notes are supported. import os import sys -import pyPdf +import PyPDF2 from prsannots.prst1 import Reader from prsannots.misc import u_raw_input, u_print, u_argv def select_book(books): + sys.setrecursionlimit(10000) u_print("Please select which book to get:") for i, book in enumerate(books): title = book.title or book.file.split('/')[-1] diff --git a/prsannots/generic.py b/prsannots/generic.py index f5c608f..edfb584 100644 --- a/prsannots/generic.py +++ b/prsannots/generic.py @@ -7,7 +7,8 @@ from xml.dom import minidom from StringIO import StringIO import hashlib -import pyPdf +import PyPDF2 +from prsannots.misc import u_raw_input, u_print, u_argv from pagetext import PageText, get_layouts, NoSubstringError, MultipleSubstringError from pdfannotation import highlight_annotation, text_annotation, add_annotation from pdfcontent import pdf_add_content, svg_to_pdf_content @@ -92,7 +93,7 @@ def hash(self): @property def pdf(self): """A pyPdf.PdfFileReader instance of the PDF file.""" - return pyPdf.PdfFileReader(open(os.path.join(self.reader.path, self.file), 'rb')) + return PyPDF2.PdfFileReader(open(os.path.join(self.reader.path, self.file), 'rb'),strict=False) def pdf_layout(self, page): """Get a pdfminer.LTPage object for page.""" @@ -125,7 +126,7 @@ def write_annotated_pdf(self, outfd, pdf=None, dice_map=None, **kw): if dice_map is None: dice_map = OneToOneMap(len(self.pdf.pages)) - outpdf = pyPdf.PdfFileWriter() + outpdf = PyPDF2.PdfFileWriter() j = k = 0 for i, page in enumerate(pdf.pages): while j < len(dice_map) and dice_map[j][0] == i: diff --git a/prsannots/pagetext.py b/prsannots/pagetext.py index 0f02753..dbd901e 100644 --- a/prsannots/pagetext.py +++ b/prsannots/pagetext.py @@ -56,7 +56,6 @@ def get_layouts(fd): parser = PDFParser(fd) doc = new_doc(parser) - doc.initialize() laparams = LAParams() rsrcmgr = PDFResourceManager() diff --git a/prsannots/pdfannotation.py b/prsannots/pdfannotation.py index d2c2f47..2f7764b 100644 --- a/prsannots/pdfannotation.py +++ b/prsannots/pdfannotation.py @@ -4,7 +4,7 @@ # the LGPL license. See the file COPYING for full details. from datetime import datetime -from pyPdf.generic import * +from PyPDF2.generic import * YELLOW = [0.95, 0.9, 0.2] diff --git a/prsannots/pdfcontent.py b/prsannots/pdfcontent.py index 18d6ec7..d563e29 100644 --- a/prsannots/pdfcontent.py +++ b/prsannots/pdfcontent.py @@ -3,8 +3,8 @@ # This file is part of prsannots and is distributed under the terms of # the LGPL license. See the file COPYING for full details. -from pyPdf.pdf import ContentStream -from pyPdf.generic import ArrayObject, NameObject +from PyPDF2.pdf import ContentStream +from PyPDF2.generic import ArrayObject, NameObject class StupidSVGInterpreterError(Exception): pass From f72727b8efb6afbfc49a2bf2a5ffe78a0a23a4cd Mon Sep 17 00:00:00 2001 From: Tom Van Doorsselaere Date: Mon, 16 Nov 2015 21:01:02 +0100 Subject: [PATCH 2/2] I removed the recursion statement. I have also removed the import of the write statements. --- getannotations | 1 - prsannots/generic.py | 1 - 2 files changed, 2 deletions(-) diff --git a/getannotations b/getannotations index 456873c..44da1ed 100755 --- a/getannotations +++ b/getannotations @@ -33,7 +33,6 @@ from prsannots.misc import u_raw_input, u_print, u_argv def select_book(books): - sys.setrecursionlimit(10000) u_print("Please select which book to get:") for i, book in enumerate(books): title = book.title or book.file.split('/')[-1] diff --git a/prsannots/generic.py b/prsannots/generic.py index edfb584..3825f45 100644 --- a/prsannots/generic.py +++ b/prsannots/generic.py @@ -8,7 +8,6 @@ from StringIO import StringIO import hashlib import PyPDF2 -from prsannots.misc import u_raw_input, u_print, u_argv from pagetext import PageText, get_layouts, NoSubstringError, MultipleSubstringError from pdfannotation import highlight_annotation, text_annotation, add_annotation from pdfcontent import pdf_add_content, svg_to_pdf_content