Skip to content

Commit 30f86ec

Browse files
JorjMcKiejulian-smith-artifex-com
authored andcommitted
Support documents with Archives
Allow "archives" parameters in Document creation.
1 parent de8641f commit 30f86ec

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

src/__init__.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,7 @@ def __getitem__(self, i=0):
28972897
raise IndexError(f"page {i} not in document")
28982898
return self.load_page(i)
28992899

2900-
def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0, height=0, fontsize=11):
2900+
def __init__(self, filename=None, stream=None, filetype=None, archive=None, rect=None, width=0, height=0, fontsize=11):
29012901
"""Creates a document. Use 'open' as a synonym.
29022902

29032903
Notes:
@@ -2943,7 +2943,17 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29432943

29442944
self._name = filename
29452945
self.stream = stream
2946-
2946+
if isinstance(archive, pathlib.Path):
2947+
archive = Archive(archive.name)
2948+
elif isinstance(archive, str):
2949+
archive = Archive(archive)
2950+
if archive and not isinstance(archive, Archive):
2951+
raise TypeError(f"bad archive: {type(archive)=}.")
2952+
if archive:
2953+
archive_parm = archive.this # pass this to open
2954+
else:
2955+
archive = Archive()
2956+
archive_parm = archive.this
29472957
if stream is not None:
29482958
if filename is not None and filetype is None:
29492959
# 2025-05-06: Use <filename> as the filetype. This is
@@ -2958,6 +2968,8 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29582968
stream = stream.getvalue()
29592969
else:
29602970
raise TypeError(f"bad stream: {type(stream)=}.")
2971+
2972+
# this prevents bad things if original goes out of existence:
29612973
self.stream = stream
29622974

29632975
assert isinstance(stream, (bytes, memoryview))
@@ -2967,9 +2979,9 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29672979
# raise a specific exception.
29682980
raise EmptyFileError('Cannot open empty stream.')
29692981

2970-
stream2 = mupdf.fz_open_memory(mupdf.python_buffer_data(stream), len(stream))
2982+
fz_stream = mupdf.fz_open_memory(mupdf.python_buffer_data(stream), len(stream))
29712983
try:
2972-
doc = mupdf.fz_open_document_with_stream(filetype if filetype else '', stream2)
2984+
doc = mupdf.fz_open_document_with_stream_and_dir(filetype if filetype else '', fz_stream, archive_parm)
29732985
except Exception as e:
29742986
if g_exceptions_verbose > 1: exception_info()
29752987
raise FileDataError('Failed to open stream') from e
@@ -2996,20 +3008,15 @@ def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0
29963008
raise EmptyFileError(f'Cannot open empty file: {filename=}.')
29973009

29983010
if filetype:
2999-
# Override the type implied by <filename>. MuPDF does not
3000-
# have a way to do this directly so we open via a stream.
3001-
try:
3002-
fz_stream = mupdf.fz_open_file(filename)
3003-
doc = mupdf.fz_open_document_with_stream(filetype, fz_stream)
3004-
except Exception as e:
3005-
if g_exceptions_verbose > 1: exception_info()
3006-
raise FileDataError(f'Failed to open file {filename!r} as type {filetype!r}.') from e
3011+
suffix = filetype
30073012
else:
3008-
try:
3009-
doc = mupdf.fz_open_document(filename)
3010-
except Exception as e:
3011-
if g_exceptions_verbose > 1: exception_info()
3012-
raise FileDataError(f'Failed to open file {filename!r}.') from e
3013+
suffix = pathlib.Path(filename).suffix
3014+
try:
3015+
fz_stream = mupdf.fz_open_file(filename)
3016+
doc = mupdf.fz_open_document_with_stream_and_dir(suffix, fz_stream, archive_parm)
3017+
except Exception as e:
3018+
if g_exceptions_verbose > 1: exception_info()
3019+
raise FileDataError(f'Failed to open file {filename!r} as type {suffix}.') from e
30133020

30143021
else:
30153022
pdf = mupdf.PdfDocument()

0 commit comments

Comments
 (0)