Skip to content

Commit 9feb169

Browse files
authored
Wait with timeout until HTML2PDF is done processing (#2)
1 parent 5d180c9 commit 9feb169

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

hpdf/hpdf.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import os.path
66
import sys
77
import tempfile
8+
from datetime import datetime
89
from pathlib import Path
910
from shutil import copy
11+
from time import sleep
1012
from typing import Optional, List
1113

1214
import requests
@@ -148,9 +150,30 @@ def get_pdf_from_html(driver, url) -> bytes:
148150
print("HTML2PDF: executing print command with Chrome Driver.") # noqa: T201
149151
result = driver.execute_cdp_cmd("Page.printToPDF", calculated_print_options)
150152

153+
class Done(Exception): pass
154+
155+
datetime_start = datetime.today()
156+
157+
logs = None
158+
try:
159+
while True:
160+
logs = driver.get_log("browser")
161+
for entry_ in logs:
162+
if "HTML2PDF4DOC time" in entry_["message"]:
163+
print("success: HTML2PDF completed its job.")
164+
raise Done
165+
if (datetime.today() - datetime_start).total_seconds() > 60:
166+
raise TimeoutError
167+
sleep(0.5)
168+
except Done:
169+
pass
170+
except TimeoutError:
171+
print("error: could not receive a successful completion status from HTML2PDF.")
172+
sys.exit(1)
173+
151174
print("HTML2PDF: JS logs from the print session:") # noqa: T201
152175
print('"""') # noqa: T201
153-
for entry in driver.get_log("browser"):
176+
for entry in logs:
154177
print(entry) # noqa: T201
155178
print('"""') # noqa: T201
156179

0 commit comments

Comments
 (0)