-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (44 loc) · 1.75 KB
/
main.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
import os, time, tempfile
import moratab
from flask import Flask, request, render_template, make_response
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
new_pdf_filename = lambda: 'static/{}.pdf'.format(int(time.time()*100))
def to_pdf(html, output):
address = os.path.abspath(os.path.dirname(__file__))
with tempfile.NamedTemporaryFile(delete=True, suffix='.html', dir=address) as html_file:
html_file.write(html.encode('utf-8'))
html_file.flush()
filename = os.path.join(address, html_file.name)
# os.system('chromium-browser --headless --disable-gpu --print-to-pdf={} file://{}'.format(output, filename))
os.system('chrome-headless-render-pdf --chrome-option=--disable-web-security --include-background --url file://{} --pdf {}'.format(filename, output))
@app.route('/')
def main():
return 'Moratab Server!'
@app.route('/form')
def form():
return render_template('form.html')
@app.route('/html', methods=['POST'])
def html():
content = moratab.render(request.form['moratab'])
return render_template('main.html', content=content)
@app.route('/test')
def test():
pdf_file = new_pdf_filename()
to_pdf('Salam!', pdf_file)
response = make_response(open(pdf_file, 'rb').read())
response.content_type = 'application/pdf'
return response
@app.route('/pdf', methods=['POST'])
def pdf():
content = moratab.render(request.form['moratab'])
html = render_template('main.html', content=content)
pdf_file = new_pdf_filename()
to_pdf(html, pdf_file)
response = make_response(open(pdf_file, 'rb').read())
response.content_type = 'application/pdf'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Content-Disposition'] = 'attachment; filename="moratab.pdf"'
return response
if __name__ == '__main__':
app.run(debug=True)