-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (39 loc) · 1.42 KB
/
app.py
File metadata and controls
47 lines (39 loc) · 1.42 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
from flask import Flask,jsonify,request
import extractify as ext
from pdf2image import convert_from_path
from flask_cors import CORS
import base64
app = Flask(__name__)
CORS(app)
# To run python file without re-running the flask command
def before_request():
app.jinja_env.cache = {}
app.before_request(before_request)
# Routes
@app.route('/api',methods = ['GET','POST'])
def api():
if(request.method=="GET"):
json = request.json
return jsonify(ext.generate(json))
if(request.method=="POST"):
json = request.json
return jsonify(ext.generate(json))
@app.route('/pdf_converter',methods = ['POST'])
def pdftoimg():
if(request.method == "POST"):
pdf = request.files['pdf']
with open('./static/tempfiles/temp.pdf', 'wb+') as destination:
for chunk in pdf:
destination.write(chunk)
images = convert_from_path('./static/tempfiles/temp.pdf')
for i in range(len(images)):
# Save pages as images in the pdf
images[i].save('./static/tempfiles/temp' +
str(i) + '.jpg', 'JPEG')
with open("./static/tempfiles/temp0.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return jsonify({'status': str(encoded_string.decode("utf-8"))})
return jsonify({'status': 'failure'})
# init
if __name__ == '__main__':
app.run(debug=True)