Skip to content

Commit 64ad507

Browse files
committed
Initial external wkhtml implementation
1 parent 0d9a73a commit 64ad507

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

13.0.Dockerfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ EXPOSE 8069 8072
44

55
ARG GEOIP_UPDATER_VERSION=4.1.5
66
ARG MQT=https://github.com/OCA/maintainer-quality-tools.git
7-
ARG WKHTMLTOPDF_VERSION=0.12.5
8-
ARG WKHTMLTOPDF_CHECKSUM='dfab5506104447eef2530d1adb9840ee3a67f30caaad5e9bcb8743ef2f9421bd'
97
ENV DB_FILTER=.* \
108
DEPTH_DEFAULT=1 \
119
DEPTH_MERGE=100 \
@@ -39,10 +37,7 @@ ENV DB_FILTER=.* \
3937
RUN apt-get -qq update \
4038
&& apt-get install -yqq --no-install-recommends \
4139
curl \
42-
&& curl -SLo wkhtmltox.deb https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox_${WKHTMLTOPDF_VERSION}-1.buster_amd64.deb \
43-
&& echo "${WKHTMLTOPDF_CHECKSUM} wkhtmltox.deb" | sha256sum -c - \
4440
&& apt-get install -yqq --no-install-recommends \
45-
./wkhtmltox.deb \
4641
chromium \
4742
ffmpeg \
4843
fonts-liberation2 \
@@ -69,6 +64,7 @@ RUN apt-get -qq update \
6964

7065
WORKDIR /opt/odoo
7166
COPY bin/* /usr/local/bin/
67+
COPY lib/kwkhtmltopdf_client.py /usr/local/bin/wkhtmltopdf
7268
COPY lib/doodbalib /usr/local/lib/python3.6/site-packages/doodbalib
7369
COPY build.d common/build.d
7470
COPY conf.d common/conf.d

15.0.Dockerfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ FROM python:3.8-slim-bullseye AS base
33
EXPOSE 8069 8072
44

55
ARG GEOIP_UPDATER_VERSION=4.3.0
6-
ARG WKHTMLTOPDF_VERSION=0.12.5
7-
ARG WKHTMLTOPDF_CHECKSUM='dfab5506104447eef2530d1adb9840ee3a67f30caaad5e9bcb8743ef2f9421bd'
86
ENV DB_FILTER=.* \
97
DEPTH_DEFAULT=1 \
108
DEPTH_MERGE=100 \
@@ -36,10 +34,7 @@ ENV DB_FILTER=.* \
3634
RUN apt-get -qq update \
3735
&& apt-get install -yqq --no-install-recommends \
3836
curl \
39-
&& curl -SLo wkhtmltox.deb https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox_${WKHTMLTOPDF_VERSION}-1.buster_amd64.deb \
40-
&& echo "${WKHTMLTOPDF_CHECKSUM} wkhtmltox.deb" | sha256sum -c - \
4137
&& apt-get install -yqq --no-install-recommends \
42-
./wkhtmltox.deb \
4338
chromium \
4439
ffmpeg \
4540
fonts-liberation2 \
@@ -64,6 +59,7 @@ RUN apt-get -qq update \
6459

6560
WORKDIR /opt/odoo
6661
COPY bin/* /usr/local/bin/
62+
COPY lib/kwkhtmltopdf_client.py /usr/local/bin/wkhtmltopdf
6763
COPY lib/doodbalib /usr/local/lib/python3.8/site-packages/doodbalib
6864
COPY build.d common/build.d
6965
COPY conf.d common/conf.d

lib/kwkhtmltopdf_client.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2019 ACSONE SA/NV
3+
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
4+
5+
from __future__ import print_function
6+
7+
import os
8+
import sys
9+
10+
import requests
11+
12+
CHUNK_SIZE = 2 ** 16
13+
14+
15+
class Error(Exception):
16+
pass
17+
18+
19+
class UsageError(Error):
20+
pass
21+
22+
23+
class ServerError(Error):
24+
pass
25+
26+
27+
def wkhtmltopdf(args):
28+
url = os.getenv("KWKHTMLTOPDF_SERVER_URL")
29+
30+
if url == "":
31+
raise UsageError("KWKHTMLTOPDF_SERVER_URL not set")
32+
elif url == "MOCK":
33+
print("wkhtmltopdf 0.12.5 (mock)")
34+
return
35+
36+
parts = []
37+
38+
def add_option(option):
39+
# TODO option encoding?
40+
parts.append(("option", (None, option)))
41+
42+
def add_file(filename):
43+
with open(filename, "rb") as f:
44+
parts.append(("file", (filename, f.read())))
45+
46+
if "-" in args:
47+
raise UsageError("stdin/stdout input is not implemented")
48+
49+
output = "-"
50+
if len(args) >= 2 and not args[-1].startswith("-") and not args[-2].startswith("-"):
51+
output = args[-1]
52+
args = args[:-1]
53+
54+
for arg in args:
55+
if arg.startswith("-"):
56+
add_option(arg)
57+
elif arg.startswith("http://") or arg.startswith("https://"):
58+
add_option(arg)
59+
elif arg.startswith("file://"):
60+
add_file(arg[7:])
61+
elif os.path.isfile(arg):
62+
# TODO better way to detect args that are actually options
63+
# TODO in case an option has the same name as an existing file
64+
# TODO only way I see so far is enumerating them in a static
65+
# TODO datastructure (that can be initialized with a quick parse
66+
# TODO of wkhtmltopdf --extended-help)
67+
add_file(arg)
68+
else:
69+
add_option(arg)
70+
71+
if not parts:
72+
add_option("-h")
73+
74+
try:
75+
r = requests.post(url, files=parts)
76+
r.raise_for_status()
77+
78+
if output == "-":
79+
if sys.version_info[0] < 3:
80+
out = sys.stdout
81+
else:
82+
out = sys.stdout.buffer
83+
else:
84+
out = open(output, "wb")
85+
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
86+
out.write(chunk)
87+
except requests.exceptions.ChunkedEncodingError:
88+
# TODO look if client and server could use trailer headers
89+
# TODO to report errors
90+
raise ServerError("kwkhtmltopdf server error, consult server log")
91+
92+
93+
if __name__ == "__main__":
94+
try:
95+
wkhtmltopdf(sys.argv[1:])
96+
except Error as e:
97+
print(e, file=sys.stderr)
98+
sys.exit(-1)

0 commit comments

Comments
 (0)