From 36429e971633858a96e5bca804860385898e2b5c Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Sun, 7 Sep 2025 17:32:50 +0200 Subject: [PATCH] feat: introduce --strict argument to raise an exception if printed page validation --- html2pdf4doc/html2pdf4doc.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/html2pdf4doc/html2pdf4doc.py b/html2pdf4doc/html2pdf4doc.py index dfc7f48..8a6e5ef 100644 --- a/html2pdf4doc/html2pdf4doc.py +++ b/html2pdf4doc/html2pdf4doc.py @@ -528,6 +528,16 @@ def main() -> None: f"{PATH_TO_CHROME_DRIVER_DEBUG_LOG}." ), ) + command_parser_print.add_argument( + "--strict", + action="store_true", + help=( + "Enables Strict mode. In this mode, the library always performs a " + "validation of printed pages and raise a runtime error if the " + "validation fails. Without the Strict mode enabled, only a warning " + "message is printed and the execution continues." + ), + ) command_parser_print.add_argument( "paths", nargs="+", help="Paths to input HTML file." ) @@ -587,12 +597,18 @@ def exit_handler() -> None: with measure_performance("html2pdf4doc: validating page count"): reader = PdfReader(path_to_output_pdf) if len(reader.pages) != page_count: - raise RuntimeError( - "Something went wrong with the printed page. " + error_message = ( + "An error occurred with the printed page. " f"Page count mismatch: " - f"PDF pages: {len(reader.pages)}, " - f"html2pdf4doc pages: {page_count}." + f"PDF pages = {len(reader.pages)}, " + f"html2pdf4doc pages = {page_count}. " + f"Please report this issue at: " + f"https://github.com/strictdoc-project/html2pdf4doc_python." ) + if args.strict: + raise RuntimeError(error_message) + else: + print("html2pdf4doc: warning: " + error_message) # noqa: T201 else: print("html2pdf4doc: unknown command.") # noqa: T201