|
1 |
| -from io import BytesIO, StringIO |
2 |
| -from contextlib import redirect_stdout |
3 |
| -import json |
| 1 | +# Standard library imports |
4 | 2 | import base64
|
| 3 | +import json |
| 4 | +import logging |
5 | 5 | import sys
|
| 6 | +from io import BytesIO, StringIO |
6 | 7 |
|
7 | 8 | # TODO chuang8511:
|
8 | 9 | # Deal with the import error when running the code in the docker container.
|
|
12 | 13 |
|
13 | 14 |
|
14 | 15 | if __name__ == "__main__":
|
15 |
| - json_str = sys.stdin.buffer.read().decode('utf-8') |
16 |
| - params = json.loads(json_str) |
17 |
| - display_image_tag = params["display-image-tag"] |
18 |
| - display_all_page_image = params["display-all-page-image"] |
19 |
| - pdf_string = params["PDF"] |
| 16 | + # Capture warnings and errors. These are printed to stderr by default, which |
| 17 | + # will prevent clients from unmarshalling the response. |
| 18 | + conversion_logs = StringIO() |
| 19 | + log_handler = logging.StreamHandler(conversion_logs) |
| 20 | + log_handler.setLevel(logging.WARNING) |
| 21 | + |
| 22 | + # Remove any existing handlers to avoid duplicate logging |
| 23 | + logging.getLogger().handlers = [] |
| 24 | + |
| 25 | + # Add the handler to capture warnings/errors |
| 26 | + logging.getLogger().addHandler(log_handler) |
| 27 | + |
| 28 | + json_str = sys.stdin.buffer.read().decode('utf-8') |
| 29 | + params = json.loads(json_str) |
| 30 | + display_image_tag = params["display-image-tag"] |
| 31 | + display_all_page_image = params["display-all-page-image"] |
| 32 | + pdf_string = params["PDF"] |
20 | 33 | if "resolution" in params and params["resolution"] != 0 and params["resolution"] != None:
|
21 | 34 | resolution = params["resolution"]
|
22 | 35 | else:
|
|
42 | 55 | else:
|
43 | 56 | pdf.pages = pdf.raw_pages[i*separator_number:(i+1)*separator_number]
|
44 | 57 |
|
45 |
| - conversion_logs = StringIO() |
46 |
| - with redirect_stdout(conversion_logs): |
47 |
| - pdf.preprocess() |
48 |
| - image_index = pdf.image_index |
49 |
| - result += pdf.execute() |
| 58 | + pdf.preprocess() |
| 59 | + image_index = pdf.image_index |
| 60 | + result += pdf.execute() |
50 | 61 |
|
51 | 62 | for image in pdf.base64_images:
|
52 | 63 | images.append(image)
|
|
75 | 86 | }
|
76 | 87 | print(json.dumps(output))
|
77 | 88 | except Exception as e:
|
78 |
| - print(json.dumps({"system_error": str(e)})) |
| 89 | + print(json.dumps({"system_error": str(e)}), file=sys.stderr) |
0 commit comments