-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun.py
More file actions
59 lines (42 loc) · 1.29 KB
/
run.py
File metadata and controls
59 lines (42 loc) · 1.29 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
# coding: utf-8
import logging
import flask
import flask_config
import phantom
app = flask.Flask(__name__)
app.static_folder = "public"
app.SEND_FILE_MAX_AGE_DEFAULT = 0
@app.route('/')
def pdf():
"""Returns html that is useful for understanding, debugging and extending
the charting API"""
return file('public/pdf.html').read()
@app.route('/html-to-pdf', methods=['POST'])
def html_to_pdf():
"""Takes an HTTP POST with html, renders a pdf using phantomjs and returns
a pdf.
Example use with jQuery:
$.post('/html-to-pdf', {'html': HTML})
"""
html = flask.request.form.get('html', '')
title = 'html2pdf'
try:
pdf_data = phantom.html_to_pdf(html)
except OSError as e:
# Reraise the error so flask can log it
raise e
if pdf_data:
response = flask.make_response(pdf_data)
response.headers['Cache-Control'] = 'no-cache'
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = "attachment;filename=%s.pdf"%title
return response
else:
flask.abort(400)
if __name__ == '__main__':
# Set up logging to stdout, which ends up in Heroku logs
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.WARNING)
app.logger.addHandler(stream_handler)
app.debug = True
app.run(host='0.0.0.0', port=flask_config.port)