MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / __init__

Method __init__

src/__init__.py:2895–3046  ·  view source on GitHub ↗

Creates a document. Use 'open' as a synonym. Notes: Basic usages: open() - new PDF document open(filename) - string or pathlib.Path, must have supported file extension. open(type, buffer) - type: valid extension, buffer: by

(self, filename=None, stream=None, filetype=None, rect=None, width=0, height=0, fontsize=11)

Source from the content-addressed store, hash-verified

2893 return self.load_page(i)
2894
2895 def __init__(self, filename=None, stream=None, filetype=None, rect=None, width=0, height=0, fontsize=11):
2896 """Creates a document. Use 'open' as a synonym.
2897
2898 Notes:
2899 Basic usages:
2900 open() - new PDF document
2901 open(filename) - string or pathlib.Path, must have supported
2902 file extension.
2903 open(type, buffer) - type: valid extension, buffer: bytes object.
2904 open(stream=buffer, filetype=type) - keyword version of previous.
2905 open(filename, fileype=type) - filename with unrecognized extension.
2906 rect, width, height, fontsize: layout reflowable document
2907 on open (e.g. EPUB). Ignored if n/a.
2908 """
2909 # We temporarily set JM_mupdf_show_errors=0 while we are constructing,
2910 # then restore its original value in a `finally:` block.
2911 #
2912 global JM_mupdf_show_errors
2913 JM_mupdf_show_errors_old = JM_mupdf_show_errors
2914 JM_mupdf_show_errors = 0
2915
2916 try:
2917 self.is_closed = False
2918 self.is_encrypted = False
2919 self.is_encrypted = False
2920 self.metadata = None
2921 self.FontInfos = []
2922 self.Graftmaps = {}
2923 self.ShownPages = {}
2924 self.InsertedImages = {}
2925 self._page_refs = weakref.WeakValueDictionary()
2926 if isinstance(filename, mupdf.PdfDocument):
2927 pdf_document = filename
2928 self.this = pdf_document
2929 self.this_is_pdf = True
2930 return
2931
2932 w = width
2933 h = height
2934 r = JM_rect_from_py(rect)
2935 if not mupdf.fz_is_infinite_rect(r):
2936 w = r.x1 - r.x0
2937 h = r.y1 - r.y0
2938
2939 self._name = filename
2940 self.stream = stream
2941
2942 if stream is not None:
2943 if filename is not None and filetype is None:
2944 # 2025-05-06: Use <filename> as the filetype. This is
2945 # reversing precedence - we used to use <filename> if both
2946 # were set.
2947 filetype = filename
2948 if isinstance(stream, (bytes, memoryview)):
2949 pass
2950 elif isinstance(stream, bytearray):
2951 stream = bytes(stream)
2952 elif isinstance(stream, io.BytesIO):

Callers

nothing calls this directly

Calls 8

init_docMethod · 0.95
convert_to_pdfMethod · 0.95
JM_rect_from_pyFunction · 0.85
EmptyFileErrorClass · 0.85
exception_infoFunction · 0.85
FileDataErrorClass · 0.85
FileNotFoundErrorClass · 0.85
gen_idMethod · 0.80

Tested by

no test coverage detected