From bd7500d9f93f39c826fc21c396e81aa5bad98029 Mon Sep 17 00:00:00 2001 From: Denis Osipov Date: Sat, 16 Apr 2022 10:47:36 +0500 Subject: [PATCH] BUG: IndexError for getPage() of decryped file (#359) ISSUE: Problem appears because _flatten() method sets self.flattenedPages before it tries to get pages and doesn't set it back to None in case of error. This PR just makes _flatten() to set self.flattenedPages to an empty array after it successfully got pages. FIX: Call `self.flattenedPages` after calling `catalog["/Pages"].getObject()` Closes #327 --- PyPDF2/pdf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index 547284f80..1d0e95011 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -1575,9 +1575,11 @@ def _flatten(self, pages=None, inherit=None, indirectRef=None): if inherit is None: inherit = dict() if pages is None: - self.flattenedPages = [] + # Fix issue 327: set flattenedPages attribute only for + # decrypted file catalog = self.trailer["/Root"].getObject() pages = catalog["/Pages"].getObject() + self.flattenedPages = [] t = "/Pages" if "/Type" in pages: