-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_to_pdf.py
54 lines (40 loc) · 1.35 KB
/
html_to_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import logging
logger = logging.getLogger(__name__)
def _encode_html(html: str) -> str:
return base64.b64encode(html.encode("utf-8")).decode()
def to_pdf(html: str) -> bytes:
html_b64 = _encode_html(html)
chrome_options = build_chrome_options()
executable_path = os.environ.get("CHROMEDRIVER_PATH") or "/usr/bin/chromedriver"
output = b""
driver = None
try:
driver = webdriver.Chrome(
executable_path=executable_path, chrome_options=chrome_options
)
except Exception as e:
logger.error(e)
if driver:
try:
driver.get(f"data:text/html;base64,{html_b64}")
pdf = driver.execute_cdp_cmd("Page.printToPDF", {"printBackground": True})
except Exception as e:
logger.error(e)
else:
output = base64.b64decode(pdf["data"])
finally:
driver.quit()
return output
def build_chrome_options():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.binary_location = (
os.environ.get("CHROME_BINARY") or "/opt/google/chrome/chrome"
)
return chrome_options