Skip to content

Commit

Permalink
Initial external wkhtml implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
amon-ra committed Apr 7, 2022
1 parent 0d9a73a commit 64ad507
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
6 changes: 1 addition & 5 deletions 13.0.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ EXPOSE 8069 8072

ARG GEOIP_UPDATER_VERSION=4.1.5
ARG MQT=https://github.com/OCA/maintainer-quality-tools.git
ARG WKHTMLTOPDF_VERSION=0.12.5
ARG WKHTMLTOPDF_CHECKSUM='dfab5506104447eef2530d1adb9840ee3a67f30caaad5e9bcb8743ef2f9421bd'
ENV DB_FILTER=.* \
DEPTH_DEFAULT=1 \
DEPTH_MERGE=100 \
Expand Down Expand Up @@ -39,10 +37,7 @@ ENV DB_FILTER=.* \
RUN apt-get -qq update \
&& apt-get install -yqq --no-install-recommends \
curl \
&& curl -SLo wkhtmltox.deb https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox_${WKHTMLTOPDF_VERSION}-1.buster_amd64.deb \
&& echo "${WKHTMLTOPDF_CHECKSUM} wkhtmltox.deb" | sha256sum -c - \
&& apt-get install -yqq --no-install-recommends \
./wkhtmltox.deb \
chromium \
ffmpeg \
fonts-liberation2 \
Expand All @@ -69,6 +64,7 @@ RUN apt-get -qq update \

WORKDIR /opt/odoo
COPY bin/* /usr/local/bin/
COPY lib/kwkhtmltopdf_client.py /usr/local/bin/wkhtmltopdf
COPY lib/doodbalib /usr/local/lib/python3.6/site-packages/doodbalib
COPY build.d common/build.d
COPY conf.d common/conf.d
Expand Down
6 changes: 1 addition & 5 deletions 15.0.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ FROM python:3.8-slim-bullseye AS base
EXPOSE 8069 8072

ARG GEOIP_UPDATER_VERSION=4.3.0
ARG WKHTMLTOPDF_VERSION=0.12.5
ARG WKHTMLTOPDF_CHECKSUM='dfab5506104447eef2530d1adb9840ee3a67f30caaad5e9bcb8743ef2f9421bd'
ENV DB_FILTER=.* \
DEPTH_DEFAULT=1 \
DEPTH_MERGE=100 \
Expand Down Expand Up @@ -36,10 +34,7 @@ ENV DB_FILTER=.* \
RUN apt-get -qq update \
&& apt-get install -yqq --no-install-recommends \
curl \
&& curl -SLo wkhtmltox.deb https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox_${WKHTMLTOPDF_VERSION}-1.buster_amd64.deb \
&& echo "${WKHTMLTOPDF_CHECKSUM} wkhtmltox.deb" | sha256sum -c - \
&& apt-get install -yqq --no-install-recommends \
./wkhtmltox.deb \
chromium \
ffmpeg \
fonts-liberation2 \
Expand All @@ -64,6 +59,7 @@ RUN apt-get -qq update \

WORKDIR /opt/odoo
COPY bin/* /usr/local/bin/
COPY lib/kwkhtmltopdf_client.py /usr/local/bin/wkhtmltopdf
COPY lib/doodbalib /usr/local/lib/python3.8/site-packages/doodbalib
COPY build.d common/build.d
COPY conf.d common/conf.d
Expand Down
98 changes: 98 additions & 0 deletions lib/kwkhtmltopdf_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python
# Copyright (c) 2019 ACSONE SA/NV
# Distributed under the MIT License (http://opensource.org/licenses/MIT)

from __future__ import print_function

import os
import sys

import requests

CHUNK_SIZE = 2 ** 16


class Error(Exception):
pass


class UsageError(Error):
pass


class ServerError(Error):
pass


def wkhtmltopdf(args):
url = os.getenv("KWKHTMLTOPDF_SERVER_URL")

if url == "":
raise UsageError("KWKHTMLTOPDF_SERVER_URL not set")
elif url == "MOCK":
print("wkhtmltopdf 0.12.5 (mock)")
return

parts = []

def add_option(option):
# TODO option encoding?
parts.append(("option", (None, option)))

def add_file(filename):
with open(filename, "rb") as f:
parts.append(("file", (filename, f.read())))

if "-" in args:
raise UsageError("stdin/stdout input is not implemented")

output = "-"
if len(args) >= 2 and not args[-1].startswith("-") and not args[-2].startswith("-"):
output = args[-1]
args = args[:-1]

for arg in args:
if arg.startswith("-"):
add_option(arg)
elif arg.startswith("http://") or arg.startswith("https://"):
add_option(arg)
elif arg.startswith("file://"):
add_file(arg[7:])
elif os.path.isfile(arg):
# TODO better way to detect args that are actually options
# TODO in case an option has the same name as an existing file
# TODO only way I see so far is enumerating them in a static
# TODO datastructure (that can be initialized with a quick parse
# TODO of wkhtmltopdf --extended-help)
add_file(arg)
else:
add_option(arg)

if not parts:
add_option("-h")

try:
r = requests.post(url, files=parts)
r.raise_for_status()

if output == "-":
if sys.version_info[0] < 3:
out = sys.stdout
else:
out = sys.stdout.buffer
else:
out = open(output, "wb")
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
out.write(chunk)
except requests.exceptions.ChunkedEncodingError:
# TODO look if client and server could use trailer headers
# TODO to report errors
raise ServerError("kwkhtmltopdf server error, consult server log")


if __name__ == "__main__":
try:
wkhtmltopdf(sys.argv[1:])
except Error as e:
print(e, file=sys.stderr)
sys.exit(-1)

0 comments on commit 64ad507

Please sign in to comment.