From ee17c5c4c3b1648801bed74230011d9bc9e6288a Mon Sep 17 00:00:00 2001 From: Vitaly Bogomolov Date: Fri, 29 Aug 2025 08:59:10 +0400 Subject: [PATCH] save_bytes returns byte size of the filled object. --- history.txt | 2 ++ markdown_pdf/__init__.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/history.txt b/history.txt index 2b25cd8..65c6eb5 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,5 @@ ++ Allow to save result to file-like object. + 17.04.2025 ver.1.7 ------------------ diff --git a/markdown_pdf/__init__.py b/markdown_pdf/__init__.py index e234577..335bc60 100644 --- a/markdown_pdf/__init__.py +++ b/markdown_pdf/__init__.py @@ -116,8 +116,8 @@ def save(self, file_name: typing.Union[str, pathlib.Path]) -> None: doc.save(file_name) doc.close() - def save_bytes(self, bytesio: io.BytesIO) -> None: - """Save pdf to file-like object.""" + def save_bytes(self, bytesio: io.BytesIO) -> int: + """Save pdf to file-like object and return byte size of the filled object.""" doc = self._make_doc() pdf = _as_pdf_document(doc) @@ -150,3 +150,5 @@ def save_bytes(self, bytesio: io.BytesIO) -> None: out = JM_new_output_fileptr(bytesio) mupdf.pdf_write_document(pdf, out, opts) out.fz_close_output() + + return bytesio.getbuffer().nbytes