Skip to content

Commit 79860be

Browse files
committed
WIP
1 parent 3343e04 commit 79860be

10 files changed

Lines changed: 153 additions & 6 deletions

File tree

.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 }}
File renamed without changes.

.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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import subprocess
99
import sys
10+
import tempfile
1011
import zipfile
1112
from datetime import datetime
1213
from pathlib import Path
@@ -20,7 +21,7 @@
2021
from selenium.webdriver.chrome.service import Service
2122
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
2223

23-
__version__ = "0.0.14"
24+
__version__ = "0.0.15a2"
2425

2526
PATH_TO_HTML2PDF_JS = os.path.join(
2627
os.path.dirname(os.path.join(__file__)), "html2pdf_js", "html2pdf.min.js"
@@ -284,7 +285,7 @@ class Done(Exception):
284285
print( # noqa: T201
285286
"error: could not receive a successful completion status from HTML2PDF."
286287
)
287-
sys.exit(1)
288+
# sys.exit(1)
288289

289290
print("html2print: JS logs from the print session:") # noqa: T201
290291
print('"""') # noqa: T201
@@ -314,13 +315,14 @@ def create_webdriver(
314315
path_to_chrome = chromedriver
315316
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
316317

317-
service = Service(path_to_chrome)
318+
service = Service(path_to_chrome, log_output="/tmp/chromedriver.log")
318319

319320
webdriver_options = Options()
320321
webdriver_options.add_argument("start-maximized")
321322
webdriver_options.add_argument("disable-infobars")
323+
webdriver_options.add_argument('--disable-gpu')
322324
webdriver_options.add_argument("--disable-extensions")
323-
webdriver_options.add_argument("--headless")
325+
# webdriver_options.add_argument("--headless=chrome")
324326
# FIXME: This is not nice but otherwise it does not work in Ubuntu 24-based Docker image.
325327
# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2689287561
326328
webdriver_options.add_argument("--no-sandbox")

tasks.py

Lines changed: 54 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,55 @@ 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(context, image: str = "html2print:latest", no_cache: bool = False, source="pypi"):
275+
no_cache_argument = "--no-cache" if no_cache else ""
276+
run_invoke(
277+
context,
278+
f"""
279+
docker build .
280+
--build-arg HTML2PRINT_SOURCE={source}
281+
-t {image}
282+
{no_cache_argument}
283+
"""
284+
)
285+
286+
287+
@task(aliases=["rd"])
288+
def run_docker(context, image: str = "html2print:latest", command: Optional[str] = None):
289+
command_argument = f'/bin/bash -c "{command}"' if command is not None else ""
290+
entry_point_argument = '--entrypoint=""' if command_argument else ""
291+
292+
run_invoke(
293+
context,
294+
f"""
295+
docker run
296+
--name html2print
297+
--rm
298+
-it
299+
-v "$(pwd):/data"
300+
{entry_point_argument}
301+
{image}
302+
{command_argument}
303+
""",
304+
pty=True
305+
)
306+
307+
308+
@task(aliases=["td"])
309+
def test_docker(context, image: str = "html2print:latest"):
310+
run_invoke(
311+
context,
312+
f"""
313+
mkdir -p output/ && chmod 777 output/
314+
"""
315+
)
316+
run_docker(
317+
context,
318+
image=image,
319+
command=(
320+
"cd tests/integration/01_hello_world && html2print print index.html /data/output/index.pdf && cat /tmp/chromedriver.log"
321+
)
322+
)

tests/integration/01_hello_world/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<meta charset="utf-8">
66
<title>Test page</title>
77
<script src="../../../submodules/html2pdf/dist/bundle.js"></script>
8+
<script>
9+
console.log("Hello world!");
10+
</script>
811
<link rel="stylesheet" href="../../../submodules/html2pdf/test/shared/css/main.css">
912
</head>
1013

0 commit comments

Comments
 (0)