Skip to content

Commit

Permalink
Inline opener into document
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Oct 12, 2022
1 parent bfbaf0f commit 549e02f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
36 changes: 0 additions & 36 deletions src/pypdfium2/_helpers/_opener.py

This file was deleted.

57 changes: 42 additions & 15 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 549e02f

Please sign in to comment.