tindtechnologies-py-pdftools 0.1.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_pdftools/__init__.py +48 -0
- py_pdftools/_api.py +45 -0
- py_pdftools/_exceptions.py +37 -0
- py_pdftools/_ffi.py +345 -0
- py_pdftools/_models.py +277 -0
- py_pdftools/_native/awt.dll +0 -0
- py_pdftools/_native/fontmanager.dll +0 -0
- py_pdftools/_native/freetype.dll +0 -0
- py_pdftools/_native/java.dll +0 -0
- py_pdftools/_native/javaaccessbridge.dll +0 -0
- py_pdftools/_native/javajpeg.dll +0 -0
- py_pdftools/_native/jawt.dll +0 -0
- py_pdftools/_native/jvm.dll +0 -0
- py_pdftools/_native/lcms.dll +0 -0
- py_pdftools/_native/py_pdftools.dll +0 -0
- py_pdftools/_protocol.py +186 -0
- py_pdftools/_runtime.py +130 -0
- py_pdftools/py.typed +1 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/METADATA +179 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/RECORD +25 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/WHEEL +4 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/licenses/LICENSE +201 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/licenses/LICENSES/PDFBOX-3.0.8.txt +1156 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/licenses/NOTICE +76 -0
- tindtechnologies_py_pdftools-0.1.0.dist-info/licenses/java/src/main/resources/dev/pypdftools/fonts/LICENSE.txt +94 -0
py_pdftools/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Public API for :mod:`py_pdftools`."""
|
|
2
|
+
|
|
3
|
+
from ._api import add_ocr_text_layer, inspect_pdf
|
|
4
|
+
from ._exceptions import (
|
|
5
|
+
FontError,
|
|
6
|
+
InvalidOcrDataError,
|
|
7
|
+
InvalidPdfError,
|
|
8
|
+
NativeLibraryError,
|
|
9
|
+
PageMismatchError,
|
|
10
|
+
PdfPasswordRequiredError,
|
|
11
|
+
PdfPermissionError,
|
|
12
|
+
PdfProcessingError,
|
|
13
|
+
PdfToolsError,
|
|
14
|
+
)
|
|
15
|
+
from ._models import (
|
|
16
|
+
NormalizedRect,
|
|
17
|
+
OcrDocument,
|
|
18
|
+
OcrLine,
|
|
19
|
+
OcrPage,
|
|
20
|
+
OcrTextLayerOptions,
|
|
21
|
+
Orientation,
|
|
22
|
+
PdfBox,
|
|
23
|
+
PdfInfo,
|
|
24
|
+
PdfPageInfo,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"FontError",
|
|
29
|
+
"InvalidOcrDataError",
|
|
30
|
+
"InvalidPdfError",
|
|
31
|
+
"NativeLibraryError",
|
|
32
|
+
"NormalizedRect",
|
|
33
|
+
"OcrDocument",
|
|
34
|
+
"OcrLine",
|
|
35
|
+
"OcrPage",
|
|
36
|
+
"OcrTextLayerOptions",
|
|
37
|
+
"Orientation",
|
|
38
|
+
"PageMismatchError",
|
|
39
|
+
"PdfBox",
|
|
40
|
+
"PdfInfo",
|
|
41
|
+
"PdfPageInfo",
|
|
42
|
+
"PdfPasswordRequiredError",
|
|
43
|
+
"PdfPermissionError",
|
|
44
|
+
"PdfProcessingError",
|
|
45
|
+
"PdfToolsError",
|
|
46
|
+
"add_ocr_text_layer",
|
|
47
|
+
"inspect_pdf",
|
|
48
|
+
]
|
py_pdftools/_api.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""High-level Python operations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from . import _runtime
|
|
6
|
+
from ._exceptions import InvalidPdfError
|
|
7
|
+
from ._models import OcrDocument, OcrTextLayerOptions, PdfInfo
|
|
8
|
+
from ._protocol import deserialize_pdf_info, serialize_ocr_request
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _validated_pdf_bytes(pdf: bytes | bytearray | memoryview) -> bytes:
|
|
12
|
+
if not isinstance(pdf, (bytes, bytearray, memoryview)):
|
|
13
|
+
raise TypeError("pdf must be bytes, bytearray, or memoryview")
|
|
14
|
+
|
|
15
|
+
value = bytes(pdf)
|
|
16
|
+
if not value:
|
|
17
|
+
raise InvalidPdfError("pdf must not be empty")
|
|
18
|
+
return value
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def add_ocr_text_layer(
|
|
22
|
+
pdf: bytes | bytearray | memoryview,
|
|
23
|
+
ocr: OcrDocument,
|
|
24
|
+
*,
|
|
25
|
+
options: OcrTextLayerOptions | None = None,
|
|
26
|
+
) -> bytes:
|
|
27
|
+
"""Return a copy of a PDF with an invisible OCR text layer added."""
|
|
28
|
+
|
|
29
|
+
pdf_bytes = _validated_pdf_bytes(pdf)
|
|
30
|
+
if not isinstance(ocr, OcrDocument):
|
|
31
|
+
raise TypeError("ocr must be an OcrDocument")
|
|
32
|
+
if options is not None and not isinstance(options, OcrTextLayerOptions):
|
|
33
|
+
raise TypeError("options must be an OcrTextLayerOptions or None")
|
|
34
|
+
|
|
35
|
+
effective_options = options or OcrTextLayerOptions()
|
|
36
|
+
request = serialize_ocr_request(ocr, effective_options)
|
|
37
|
+
return _runtime.add_ocr_text_layer(pdf_bytes, request)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def inspect_pdf(pdf: bytes | bytearray | memoryview) -> PdfInfo:
|
|
41
|
+
"""Return basic page and geometry information about a PDF."""
|
|
42
|
+
|
|
43
|
+
pdf_bytes = _validated_pdf_bytes(pdf)
|
|
44
|
+
response = _runtime.inspect_pdf(pdf_bytes)
|
|
45
|
+
return deserialize_pdf_info(response)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Public exception hierarchy for :mod:`py_pdftools`."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PdfToolsError(Exception):
|
|
5
|
+
"""Base exception for py-pdftools."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NativeLibraryError(PdfToolsError):
|
|
9
|
+
"""The native library could not be loaded, initialized, or used."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class InvalidPdfError(PdfToolsError):
|
|
13
|
+
"""The input is not a valid or supported PDF."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class InvalidOcrDataError(PdfToolsError):
|
|
17
|
+
"""The OCR model is structurally or numerically invalid."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class PageMismatchError(InvalidOcrDataError):
|
|
21
|
+
"""The supplied OCR pages do not match the PDF pages."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class PdfPasswordRequiredError(InvalidPdfError):
|
|
25
|
+
"""The PDF requires a password."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class PdfPermissionError(InvalidPdfError):
|
|
29
|
+
"""The PDF security settings prohibit modification."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class FontError(PdfToolsError):
|
|
33
|
+
"""OCR text cannot be represented using the configured font."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class PdfProcessingError(PdfToolsError):
|
|
37
|
+
"""The native backend failed while processing or saving the PDF."""
|
py_pdftools/_ffi.py
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"""Low-level ctypes adapter for the private GraalVM native ABI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ctypes
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import threading
|
|
9
|
+
from contextlib import contextmanager
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, Iterator
|
|
12
|
+
|
|
13
|
+
from ._exceptions import NativeLibraryError
|
|
14
|
+
from ._protocol import NativeStatus, exception_for_status
|
|
15
|
+
|
|
16
|
+
_NATIVE_LIBRARY_ENV = "PY_PDFTOOLS_NATIVE_LIBRARY"
|
|
17
|
+
_MAX_ERROR_MESSAGE_BYTES = 1024 * 1024
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _native_library_mode() -> int:
|
|
21
|
+
"""Expose native-image runtime symbols to its dynamically loaded JDK libraries."""
|
|
22
|
+
|
|
23
|
+
if os.name == "posix":
|
|
24
|
+
return int(getattr(ctypes, "RTLD_GLOBAL", ctypes.DEFAULT_MODE))
|
|
25
|
+
return int(ctypes.DEFAULT_MODE)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _NativeBuffer(ctypes.Structure):
|
|
29
|
+
_fields_ = [
|
|
30
|
+
("data", ctypes.c_void_p),
|
|
31
|
+
("length", ctypes.c_size_t),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def native_library_filename(platform: str | None = None) -> str:
|
|
36
|
+
"""Return the packaged library filename for a supported operating system."""
|
|
37
|
+
|
|
38
|
+
platform = sys.platform if platform is None else platform
|
|
39
|
+
if platform.startswith("linux"):
|
|
40
|
+
return "libpy_pdftools.so"
|
|
41
|
+
if platform == "darwin":
|
|
42
|
+
return "libpy_pdftools.dylib"
|
|
43
|
+
if platform in {"win32", "cygwin"}:
|
|
44
|
+
return "py_pdftools.dll"
|
|
45
|
+
raise NativeLibraryError(f"unsupported native platform: {platform}")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def discover_native_library() -> Path:
|
|
49
|
+
"""Locate an override library or the native artifact bundled in the package."""
|
|
50
|
+
|
|
51
|
+
override = os.environ.get(_NATIVE_LIBRARY_ENV)
|
|
52
|
+
if override:
|
|
53
|
+
path = Path(override)
|
|
54
|
+
if not path.is_file():
|
|
55
|
+
raise NativeLibraryError(
|
|
56
|
+
f"native library from {_NATIVE_LIBRARY_ENV} does not exist: {path}"
|
|
57
|
+
)
|
|
58
|
+
return path.resolve()
|
|
59
|
+
|
|
60
|
+
path = Path(__file__).with_name("_native") / native_library_filename()
|
|
61
|
+
if not path.is_file():
|
|
62
|
+
raise NativeLibraryError(
|
|
63
|
+
"native py-pdftools library was not found; expected bundled artifact "
|
|
64
|
+
f"at {path}"
|
|
65
|
+
)
|
|
66
|
+
return path
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class CtypesNativeBackend:
|
|
70
|
+
"""One GraalVM isolate and the one-shot operations exported by the C ABI."""
|
|
71
|
+
|
|
72
|
+
def __init__(self, library_path: Path | None = None) -> None:
|
|
73
|
+
self._library_path = library_path or discover_native_library()
|
|
74
|
+
try:
|
|
75
|
+
self._library = ctypes.CDLL(
|
|
76
|
+
str(self._library_path),
|
|
77
|
+
mode=_native_library_mode(),
|
|
78
|
+
)
|
|
79
|
+
except OSError as error:
|
|
80
|
+
raise NativeLibraryError(
|
|
81
|
+
f"failed to load native library {self._library_path}: {error}"
|
|
82
|
+
) from error
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
self._bind_functions()
|
|
86
|
+
except AttributeError as error:
|
|
87
|
+
raise NativeLibraryError(
|
|
88
|
+
f"native library {self._library_path} is missing a required symbol: {error}"
|
|
89
|
+
) from error
|
|
90
|
+
|
|
91
|
+
self._isolate: ctypes.c_void_p | None = None
|
|
92
|
+
self._creator_thread: ctypes.c_void_p | None = None
|
|
93
|
+
self._creator_ident: int | None = None
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def abi_version(self) -> int:
|
|
97
|
+
"""Return the ABI version reported by the loaded library."""
|
|
98
|
+
|
|
99
|
+
with self._attached_thread() as isolate_thread:
|
|
100
|
+
return int(self._pdftools_abi_version(isolate_thread))
|
|
101
|
+
|
|
102
|
+
def initialize(self) -> None:
|
|
103
|
+
"""Create the process-wide GraalVM isolate used by this backend."""
|
|
104
|
+
|
|
105
|
+
if self._isolate is not None:
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
isolate = ctypes.c_void_p()
|
|
109
|
+
isolate_thread = ctypes.c_void_p()
|
|
110
|
+
status = self._graal_create_isolate(
|
|
111
|
+
None,
|
|
112
|
+
ctypes.byref(isolate),
|
|
113
|
+
ctypes.byref(isolate_thread),
|
|
114
|
+
)
|
|
115
|
+
if status != 0 or not isolate.value or not isolate_thread.value:
|
|
116
|
+
raise NativeLibraryError(
|
|
117
|
+
f"failed to create GraalVM isolate (status {status})"
|
|
118
|
+
)
|
|
119
|
+
self._isolate = isolate
|
|
120
|
+
self._creator_thread = isolate_thread
|
|
121
|
+
self._creator_ident = threading.get_ident()
|
|
122
|
+
|
|
123
|
+
def inspect_pdf(self, pdf: bytes) -> bytes:
|
|
124
|
+
"""Invoke the one-shot native PDF inspection operation."""
|
|
125
|
+
|
|
126
|
+
with self._attached_thread() as isolate_thread:
|
|
127
|
+
pdf_buffer = self._input_buffer(pdf)
|
|
128
|
+
output = _NativeBuffer()
|
|
129
|
+
status = self._pdftools_inspect_pdf(
|
|
130
|
+
isolate_thread,
|
|
131
|
+
pdf_buffer,
|
|
132
|
+
len(pdf),
|
|
133
|
+
ctypes.byref(output),
|
|
134
|
+
)
|
|
135
|
+
return self._consume_output(isolate_thread, status, output)
|
|
136
|
+
|
|
137
|
+
def add_ocr_text_layer(self, pdf: bytes, request: bytes) -> bytes:
|
|
138
|
+
"""Invoke the one-shot native OCR text-layer operation."""
|
|
139
|
+
|
|
140
|
+
with self._attached_thread() as isolate_thread:
|
|
141
|
+
pdf_buffer = self._input_buffer(pdf)
|
|
142
|
+
request_buffer = self._input_buffer(request)
|
|
143
|
+
output = _NativeBuffer()
|
|
144
|
+
status = self._pdftools_add_ocr_text_layer(
|
|
145
|
+
isolate_thread,
|
|
146
|
+
pdf_buffer,
|
|
147
|
+
len(pdf),
|
|
148
|
+
request_buffer,
|
|
149
|
+
len(request),
|
|
150
|
+
ctypes.byref(output),
|
|
151
|
+
)
|
|
152
|
+
return self._consume_output(isolate_thread, status, output)
|
|
153
|
+
|
|
154
|
+
def close(self) -> None:
|
|
155
|
+
"""Tear down the GraalVM isolate, if it has been initialized."""
|
|
156
|
+
|
|
157
|
+
isolate = self._isolate
|
|
158
|
+
creator_thread = self._creator_thread
|
|
159
|
+
creator_ident = self._creator_ident
|
|
160
|
+
if isolate is None or creator_thread is None:
|
|
161
|
+
return
|
|
162
|
+
|
|
163
|
+
self._isolate = None
|
|
164
|
+
self._creator_thread = None
|
|
165
|
+
self._creator_ident = None
|
|
166
|
+
|
|
167
|
+
if creator_ident == threading.get_ident():
|
|
168
|
+
isolate_thread = creator_thread
|
|
169
|
+
else:
|
|
170
|
+
isolate_thread = ctypes.c_void_p()
|
|
171
|
+
status = self._graal_attach_thread(isolate, ctypes.byref(isolate_thread))
|
|
172
|
+
if status != 0 or not isolate_thread.value:
|
|
173
|
+
raise NativeLibraryError(
|
|
174
|
+
f"failed to attach thread for GraalVM shutdown (status {status})"
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
status = self._graal_tear_down_isolate(isolate_thread)
|
|
178
|
+
if status != 0:
|
|
179
|
+
raise NativeLibraryError(
|
|
180
|
+
f"failed to tear down GraalVM isolate (status {status})"
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
def _bind_functions(self) -> None:
|
|
184
|
+
self._pdftools_abi_version = self._bind(
|
|
185
|
+
"pdftools_abi_version",
|
|
186
|
+
[ctypes.c_void_p],
|
|
187
|
+
ctypes.c_uint32,
|
|
188
|
+
)
|
|
189
|
+
self._pdftools_inspect_pdf = self._bind(
|
|
190
|
+
"pdftools_inspect_pdf",
|
|
191
|
+
[
|
|
192
|
+
ctypes.c_void_p,
|
|
193
|
+
ctypes.POINTER(ctypes.c_uint8),
|
|
194
|
+
ctypes.c_size_t,
|
|
195
|
+
ctypes.POINTER(_NativeBuffer),
|
|
196
|
+
],
|
|
197
|
+
ctypes.c_uint32,
|
|
198
|
+
)
|
|
199
|
+
self._pdftools_add_ocr_text_layer = self._bind(
|
|
200
|
+
"pdftools_add_ocr_text_layer",
|
|
201
|
+
[
|
|
202
|
+
ctypes.c_void_p,
|
|
203
|
+
ctypes.POINTER(ctypes.c_uint8),
|
|
204
|
+
ctypes.c_size_t,
|
|
205
|
+
ctypes.POINTER(ctypes.c_uint8),
|
|
206
|
+
ctypes.c_size_t,
|
|
207
|
+
ctypes.POINTER(_NativeBuffer),
|
|
208
|
+
],
|
|
209
|
+
ctypes.c_uint32,
|
|
210
|
+
)
|
|
211
|
+
self._pdftools_free_buffer = self._bind(
|
|
212
|
+
"pdftools_free_buffer",
|
|
213
|
+
[ctypes.c_void_p, ctypes.c_void_p],
|
|
214
|
+
None,
|
|
215
|
+
)
|
|
216
|
+
self._pdftools_last_error = self._bind(
|
|
217
|
+
"pdftools_last_error",
|
|
218
|
+
[
|
|
219
|
+
ctypes.c_void_p,
|
|
220
|
+
ctypes.c_void_p,
|
|
221
|
+
ctypes.c_size_t,
|
|
222
|
+
ctypes.POINTER(ctypes.c_size_t),
|
|
223
|
+
],
|
|
224
|
+
ctypes.c_uint32,
|
|
225
|
+
)
|
|
226
|
+
self._graal_create_isolate = self._bind(
|
|
227
|
+
"graal_create_isolate",
|
|
228
|
+
[
|
|
229
|
+
ctypes.c_void_p,
|
|
230
|
+
ctypes.POINTER(ctypes.c_void_p),
|
|
231
|
+
ctypes.POINTER(ctypes.c_void_p),
|
|
232
|
+
],
|
|
233
|
+
ctypes.c_int,
|
|
234
|
+
)
|
|
235
|
+
self._graal_attach_thread = self._bind(
|
|
236
|
+
"graal_attach_thread",
|
|
237
|
+
[ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)],
|
|
238
|
+
ctypes.c_int,
|
|
239
|
+
)
|
|
240
|
+
self._graal_detach_thread = self._bind(
|
|
241
|
+
"graal_detach_thread",
|
|
242
|
+
[ctypes.c_void_p],
|
|
243
|
+
ctypes.c_int,
|
|
244
|
+
)
|
|
245
|
+
self._graal_tear_down_isolate = self._bind(
|
|
246
|
+
"graal_tear_down_isolate",
|
|
247
|
+
[ctypes.c_void_p],
|
|
248
|
+
ctypes.c_int,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
def _bind(self, name: str, argument_types: list[object], result_type: object) -> Any:
|
|
252
|
+
function = getattr(self._library, name)
|
|
253
|
+
function.argtypes = argument_types
|
|
254
|
+
function.restype = result_type
|
|
255
|
+
return function
|
|
256
|
+
|
|
257
|
+
@contextmanager
|
|
258
|
+
def _attached_thread(self) -> Iterator[ctypes.c_void_p]:
|
|
259
|
+
isolate = self._isolate
|
|
260
|
+
creator_thread = self._creator_thread
|
|
261
|
+
if isolate is None or creator_thread is None:
|
|
262
|
+
raise NativeLibraryError("native runtime has not been initialized")
|
|
263
|
+
|
|
264
|
+
if self._creator_ident == threading.get_ident():
|
|
265
|
+
yield creator_thread
|
|
266
|
+
return
|
|
267
|
+
|
|
268
|
+
isolate_thread = ctypes.c_void_p()
|
|
269
|
+
status = self._graal_attach_thread(isolate, ctypes.byref(isolate_thread))
|
|
270
|
+
if status != 0 or not isolate_thread.value:
|
|
271
|
+
raise NativeLibraryError(
|
|
272
|
+
f"failed to attach thread to GraalVM isolate (status {status})"
|
|
273
|
+
)
|
|
274
|
+
try:
|
|
275
|
+
yield isolate_thread
|
|
276
|
+
except BaseException:
|
|
277
|
+
self._graal_detach_thread(isolate_thread)
|
|
278
|
+
raise
|
|
279
|
+
else:
|
|
280
|
+
detach_status = self._graal_detach_thread(isolate_thread)
|
|
281
|
+
if detach_status != 0:
|
|
282
|
+
raise NativeLibraryError(
|
|
283
|
+
"failed to detach thread from GraalVM isolate "
|
|
284
|
+
f"(status {detach_status})"
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
def _consume_output(
|
|
288
|
+
self,
|
|
289
|
+
isolate_thread: ctypes.c_void_p,
|
|
290
|
+
status: int,
|
|
291
|
+
output: _NativeBuffer,
|
|
292
|
+
) -> bytes:
|
|
293
|
+
try:
|
|
294
|
+
if status != NativeStatus.SUCCESS:
|
|
295
|
+
message = self._last_error_message(isolate_thread, status)
|
|
296
|
+
raise exception_for_status(status, message)
|
|
297
|
+
if output.length and not output.data:
|
|
298
|
+
raise NativeLibraryError(
|
|
299
|
+
"native operation returned a null buffer with a non-zero length"
|
|
300
|
+
)
|
|
301
|
+
return ctypes.string_at(output.data, output.length) if output.length else b""
|
|
302
|
+
finally:
|
|
303
|
+
if output.data:
|
|
304
|
+
self._pdftools_free_buffer(isolate_thread, output.data)
|
|
305
|
+
|
|
306
|
+
def _last_error_message(
|
|
307
|
+
self,
|
|
308
|
+
isolate_thread: ctypes.c_void_p,
|
|
309
|
+
operation_status: int,
|
|
310
|
+
) -> str:
|
|
311
|
+
required_size = ctypes.c_size_t()
|
|
312
|
+
status = self._pdftools_last_error(
|
|
313
|
+
isolate_thread,
|
|
314
|
+
None,
|
|
315
|
+
0,
|
|
316
|
+
ctypes.byref(required_size),
|
|
317
|
+
)
|
|
318
|
+
if status != NativeStatus.SUCCESS or required_size.value == 0:
|
|
319
|
+
return f"native operation failed with status {operation_status}"
|
|
320
|
+
if required_size.value > _MAX_ERROR_MESSAGE_BYTES:
|
|
321
|
+
return (
|
|
322
|
+
"native operation returned an oversized error message "
|
|
323
|
+
f"({required_size.value} bytes)"
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
buffer = ctypes.create_string_buffer(required_size.value)
|
|
327
|
+
status = self._pdftools_last_error(
|
|
328
|
+
isolate_thread,
|
|
329
|
+
buffer,
|
|
330
|
+
len(buffer),
|
|
331
|
+
ctypes.byref(required_size),
|
|
332
|
+
)
|
|
333
|
+
if status != NativeStatus.SUCCESS:
|
|
334
|
+
return f"native operation failed with status {operation_status}"
|
|
335
|
+
return buffer.raw.split(b"\0", 1)[0].decode("utf-8", errors="replace")
|
|
336
|
+
|
|
337
|
+
@staticmethod
|
|
338
|
+
def _input_buffer(data: bytes) -> Any:
|
|
339
|
+
return (ctypes.c_uint8 * len(data)).from_buffer_copy(data)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def load_native_backend() -> CtypesNativeBackend:
|
|
343
|
+
"""Create the default ctypes-backed native adapter."""
|
|
344
|
+
|
|
345
|
+
return CtypesNativeBackend()
|