|
| 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