Skip to content

Commit e6cda57

Browse files
authored
Print multiple documents with one command (#13)
* Rename the logs to "html2print: ..." * Print multiple documents with one command
1 parent a8e3ec1 commit e6cda57

File tree

7 files changed

+65
-20
lines changed

7 files changed

+65
-20
lines changed

.github/workflows/ci-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
- name: Install Google Chrome
2626
run: |
27-
choco install googlechrome --no-progress -y
27+
choco install googlechrome --no-progress -y --ignore-checksums
2828
shell: powershell
2929

3030
- name: Check Chrome Version

html2print/html2print.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from webdriver_manager.core.http import HttpClient
2424
from webdriver_manager.core.os_manager import OperationSystemManager
2525

26-
__version__ = "0.0.3"
26+
__version__ = "0.0.5"
2727

2828
DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".html2print", "chromedriver")
2929

@@ -40,7 +40,7 @@ def get(self, url, params=None, **kwargs) -> Response:
4040
last_error: Optional[Exception] = None
4141
for attempt in range(1, 3):
4242
print( # noqa: T201
43-
f"html2pdf: sending GET request attempt {attempt}: {url}"
43+
f"html2print: sending GET request attempt {attempt}: {url}"
4444
)
4545
try:
4646
return requests.get(url, params, timeout=(5, 5), **kwargs)
@@ -50,10 +50,10 @@ def get(self, url, params=None, **kwargs) -> Response:
5050
last_error = read_timeout_
5151
except Exception as exception_:
5252
raise AssertionError(
53-
"html2pdf: unknown exception", exception_
53+
"html2print: unknown exception", exception_
5454
) from None
5555
print( # noqa: T201
56-
f"html2pdf: "
56+
f"html2print: "
5757
f"failed to get response for URL: {url} with error: {last_error}"
5858
)
5959

@@ -83,24 +83,24 @@ def find_driver(self, driver: Driver):
8383
)
8484
if os.path.isfile(path_to_cached_chrome_driver):
8585
print( # noqa: T201
86-
f"html2pdf: ChromeDriver exists in the local cache: "
86+
f"html2print: ChromeDriver exists in the local cache: "
8787
f"{path_to_cached_chrome_driver}"
8888
)
8989
return path_to_cached_chrome_driver
9090
print( # noqa: T201
91-
f"html2pdf: ChromeDriver does not exist in the local cache: "
91+
f"html2print: ChromeDriver does not exist in the local cache: "
9292
f"{path_to_cached_chrome_driver}"
9393
)
9494
path_to_downloaded_chrome_driver = super().find_driver(driver)
9595
if path_to_downloaded_chrome_driver is None:
9696
print( # noqa: T201
97-
f"html2pdf: could not get a downloaded ChromeDriver: "
97+
f"html2print: could not get a downloaded ChromeDriver: "
9898
f"{path_to_cached_chrome_driver}"
9999
)
100100
return None
101101

102102
print( # noqa: T201
103-
f"html2pdf: saving chromedriver to StrictDoc's local cache: "
103+
f"html2print: saving chromedriver to StrictDoc's local cache: "
104104
f"{path_to_downloaded_chrome_driver} -> {path_to_cached_chrome_driver}"
105105
)
106106
Path(path_to_cached_chrome_driver_dir).mkdir(
@@ -116,7 +116,7 @@ def get_inches_from_millimeters(mm: float) -> float:
116116

117117

118118
def get_pdf_from_html(driver, url) -> bytes:
119-
print(f"html2pdf: opening URL with ChromeDriver: {url}") # noqa: T201
119+
print(f"html2print: opening URL with ChromeDriver: {url}") # noqa: T201
120120

121121
driver.get(url)
122122

@@ -170,7 +170,7 @@ class Done(Exception):
170170
)
171171
sys.exit(1)
172172

173-
print("html2pdf: JS logs from the print session:") # noqa: T201
173+
print("html2print: JS logs from the print session:") # noqa: T201
174174
print('"""') # noqa: T201
175175
for entry in logs:
176176
print(entry) # noqa: T201
@@ -179,7 +179,7 @@ class Done(Exception):
179179
#
180180
# Execute Print command with ChromeDriver.
181181
#
182-
print("html2pdf: executing print command with ChromeDriver.") # noqa: T201
182+
print("html2print: executing print command with ChromeDriver.") # noqa: T201
183183
result = driver.execute_cdp_cmd("Page.printToPDF", calculated_print_options)
184184

185185
data = base64.b64decode(result["data"])
@@ -205,7 +205,7 @@ def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str):
205205
path_to_chrome = get_chrome_driver(path_to_cache_dir)
206206
else:
207207
path_to_chrome = chromedriver
208-
print(f"html2pdf: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
208+
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
209209

210210
service = Service(path_to_chrome)
211211

@@ -223,7 +223,7 @@ def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str):
223223
# Enable the capturing of everything in JS console.
224224
webdriver_options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
225225

226-
print("html2pdf: creating ChromeDriver.", flush=True) # noqa: T201
226+
print("html2print: creating ChromeDriver.", flush=True) # noqa: T201
227227

228228
driver = webdriver.Chrome(
229229
options=webdriver_options,
@@ -295,7 +295,7 @@ def main():
295295
)
296296

297297
path_to_chrome = get_chrome_driver(path_to_cache_dir)
298-
print(f"html2pdf: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
298+
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
299299
sys.exit(0)
300300

301301
elif args.command == "print":
@@ -310,13 +310,13 @@ def main():
310310

311311
@atexit.register
312312
def exit_handler():
313-
print("html2pdf: exit handler: quitting the ChromeDriver.") # noqa: T201
313+
print("html2print: exit handler: quitting the ChromeDriver.") # noqa: T201
314314
driver.quit()
315315

316316
assert len(paths) % 2 == 0, (
317317
f"Expecting an even number of input/output path arguments: {paths}."
318318
)
319-
for current_pair_idx in range(0, 2, len(paths)):
319+
for current_pair_idx in range(0, len(paths), 2):
320320
path_to_input_html = paths[current_pair_idx]
321321
path_to_output_pdf = paths[current_pair_idx + 1]
322322

@@ -331,7 +331,7 @@ def exit_handler():
331331
with open(path_to_output_pdf, "wb") as f:
332332
f.write(pdf_bytes)
333333
else:
334-
print("html2pdf: unknown command.") # noqa: T201
334+
print("html2print: unknown command.") # noqa: T201
335335
sys.exit(1)
336336

337337

tests/integration/03_cache_dir_argument/test.itest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ RUN: %html2pdf print --cache-dir %S/Output/cache %S/index.html %S/Output/index.p
22
RUN: %check_exists --file "%S/Output/index.pdf"
33
RUN: python %S/test.py
44

5-
CHECK-RUN1: html2pdf: ChromeDriver does not exist in the local cache:
5+
CHECK-RUN1: html2print: ChromeDriver does not exist in the local cache:
66

77
RUN: %html2pdf print --cache-dir %S/Output/cache %S/index.html %S/Output/index.pdf | filecheck %s --dump-input=fail --check-prefix CHECK-RUN2
88
RUN: %check_exists --file "%S/Output/index.pdf"
99
RUN: python %S/test.py
1010

11-
CHECK-RUN2: html2pdf: ChromeDriver exists in the local cache:
11+
CHECK-RUN2: html2print: ChromeDriver exists in the local cache:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Test page</title>
7+
<script src="../../../submodules/html2pdf/dist/bundle.js"></script>
8+
<link rel="stylesheet" href="../../../submodules/html2pdf/test/shared/css/main.css">
9+
</head>
10+
11+
<body>
12+
<p>Hello world!</p>
13+
</body>
14+
15+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Test page</title>
7+
<script src="../../../submodules/html2pdf/dist/bundle.js"></script>
8+
<link rel="stylesheet" href="../../../submodules/html2pdf/test/shared/css/main.css">
9+
</head>
10+
11+
<body>
12+
<p>Hello world!</p>
13+
</body>
14+
15+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RUN: %html2pdf print %S/index1.html %S/Output/index1.pdf %S/index2.html %S/Output/index2.pdf
2+
3+
RUN: %check_exists --file "%S/Output/index1.pdf"
4+
RUN: %check_exists --file "%S/Output/index2.pdf"
5+
6+
RUN: python %S/test.py
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pypdf import PdfReader
2+
3+
reader = PdfReader("Output/index1.pdf")
4+
assert len(reader.pages) == 1
5+
assert reader.pages[0].extract_text() == "Hello world!"
6+
7+
reader = PdfReader("Output/index2.pdf")
8+
assert len(reader.pages) == 1
9+
assert reader.pages[0].extract_text() == "Hello world!"

0 commit comments

Comments
 (0)