From 549e02f9e4ea18f0e116cb8b3f807bda3768b2ed Mon Sep 17 00:00:00 2001 From: geisserml Date: Wed, 12 Oct 2022 13:21:44 +0200 Subject: [PATCH] Inline opener into document --- src/pypdfium2/_helpers/_opener.py | 36 ------------------- src/pypdfium2/_helpers/document.py | 57 ++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 51 deletions(-) delete mode 100644 src/pypdfium2/_helpers/_opener.py diff --git a/src/pypdfium2/_helpers/_opener.py b/src/pypdfium2/_helpers/_opener.py deleted file mode 100644 index e9c7c4254..000000000 --- a/src/pypdfium2/_helpers/_opener.py +++ /dev/null @@ -1,36 +0,0 @@ -# SPDX-FileCopyrightText: 2022 geisserml -# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause - -import pypdfium2._pypdfium as pdfium -from pypdfium2._helpers.misc import ( - PdfiumError, - ErrorToStr, - DataHolder, - is_input_buffer, - get_fileaccess, -) - - -def open_pdf(input_data, password=None): - - if isinstance(password, str): - password = password.encode("utf-8") - - ld_data = None - if isinstance(input_data, str): - pdf = pdfium.FPDF_LoadDocument(input_data.encode("utf-8"), password) - elif isinstance(input_data, bytes): - pdf = pdfium.FPDF_LoadMemDocument64(input_data, len(input_data), password) - ld_data = DataHolder(input_data) - elif is_input_buffer(input_data): - fileaccess, ld_data = get_fileaccess(input_data) - pdf = pdfium.FPDF_LoadCustomDocument(fileaccess, password) - else: - raise TypeError("Invalid input type '%s'" % type(input_data).__name__) - - if pdfium.FPDF_GetPageCount(pdf) < 1: - err_code = pdfium.FPDF_GetLastError() - pdfium_msg = ErrorToStr.get(err_code, "Error code %s" % err_code) - raise PdfiumError("Loading the document failed (PDFium: %s)" % pdfium_msg) - - return pdf, ld_data diff --git a/src/pypdfium2/_helpers/document.py b/src/pypdfium2/_helpers/document.py index 387ccdc09..a78e98512 100644 --- a/src/pypdfium2/_helpers/document.py +++ b/src/pypdfium2/_helpers/document.py @@ -14,11 +14,13 @@ OutlineItem, FileAccess, PdfiumError, + DataHolder, + ErrorToStr, ViewmodeToStr, get_functype, + get_fileaccess, is_input_buffer, ) -from pypdfium2._helpers._opener import open_pdf from pypdfium2._helpers.pageobject import ( PdfPageObject, ) @@ -33,6 +35,44 @@ logger = logging.getLogger(__name__) +def _open_pdf(input_data, password=None): + + if isinstance(password, str): + password = password.encode("utf-8") + + ld_data = None + if isinstance(input_data, str): + pdf = pdfium.FPDF_LoadDocument(input_data.encode("utf-8"), password) + elif isinstance(input_data, bytes): + pdf = pdfium.FPDF_LoadMemDocument64(input_data, len(input_data), password) + ld_data = DataHolder(input_data) + elif is_input_buffer(input_data): + fileaccess, ld_data = get_fileaccess(input_data) + pdf = pdfium.FPDF_LoadCustomDocument(fileaccess, password) + else: + raise TypeError("Invalid input type '%s'" % type(input_data).__name__) + + if pdfium.FPDF_GetPageCount(pdf) < 1: + err_code = pdfium.FPDF_GetLastError() + pdfium_msg = ErrorToStr.get(err_code, "Error code %s" % err_code) + raise PdfiumError("Loading the document failed (PDFium: %s)" % pdfium_msg) + + return pdf, ld_data + + +class _writer_class: + + def __init__(self, buffer): + self.buffer = buffer + if not callable( getattr(self.buffer, "write", None) ): + raise ValueError("Output buffer must implement the write() method.") + + def __call__(self, _, data, size): + block = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte * size)) + self.buffer.write(block.contents) + return 1 + + class PdfDocument (BitmapConvAliases): """ Document helper class. @@ -100,7 +140,7 @@ def __init__( if isinstance(self._actual_input, pdfium.FPDF_DOCUMENT): self.raw = self._actual_input else: - self.raw, self._ld_data = open_pdf(self._actual_input, self._password) + self.raw, self._ld_data = _open_pdf(self._actual_input, self._password) self._form_env = None self._form_config = None @@ -568,19 +608,6 @@ def render_to( assert len(page_indices) == i -class _writer_class: - - def __init__(self, buffer): - self.buffer = buffer - if not callable( getattr(self.buffer, "write", None) ): - raise ValueError("Output buffer must implement the write() method.") - - def __call__(self, _, data, size): - block = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte * size)) - self.buffer.write(block.contents) - return 1 - - class PdfXObject: """ XObject helper class.