1"""Helpers for working with PDF types."""
2
3from abc import abstractmethod
4from pathlib import Path
5from typing import IO, Any, Dict, List, Optional, Protocol, Tuple, Union
6
7from ._utils import StrByteType, StreamType
8
9
10class PdfObjectProtocol(Protocol):
11 indirect_reference: Any
12
13 def clone(
14 self,
15 pdf_dest: Any,
16 force_duplicate: bool = False,
17 ignore_fields: Union[Tuple[str, ...], List[str], None] = (),
18 ) -> Any:
19 ... # pragma: no cover
20
21 def _reference_clone(self, clone: Any, pdf_dest: Any) -> Any:
22 ... # pragma: no cover
23
24 def get_object(self) -> Optional["PdfObjectProtocol"]:
25 ... # pragma: no cover
26
27 def hash_value(self) -> bytes:
28 ... # pragma: no cover
29
30 def write_to_stream(
31 self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
32 ) -> None:
33 ... # pragma: no cover
34
35
36class XmpInformationProtocol(PdfObjectProtocol):
37 pass
38
39
40class PdfCommonDocProtocol(Protocol):
41 @property
42 def pdf_header(self) -> str:
43 ... # pragma: no cover
44
45 @property
46 def pages(self) -> List[Any]:
47 ... # pragma: no cover
48
49 @property
50 def root_object(self) -> PdfObjectProtocol:
51 ... # pragma: no cover
52
53 def get_object(self, indirect_reference: Any) -> Optional[PdfObjectProtocol]:
54 ... # pragma: no cover
55
56 @property
57 def strict(self) -> bool:
58 ... # pragma: no cover
59
60
61class PdfReaderProtocol(PdfCommonDocProtocol, Protocol):
62 @property
63 @abstractmethod
64 def xref(self) -> Dict[int, Dict[int, Any]]:
65 ... # pragma: no cover
66
67 @property
68 @abstractmethod
69 def trailer(self) -> Dict[str, Any]:
70 ... # pragma: no cover
71
72
73class PdfWriterProtocol(PdfCommonDocProtocol, Protocol):
74 _objects: List[Any]
75 _id_translated: Dict[int, Dict[int, int]]
76
77 incremental: bool
78 _reader: Any # PdfReader
79
80 @abstractmethod
81 def write(self, stream: Union[Path, StrByteType]) -> Tuple[bool, IO[Any]]:
82 ... # pragma: no cover
83
84 @abstractmethod
85 def _add_object(self, obj: Any) -> Any:
86 ... # pragma: no cover