1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Creating PDF for Image files with Web GUI |
3
|
|
|
""" |
4
|
|
|
from flask import Flask, request, make_response, jsonify, render_template |
5
|
|
|
import os |
6
|
|
|
import sys |
7
|
|
|
import werkzeug |
8
|
|
|
from ebook_homebrew.convert import Image2PDF |
9
|
|
|
|
10
|
|
|
app = Flask(__name__) |
11
|
|
|
|
12
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 |
13
|
|
|
|
14
|
|
|
UPLOAD_DIR = "." |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
@app.route("/") |
18
|
|
|
def index(): |
19
|
|
|
"""Index Page |
20
|
|
|
""" |
21
|
|
|
return render_template("index.html") |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@app.route("/upload") |
25
|
|
|
def uploader(): |
26
|
|
|
"""Upload GUI |
27
|
|
|
""" |
28
|
|
|
return render_template("upload.html") |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@app.route("/data/upload", methods=["POST"]) |
32
|
|
|
def create_pdf(): |
33
|
|
|
"""Create PDF input images |
34
|
|
|
""" |
35
|
|
|
digits = "" |
36
|
|
|
extension = "" |
37
|
|
|
|
38
|
|
|
if "digits" in request.form: |
39
|
|
|
digits = str(request.form["digits"]) |
40
|
|
|
if "extension" in request.form: |
41
|
|
|
extension = str(request.form["extension"]) |
42
|
|
|
if "uploadFile" not in request.files: |
43
|
|
|
make_response(jsonify({"result": "uploadFile is required."})) |
44
|
|
|
|
45
|
|
|
sys.stderr.write("digits = " + digits + "\n") |
46
|
|
|
upload_files = request.files.getlist("uploadFiles") |
47
|
|
|
response = make_response() |
48
|
|
|
for file in upload_files: |
49
|
|
|
file_name = file.filename |
50
|
|
|
sys.stderr.write("fileName = " + file_name + "\n") |
51
|
|
|
save_file_name = werkzeug.utils.secure_filename(file_name) |
52
|
|
|
file.save(os.path.join(UPLOAD_DIR, save_file_name)) |
53
|
|
|
converter = Image2PDF(digits, extension, UPLOAD_DIR) |
54
|
|
|
converter.make_pdf("result.pdf", True) |
55
|
|
|
|
56
|
|
|
with open("result.pdf", "rb") as f: |
57
|
|
|
response.data = f.read() |
58
|
|
|
downloadFileName = "result.pdf" |
59
|
|
|
response.headers["Content-Disposition"] = "attachment; filename=" + downloadFileName |
60
|
|
|
response.mimetype = "application/pdf" |
61
|
|
|
return response |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
@app.errorhandler(werkzeug.exceptions.RequestEntityTooLarge) |
65
|
|
|
def handle_over_max_file_size(error): |
66
|
|
|
""" |
67
|
|
|
Args: |
68
|
|
|
error: |
69
|
|
|
|
70
|
|
|
Returns: |
71
|
|
|
|
72
|
|
|
""" |
73
|
|
|
print("werkzeug.exceptions.RequestEntityTooLarge" + error) |
74
|
|
|
return 'result : file size is overed.' |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
if __name__ == "__main__": |
78
|
|
|
print(app.url_map) |
79
|
|
|
app.run(host="0.0.0.0", port=8080) |
80
|
|
|
|