-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.py
78 lines (58 loc) · 2.34 KB
/
app.py
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
from flask import Flask, Response, request
from werkzeug import secure_filename
from paste.translogger import TransLogger
import pdfkit
import cherrypy
app = Flask(__name__)
tmpfolder = "/tmp/"
@app.route("/pdf", methods=['POST'])
def pdf():
config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')
doc = handle_request(config)
return Response(doc, mimetype='application/pdf')
@app.route("/jpg", methods=['POST'])
def jpg():
config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltoimage')
doc = handle_request(config)
return Response(doc, mimetype='image/jpg')
def handle_request(config):
# We are getting the url to generate from a form parameter
options = {}
options = request.values.getlist('options', type=float)
print(options)
# Converting post options group to dictionary
listname = 'options'
options = dict()
for key, value in request.form.items():
if key[:len(listname)] == listname:
options[key[len(listname)+1:-1]] = value
if ('url' in request.form):
print("URL provided: " + request.form['url'])
pdf = pdfkit.from_url(str(request.form['url']), output_path=False, configuration=config, options=options)
if ('html' in request.form):
print("Html provided")
pdf = pdfkit.from_string(unicode(request.form['html']), output_path=False, configuration=config, options=options)
# If we are receiving the html contents from a uploaded file
elif ('content' in request.files):
print("File provided: " + str(request.files['content']))
f = request.files['content']
f.save(tmpfolder + secure_filename(f.filename))
pdf = pdfkit.from_file(tmpfolder + secure_filename(f.filename), output_path=False, configuration=config, options=options)
return pdf
def run_server():
# Enable WSGI access logging via Paste
app_logged = TransLogger(app)
# Mount the WSGI callable object (app) on the root directory
cherrypy.tree.graft(app_logged, '/')
# Set the configuration of the web server
cherrypy.config.update({
'engine.autoreload_on': True,
'log.screen': True,
'server.socket_port': 80,
'server.socket_host': '0.0.0.0'
})
# Start the CherryPy WSGI web server
cherrypy.engine.start()
cherrypy.engine.block()
if __name__ == "__main__":
run_server()