-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgotenberg.py
More file actions
155 lines (121 loc) · 4.6 KB
/
gotenberg.py
File metadata and controls
155 lines (121 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import decimal, json
import requests
from requests.exceptions import RequestException
from requests_toolbelt.multipart.encoder import MultipartEncoder
class A4(object):
WIDTH = 8.3
HEIGHT = 11.7
class API(object):
def __init__(self, schema : str='http', host : str='localhost', port : int=3000, root_path : str='/', debug : bool=False):
self.schema = schema
self.host = host
self.port = port
self.root_path = root_path
self.debug = debug
@property
def root_url(self) -> str:
return f'{self.schema}://{self.host}:{self.port}{self.root_path}'
def concat_endpoint(self, endpoint):
return f'{self.root_url}{endpoint}'
def send_request(self, endpoint, files : list=[], **kwargs):
url = self.concat_endpoint(endpoint)
return self.send_safe_request(url=url, files=files, debug=self.debug, **kwargs)
@classmethod
def send_safe_request(cls, url, files : list=[], debug : bool=False, **kwargs):
try:
fields = {}
for k, v in kwargs.items():
if isinstance(v, (float, decimal.Decimal, int)):
fields[k] = str(v)
else:
fields[k] = v
opened_file_list = { f'file-{i}' : ( f, open(f, 'rb') ) for i, f in enumerate(files) }
fields.update(opened_file_list)
rtn = cls._send_safe_request(url=url, **fields)
if debug:
print(f'Here is the data sent:')
print(rtn.request.body)
if rtn.status_code != 200:
raise RequestException('API returned non 200 code', response=rtn, request=rtn.request)
except:
raise
else:
return rtn
finally:
[ v[1].close() for v in opened_file_list.values() ]
@classmethod
def _send_safe_request(cls, url, **kwargs):
mp_encoder = MultipartEncoder(fields=kwargs)
return requests.post(
url=url,
data=mp_encoder,
headers={'Content-Type': mp_encoder.content_type}
)
class Gotenberg(API):
_CONVERT_ENDPOINT = 'convert'
HTML_ENDPOINT = f'{_CONVERT_ENDPOINT}/html'
URL_ENDPOINT = f'{_CONVERT_ENDPOINT}/url'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def convert_html(
self,
index_file_path : str,
header_file_path : str = None,
footer_file_path : str = None,
asset_files : list = [],
paperWidth : decimal.Decimal = A4.WIDTH,
paperHeight : decimal.Decimal = A4.HEIGHT,
marginTop : decimal.Decimal = 1,
marginBottom : decimal.Decimal = 1,
marginLeft : decimal.Decimal = 1,
marginRight : decimal.Decimal = 1,
landscape : bool=False,
scale : decimal.Decimal=1,
**kwargs,
) -> requests.Response:
file_list = [ index_file_path , ]
if header_file_path:
file_list.append(header_file_path)
if footer_file_path:
file_list.append(footer_file_path)
file_list.extend(asset_files)
kwargs.update(
paperWidth=paperWidth,
paperHeight=paperHeight,
marginTop=marginTop,
marginBottom=marginBottom,
marginLeft=marginLeft,
marginRight=marginRight,
landscape=landscape,
scale=scale,
)
return self._convert_html(file_list=file_list, **kwargs)
def _convert_html(self, file_list : list, **kwargs) -> requests.Response:
return self.send_request(endpoint=self.HTML_ENDPOINT, files=file_list, **kwargs)
def convert_url(
self,
remoteURL : str,
paperWidth : decimal.Decimal = A4.WIDTH,
paperHeight : decimal.Decimal = A4.HEIGHT,
marginTop : decimal.Decimal = 1,
marginBottom : decimal.Decimal = 1,
marginLeft : decimal.Decimal = 1,
marginRight : decimal.Decimal = 1,
landscape : bool=False,
scale : decimal.Decimal=1,
**kwargs,
) -> requests.Response:
kwargs.update(
remoteURL=remoteURL,
paperWidth=paperWidth,
paperHeight=paperHeight,
marginTop=marginTop,
marginBottom=marginBottom,
marginLeft=marginLeft,
marginRight=marginRight,
landscape=landscape,
scale=scale,
)
return self._convert_url(**kwargs)
def _convert_url(self, **kwargs) -> requests.Response:
return self.send_request(endpoint=self.URL_ENDPOINT, **kwargs)