diff --git a/docs/how-to-open-a-file.rst b/docs/how-to-open-a-file.rst index 26bde98d0..9a69d5004 100644 --- a/docs/how-to-open-a-file.rst +++ b/docs/how-to-open-a-file.rst @@ -87,6 +87,34 @@ For further examples which deal with files held on typical cloud services please +---------- + + +Opening Django Files +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Django implements a `File Storage API `_ to store files. The default is the `FileSystemStorage `_, but the `django-storages `_ library provides a number of other storage backends. + +You can open the file, move the contents into memory, then pass the contents to |PyMuPDF| as a stream. + +.. code-block:: python + + import pymupdf + from django.core.files.storage import default_storage + + from .models import MyModel + + obj = MyModel.objects.get(id=1) + with default_storage.open(obj.file.name) as f: + data = f.read() + + doc = pymupdf.Document(stream=data) + +Please note that if the file you open is large, you may run out of memory. + +The File Storage API works well if you're using different storage backends in different environments. If you're only using the `FileSystemStorage`, you can simply use the `obj.file.name` to open the file directly with |PyMuPDF| as shown in an earlier example. + + ----------