Skip to content

Commit 4d341d1

Browse files
authored
Update HTML2PDF.js to 0.2.1, add a testable and observable Dockerfile (#25)
1 parent 3343e04 commit 4d341d1

File tree

13 files changed

+246
-19
lines changed

13 files changed

+246
-19
lines changed

.github/workflows/docker.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: HTML2Print Docker CI
2+
3+
on:
4+
pull_request:
5+
branches: [ "**" ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Upgrade pip
15+
run: |
16+
python -m pip install --upgrade pip
17+
18+
- name: Install Python packages
19+
run: |
20+
pip install -r requirements.development.txt
21+
22+
- name: Build Docker image with PR branch
23+
run: |
24+
invoke build-docker \
25+
--image pr-${{ github.event.pull_request.number }} \
26+
--source=${{ github.event.pull_request.head.sha }}
27+
28+
- name: Run container and test StrictDoc installation
29+
run: |
30+
invoke test-docker \
31+
--image pr-${{ github.event.pull_request.number }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# HTML2PDF JS file.
2-
html2print/html2pdf_js/
2+
# html2print/html2pdf_js/
33

44
.idea/
55
**/.wdm/
66
build/
77
dist/
88
tests/integration/.lit_test_times.txt
99
tests/integration/**/Output/
10+
output/
1011

Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
FROM ubuntu:24.04
2+
3+
# Install dependencies
4+
RUN apt-get update && apt-get install -y \
5+
curl \
6+
git \
7+
python3 \
8+
python3-pip \
9+
python3-venv \
10+
sudo \
11+
vim \
12+
wget \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
# Download and install Google Chrome
16+
RUN wget -q -O google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
17+
&& apt-get update \
18+
&& apt-get install -y ./google-chrome.deb \
19+
&& rm google-chrome.deb
20+
21+
# Create a new non-root user and group.
22+
# NOTE: It is important that a non-root user is used because otherwise the
23+
# Chrome Driver fails with: "User data directory is already in use"
24+
# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2688613182
25+
RUN groupadd -r html2print && useradd -r -m -g html2print html2print
26+
27+
# Grant the new user sudo privileges.
28+
RUN echo "html2print ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/html2print
29+
30+
# Create a virtual environment in the user's home directory.
31+
RUN python3 -m venv /opt/venv
32+
33+
# Ensure the virtual environment is used by modifying the PATH.
34+
ENV PATH="/opt/venv/bin:$PATH"
35+
36+
# Install StrictDoc. Set default StrictDoc installation from PyPI but allow
37+
# overriding it with an environment variable.
38+
ARG HTML2PRINT_SOURCE="pypi"
39+
ENV HTML2PRINT_SOURCE=${HTML2PRINT_SOURCE}
40+
41+
RUN if [ "$HTML2PRINT_SOURCE" = "pypi" ]; then \
42+
pip install --no-cache-dir --upgrade pip && \
43+
pip install --no-cache-dir html2print; \
44+
else \
45+
pip install --no-cache-dir --upgrade pip && \
46+
pip install --no-cache-dir git+https://github.com/mettta/html2pdf_python.git@${HTML2PRINT_SOURCE}; \
47+
fi; \
48+
chmod -R 777 /opt/venv;
49+
50+
USER html2print
51+
52+
# Set the working directory to the user's home directory.
53+
WORKDIR /data
54+
55+
ENTRYPOINT ["/bin/bash"]

html2print/html2pdf_js/html2pdf.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

html2print/html2print.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
from selenium.webdriver.chrome.service import Service
2121
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
2222

23-
__version__ = "0.0.14"
23+
__version__ = "0.0.15a2"
2424

2525
PATH_TO_HTML2PDF_JS = os.path.join(
2626
os.path.dirname(os.path.join(__file__)), "html2pdf_js", "html2pdf.min.js"
2727
)
2828

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

31+
PATH_TO_CHROME_DRIVER_DEBUG_LOG = "/tmp/chromedriver.log"
32+
3133
# HTML2PDF.js prints unicode symbols to console. The following makes it work on
3234
# Windows which otherwise complains:
3335
# UnicodeEncodeError: 'charmap' codec can't encode characters in position 129-130: character maps to <undefined>
@@ -303,7 +305,10 @@ class Done(Exception):
303305

304306

305307
def create_webdriver(
306-
chromedriver: Optional[str], path_to_cache_dir: str, page_load_timeout: int
308+
chromedriver: Optional[str],
309+
path_to_cache_dir: str,
310+
page_load_timeout: int,
311+
debug: bool = False,
307312
) -> webdriver.Chrome:
308313
print("html2print: creating ChromeDriver service.", flush=True) # noqa: T201
309314
if chromedriver is None:
@@ -314,13 +319,20 @@ def create_webdriver(
314319
path_to_chrome = chromedriver
315320
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
316321

317-
service = Service(path_to_chrome)
322+
if debug:
323+
service = Service(
324+
path_to_chrome, log_output=PATH_TO_CHROME_DRIVER_DEBUG_LOG
325+
)
326+
else:
327+
service = Service(path_to_chrome)
318328

319329
webdriver_options = Options()
320330
webdriver_options.add_argument("start-maximized")
321331
webdriver_options.add_argument("disable-infobars")
332+
# Doesn't seem to be needed.
333+
# webdriver_options.add_argument('--disable-gpu') # noqa: ERA001
322334
webdriver_options.add_argument("--disable-extensions")
323-
webdriver_options.add_argument("--headless")
335+
webdriver_options.add_argument("--headless=chrome")
324336
# FIXME: This is not nice but otherwise it does not work in Ubuntu 24-based Docker image.
325337
# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2689287561
326338
webdriver_options.add_argument("--no-sandbox")
@@ -415,6 +427,14 @@ def main():
415427
"HTML file, load it, and let HTML2PDF.js finish its job."
416428
),
417429
)
430+
command_parser_print.add_argument(
431+
"--debug",
432+
action="store_true",
433+
help=(
434+
f"Enables ChromeDriver logging to a file: "
435+
f"{PATH_TO_CHROME_DRIVER_DEBUG_LOG}."
436+
),
437+
)
418438
command_parser_print.add_argument(
419439
"paths", nargs="+", help="Paths to input HTML file."
420440
)
@@ -442,7 +462,10 @@ def main():
442462
args.cache_dir if args.cache_dir is not None else DEFAULT_CACHE_DIR
443463
)
444464
driver = create_webdriver(
445-
args.chromedriver, path_to_cache_dir, page_load_timeout
465+
args.chromedriver,
466+
path_to_cache_dir,
467+
page_load_timeout,
468+
debug=args.debug,
446469
)
447470

448471
@atexit.register

tasks.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def run_invoke(
3030
cmd,
3131
environment: Optional[dict] = None,
3232
warn: bool = False,
33+
pty: bool = False,
3334
) -> invoke.runners.Result:
3435
def one_line_command(string):
3536
return re.sub("\\s+", " ", string).strip()
@@ -39,7 +40,7 @@ def one_line_command(string):
3940
env=environment,
4041
hide=False,
4142
warn=warn,
42-
pty=False,
43+
pty=pty,
4344
echo=True,
4445
)
4546

@@ -267,3 +268,64 @@ def release(context, test_pypi=False, username=None, password=None):
267268
{user_password}
268269
""",
269270
)
271+
272+
273+
@task(aliases=["bd"])
274+
def build_docker(
275+
context,
276+
image: str = "html2print:latest",
277+
no_cache: bool = False,
278+
source="pypi",
279+
):
280+
no_cache_argument = "--no-cache" if no_cache else ""
281+
run_invoke(
282+
context,
283+
f"""
284+
docker build .
285+
--build-arg HTML2PRINT_SOURCE={source}
286+
-t {image}
287+
{no_cache_argument}
288+
""",
289+
)
290+
291+
292+
@task(aliases=["rd"])
293+
def run_docker(
294+
context, image: str = "html2print:latest", command: Optional[str] = None
295+
):
296+
command_argument = (
297+
f'/bin/bash -c "{command}"' if command is not None else ""
298+
)
299+
entry_point_argument = '--entrypoint=""' if command_argument else ""
300+
301+
run_invoke(
302+
context,
303+
f"""
304+
docker run
305+
--name html2print
306+
--rm
307+
-it
308+
-v "$(pwd):/data"
309+
{entry_point_argument}
310+
{image}
311+
{command_argument}
312+
""",
313+
pty=True,
314+
)
315+
316+
317+
@task(aliases=["td"])
318+
def test_docker(context, image: str = "html2print:latest"):
319+
run_invoke(
320+
context,
321+
"""
322+
mkdir -p output/ && chmod 777 output/
323+
""",
324+
)
325+
run_docker(
326+
context,
327+
image=image,
328+
command=(
329+
"cd tests/integration/01_hello_world && html2print print --debug index.html /data/output/index.pdf && cat /tmp/chromedriver.log"
330+
),
331+
)

tests/integration/01_hello_world/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<head>
55
<meta charset="utf-8">
66
<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">
7+
<script src="../../../html2print/html2pdf_js/html2pdf.min.js"></script>
8+
<link rel="stylesheet" href="../../../tests/integration/main.css">
99
</head>
1010

1111
<body>

tests/integration/02_two_pages/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<head>
55
<meta charset="utf-8">
66
<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">
7+
<script src="../../../html2print/html2pdf_js/html2pdf.min.js"></script>
8+
<link rel="stylesheet" href="../../../tests/integration/main.css">
99
</head>
1010

1111
<body>

tests/integration/03_cache_dir_argument/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<head>
55
<meta charset="utf-8">
66
<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">
7+
<script src="../../../html2print/html2pdf_js/html2pdf.min.js"></script>
8+
<link rel="stylesheet" href="../../../tests/integration/main.css">
99
</head>
1010

1111
<body>

tests/integration/04_two_documents/index1.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<head>
55
<meta charset="utf-8">
66
<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">
7+
<script src="../../../html2print/html2pdf_js/html2pdf.min.js"></script>
8+
<link rel="stylesheet" href="../../../tests/integration/main.css">
99
</head>
1010

1111
<body>

0 commit comments

Comments
 (0)