From 3d6fb4e473334364334545c165cf5a9a5c07c0b1 Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Mon, 3 Feb 2025 23:15:52 +0100 Subject: [PATCH 1/2] Rename the logs to "html2print: ..." --- html2print/html2print.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/html2print/html2print.py b/html2print/html2print.py index a97db90..2735ffa 100644 --- a/html2print/html2print.py +++ b/html2print/html2print.py @@ -23,7 +23,7 @@ from webdriver_manager.core.http import HttpClient from webdriver_manager.core.os_manager import OperationSystemManager -__version__ = "0.0.3" +__version__ = "0.0.4" DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".html2print", "chromedriver") @@ -40,7 +40,7 @@ def get(self, url, params=None, **kwargs) -> Response: last_error: Optional[Exception] = None for attempt in range(1, 3): print( # noqa: T201 - f"html2pdf: sending GET request attempt {attempt}: {url}" + f"html2print: sending GET request attempt {attempt}: {url}" ) try: return requests.get(url, params, timeout=(5, 5), **kwargs) @@ -50,10 +50,10 @@ def get(self, url, params=None, **kwargs) -> Response: last_error = read_timeout_ except Exception as exception_: raise AssertionError( - "html2pdf: unknown exception", exception_ + "html2print: unknown exception", exception_ ) from None print( # noqa: T201 - f"html2pdf: " + f"html2print: " f"failed to get response for URL: {url} with error: {last_error}" ) @@ -83,24 +83,24 @@ def find_driver(self, driver: Driver): ) if os.path.isfile(path_to_cached_chrome_driver): print( # noqa: T201 - f"html2pdf: ChromeDriver exists in the local cache: " + f"html2print: ChromeDriver exists in the local cache: " f"{path_to_cached_chrome_driver}" ) return path_to_cached_chrome_driver print( # noqa: T201 - f"html2pdf: ChromeDriver does not exist in the local cache: " + f"html2print: ChromeDriver does not exist in the local cache: " f"{path_to_cached_chrome_driver}" ) path_to_downloaded_chrome_driver = super().find_driver(driver) if path_to_downloaded_chrome_driver is None: print( # noqa: T201 - f"html2pdf: could not get a downloaded ChromeDriver: " + f"html2print: could not get a downloaded ChromeDriver: " f"{path_to_cached_chrome_driver}" ) return None print( # noqa: T201 - f"html2pdf: saving chromedriver to StrictDoc's local cache: " + f"html2print: saving chromedriver to StrictDoc's local cache: " f"{path_to_downloaded_chrome_driver} -> {path_to_cached_chrome_driver}" ) Path(path_to_cached_chrome_driver_dir).mkdir( @@ -116,7 +116,7 @@ def get_inches_from_millimeters(mm: float) -> float: def get_pdf_from_html(driver, url) -> bytes: - print(f"html2pdf: opening URL with ChromeDriver: {url}") # noqa: T201 + print(f"html2print: opening URL with ChromeDriver: {url}") # noqa: T201 driver.get(url) @@ -170,7 +170,7 @@ class Done(Exception): ) sys.exit(1) - print("html2pdf: JS logs from the print session:") # noqa: T201 + print("html2print: JS logs from the print session:") # noqa: T201 print('"""') # noqa: T201 for entry in logs: print(entry) # noqa: T201 @@ -179,7 +179,7 @@ class Done(Exception): # # Execute Print command with ChromeDriver. # - print("html2pdf: executing print command with ChromeDriver.") # noqa: T201 + print("html2print: executing print command with ChromeDriver.") # noqa: T201 result = driver.execute_cdp_cmd("Page.printToPDF", calculated_print_options) data = base64.b64decode(result["data"]) @@ -205,7 +205,7 @@ def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str): path_to_chrome = get_chrome_driver(path_to_cache_dir) else: path_to_chrome = chromedriver - print(f"html2pdf: ChromeDriver available at path: {path_to_chrome}") # noqa: T201 + print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201 service = Service(path_to_chrome) @@ -223,7 +223,7 @@ def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str): # Enable the capturing of everything in JS console. webdriver_options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) - print("html2pdf: creating ChromeDriver.", flush=True) # noqa: T201 + print("html2print: creating ChromeDriver.", flush=True) # noqa: T201 driver = webdriver.Chrome( options=webdriver_options, @@ -295,7 +295,7 @@ def main(): ) path_to_chrome = get_chrome_driver(path_to_cache_dir) - print(f"html2pdf: ChromeDriver available at path: {path_to_chrome}") # noqa: T201 + print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201 sys.exit(0) elif args.command == "print": @@ -310,7 +310,7 @@ def main(): @atexit.register def exit_handler(): - print("html2pdf: exit handler: quitting the ChromeDriver.") # noqa: T201 + print("html2print: exit handler: quitting the ChromeDriver.") # noqa: T201 driver.quit() assert len(paths) % 2 == 0, ( @@ -331,7 +331,7 @@ def exit_handler(): with open(path_to_output_pdf, "wb") as f: f.write(pdf_bytes) else: - print("html2pdf: unknown command.") # noqa: T201 + print("html2print: unknown command.") # noqa: T201 sys.exit(1) From 38f6880ce397fdd3e19bda791efb2df8af6bc368 Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Tue, 4 Feb 2025 21:56:45 +0100 Subject: [PATCH 2/2] Print multiple documents with one command --- .github/workflows/ci-windows.yml | 2 +- html2print/html2print.py | 4 ++-- .../integration/03_cache_dir_argument/test.itest | 4 ++-- tests/integration/04_two_documents/index1.html | 15 +++++++++++++++ tests/integration/04_two_documents/index2.html | 15 +++++++++++++++ tests/integration/04_two_documents/test.itest | 6 ++++++ tests/integration/04_two_documents/test.py | 9 +++++++++ 7 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/integration/04_two_documents/index1.html create mode 100644 tests/integration/04_two_documents/index2.html create mode 100644 tests/integration/04_two_documents/test.itest create mode 100644 tests/integration/04_two_documents/test.py diff --git a/.github/workflows/ci-windows.yml b/.github/workflows/ci-windows.yml index 939517f..092e5ae 100644 --- a/.github/workflows/ci-windows.yml +++ b/.github/workflows/ci-windows.yml @@ -24,7 +24,7 @@ jobs: - name: Install Google Chrome run: | - choco install googlechrome --no-progress -y + choco install googlechrome --no-progress -y --ignore-checksums shell: powershell - name: Check Chrome Version diff --git a/html2print/html2print.py b/html2print/html2print.py index 2735ffa..93edb66 100644 --- a/html2print/html2print.py +++ b/html2print/html2print.py @@ -23,7 +23,7 @@ from webdriver_manager.core.http import HttpClient from webdriver_manager.core.os_manager import OperationSystemManager -__version__ = "0.0.4" +__version__ = "0.0.5" DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".html2print", "chromedriver") @@ -316,7 +316,7 @@ def exit_handler(): assert len(paths) % 2 == 0, ( f"Expecting an even number of input/output path arguments: {paths}." ) - for current_pair_idx in range(0, 2, len(paths)): + for current_pair_idx in range(0, len(paths), 2): path_to_input_html = paths[current_pair_idx] path_to_output_pdf = paths[current_pair_idx + 1] diff --git a/tests/integration/03_cache_dir_argument/test.itest b/tests/integration/03_cache_dir_argument/test.itest index 4da85d4..c4db5fb 100644 --- a/tests/integration/03_cache_dir_argument/test.itest +++ b/tests/integration/03_cache_dir_argument/test.itest @@ -2,10 +2,10 @@ RUN: %html2pdf print --cache-dir %S/Output/cache %S/index.html %S/Output/index.p RUN: %check_exists --file "%S/Output/index.pdf" RUN: python %S/test.py -CHECK-RUN1: html2pdf: ChromeDriver does not exist in the local cache: +CHECK-RUN1: html2print: ChromeDriver does not exist in the local cache: RUN: %html2pdf print --cache-dir %S/Output/cache %S/index.html %S/Output/index.pdf | filecheck %s --dump-input=fail --check-prefix CHECK-RUN2 RUN: %check_exists --file "%S/Output/index.pdf" RUN: python %S/test.py -CHECK-RUN2: html2pdf: ChromeDriver exists in the local cache: +CHECK-RUN2: html2print: ChromeDriver exists in the local cache: diff --git a/tests/integration/04_two_documents/index1.html b/tests/integration/04_two_documents/index1.html new file mode 100644 index 0000000..b49f361 --- /dev/null +++ b/tests/integration/04_two_documents/index1.html @@ -0,0 +1,15 @@ + + + + + + Test page + + + + + +

Hello world!

+ + + diff --git a/tests/integration/04_two_documents/index2.html b/tests/integration/04_two_documents/index2.html new file mode 100644 index 0000000..b49f361 --- /dev/null +++ b/tests/integration/04_two_documents/index2.html @@ -0,0 +1,15 @@ + + + + + + Test page + + + + + +

Hello world!

+ + + diff --git a/tests/integration/04_two_documents/test.itest b/tests/integration/04_two_documents/test.itest new file mode 100644 index 0000000..c2c7388 --- /dev/null +++ b/tests/integration/04_two_documents/test.itest @@ -0,0 +1,6 @@ +RUN: %html2pdf print %S/index1.html %S/Output/index1.pdf %S/index2.html %S/Output/index2.pdf + +RUN: %check_exists --file "%S/Output/index1.pdf" +RUN: %check_exists --file "%S/Output/index2.pdf" + +RUN: python %S/test.py diff --git a/tests/integration/04_two_documents/test.py b/tests/integration/04_two_documents/test.py new file mode 100644 index 0000000..14cbf44 --- /dev/null +++ b/tests/integration/04_two_documents/test.py @@ -0,0 +1,9 @@ +from pypdf import PdfReader + +reader = PdfReader("Output/index1.pdf") +assert len(reader.pages) == 1 +assert reader.pages[0].extract_text() == "Hello world!" + +reader = PdfReader("Output/index2.pdf") +assert len(reader.pages) == 1 +assert reader.pages[0].extract_text() == "Hello world!"