paperap.resources.document_download module
Module for managing document download operations in the Paperless-NgX API.
This module provides functionality for downloading documents in various formats (original, preview, thumbnail) from a Paperless-NgX server. It handles the API requests, response parsing, and content extraction for document downloads.
- class paperap.resources.document_download.DownloadedDocumentResource(client)[source]
Bases:
StandardResource
[DownloadedDocument
,DownloadedDocumentQuerySet
]Resource for managing downloaded document content from Paperless-NgX.
This resource handles retrieving document files in various formats (original, preview, thumbnail) from the Paperless-NgX API. It provides methods to load binary content and associated metadata for documents.
- model_class
The DownloadedDocument model class used by this resource.
- queryset_class
The DownloadedDocumentQuerySet class for query operations.
- name
The resource name used in API endpoints.
- endpoints
Mapping of retrieval modes to their corresponding API endpoints.
- Parameters:
client (PaperlessClient)
- model_class
alias of
DownloadedDocument
- queryset_class
alias of
DownloadedDocumentQuerySet
- name: str = 'document'
- endpoints: ClassVar[Endpoints] = {RetrieveFileMode.DOWNLOAD: <string.Template object>, RetrieveFileMode.PREVIEW: <string.Template object>, RetrieveFileMode.THUMBNAIL: <string.Template object>}
- load(downloaded_document)[source]
Load the document file content from the API.
This method fetches the binary content of the document file from the Paperless-NgX API and updates the model with the response data. It handles different retrieval modes (download, preview, thumbnail) and parses response headers to extract metadata such as content type and filename.
- Parameters:
downloaded_document (
DownloadedDocument
) – The DownloadedDocument model to load content for. This model will be updated with the fetched content and metadata.- Raises:
ResourceNotFoundError – If the document cannot be retrieved from the API.
- Return type:
Example
# Get a document reference doc = client.documents.get(123)
# Create a download request download = client.document_downloads.create(
id=doc.id, mode=RetrieveFileMode.DOWNLOAD, original=True
)
# Load the actual content client.document_downloads.load(download)
# Now download.content contains the binary data with open(“my_document.pdf”, “wb”) as f:
f.write(download.content)