Skip to content

Commit 96cb457

Browse files
committed
ENH: Optimize getting of named destinations in readers
1 parent c17f03a commit 96cb457

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pypdf/_reader.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
ArrayObject,
7272
ContentStream,
7373
DecodedStreamObject,
74+
Destination,
7475
DictionaryObject,
7576
EncodedStreamObject,
7677
IndirectObject,
@@ -80,6 +81,7 @@
8081
PdfObject,
8182
StreamObject,
8283
TextStringObject,
84+
TreeObject,
8385
is_null_or_none,
8486
read_object,
8587
)
@@ -143,6 +145,9 @@ def __init__(
143145
elif password is not None:
144146
raise PdfReadError("Not an encrypted file")
145147

148+
# cache so we don't have to parse this over and over
149+
self._named_destinations: Optional[Dict[str, Destination]] = None
150+
146151
def _initialize_stream(self, stream: Union[StrByteType, Path]) -> None:
147152
if hasattr(stream, "mode") and "b" not in stream.mode:
148153
logger_warning(
@@ -1273,3 +1278,17 @@ def _repr_mimebundle_(
12731278
data = {k: v for k, v in data.items() if k not in exclude}
12741279

12751280
return data
1281+
1282+
def _get_named_destinations(
1283+
self,
1284+
tree: Union[TreeObject, None] = None,
1285+
retval: Optional[Dict[str, Destination]] = None,
1286+
) -> Dict[str, Destination]:
1287+
'''Override from PdfDocCommon. In the reader we can assume this is
1288+
static, but not in the writer.'''
1289+
if tree or retval:
1290+
return PdfDocCommon._get_named_destinations(self, tree, retval)
1291+
1292+
if self._named_destinations is None:
1293+
self._named_destinations = PdfDocCommon._get_named_destinations(self)
1294+
return self._named_destinations

0 commit comments

Comments
 (0)