Skip to content

Commit

Permalink
Return malformed named destinations and outlines
Browse files Browse the repository at this point in the history
Always return an outline or named destination from document, even if lacking a valid destination. Raise warning and set destination to first page.
  • Loading branch information
mtd91429 committed Jul 8, 2022
1 parent dba85c4 commit 5539deb
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import os
import re
import struct
from types import NoneType
import warnings
import zlib
from io import BytesIO
Expand Down Expand Up @@ -797,12 +798,12 @@ def _build_destination(
title: str,
array: List[Union[NumberObject, IndirectObject, NullObject, DictionaryObject]],
) -> Destination:
page, typ = array[0:2]
array = array[2:]
try:
page, typ = array[0:2]
array = array[2:]
return Destination(title, page, typ, *array) # type: ignore
except PdfReadError:
warnings.warn(f"Unknown destination: {title} {array}", PdfReadWarning)
except Exception as error:
warnings.warn(f"Error when processing destination {title} {array}. {error}", PdfReadWarning)
if self.strict:
raise
else:
Expand All @@ -828,15 +829,13 @@ def _build_outline(self, node: DictionaryObject) -> Optional[Destination]:
title = node["/Title"]
dest = node["/Dest"]

# if destination found, then create outline
if dest:
if isinstance(dest, ArrayObject):
outline = self._build_destination(title, dest) # type: ignore
elif isinstance(dest, str) and dest in self._namedDests:
outline = self._namedDests[dest]
outline[NameObject("/Title")] = title # type: ignore
else:
raise PdfReadError(f"Unexpected destination {dest!r}")
if isinstance(dest, ArrayObject) or isinstance(dest, NoneType):
outline = self._build_destination(title, dest) # type: ignore
elif isinstance(dest, str) and dest in self._namedDests:
outline = self._namedDests[dest]
outline[NameObject("/Title")] = title # type: ignore
else:
raise PdfReadError(f"Unexpected destination {dest!r}")

# if color or text format specifications present, add to outline
if "/C" in node:
Expand Down

0 comments on commit 5539deb

Please sign in to comment.