From 74c467eaf712110523a0cbe5676f29b7969b3ecd Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Tue, 5 Mar 2024 11:48:38 +0200 Subject: [PATCH] conversion: Do not let PyMuPDF print to stdout PyMuPDF has some hardcoded log messages that print to stdout [1]. We don't have a way to silence them, because they don't use the Python logging infrastructure. What we can do here is silence a particular call that's been creating debug messages. For a long term solution, we have sent a PR to the PyMuPDF team, and we will follow up there [2]. Fixes #700 [1]: https://github.com/freedomofpress/dangerzone/issues/700 [2]: https://github.com/pymupdf/PyMuPDF/pull/3137 --- dangerzone/conversion/pixels_to_pdf.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dangerzone/conversion/pixels_to_pdf.py b/dangerzone/conversion/pixels_to_pdf.py index 42884a0..d69c7a3 100644 --- a/dangerzone/conversion/pixels_to_pdf.py +++ b/dangerzone/conversion/pixels_to_pdf.py @@ -6,7 +6,9 @@ Here are the steps, with progress bar percentages: - 95%-100%: Compress the final PDF """ import asyncio +import contextlib import glob +import io import json import os import shutil @@ -49,9 +51,14 @@ class PixelsToPDF(DangerzoneConverter): # The first few operations happen on a per-page basis. page_size = len(untrusted_rgb_data) total_size += page_size - pixmap = fitz.Pixmap( - fitz.Colorspace(fitz.CS_RGB), width, height, untrusted_rgb_data, False - ) + with contextlib.redirect_stdout(io.StringIO()): + pixmap = fitz.Pixmap( + fitz.Colorspace(fitz.CS_RGB), + width, + height, + untrusted_rgb_data, + False, + ) pixmap.set_dpi(DEFAULT_DPI, DEFAULT_DPI) if ocr_lang: # OCR the document self.update_progress(