This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Creating PDF for Image files with Web GUI | ||
""" | ||
from flask import Flask, request, make_response, jsonify, render_template | ||
import os | ||
import sys | ||
import werkzeug | ||
from ebook_homebrew.convert import Image2PDF | ||
|
||
app = Flask(__name__) | ||
|
||
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 | ||
|
||
UPLOAD_DIR = "." | ||
|
||
|
||
@app.route("/") | ||
def index(): | ||
"""Index Page | ||
""" | ||
return render_template("index.html") | ||
|
||
|
||
@app.route("/upload") | ||
def uploader(): | ||
"""Upload GUI | ||
""" | ||
return render_template("upload.html") | ||
|
||
|
||
@app.route("/data/upload", methods=["POST"]) | ||
def create_pdf(): | ||
"""Create PDF input images | ||
""" | ||
digits = "" | ||
extension = "" | ||
|
||
if "digits" in request.form: | ||
digits = str(request.form["digits"]) | ||
if "extension" in request.form: | ||
extension = str(request.form["extension"]) | ||
if "uploadFile" not in request.files: | ||
make_response(jsonify({"result": "uploadFile is required."})) | ||
|
||
sys.stderr.write("digits = " + digits + "\n") | ||
upload_files = request.files.getlist("uploadFiles") | ||
response = make_response() | ||
for file in upload_files: | ||
file_name = file.filename | ||
sys.stderr.write("fileName = " + file_name + "\n") | ||
save_file_name = werkzeug.utils.secure_filename(file_name) | ||
file.save(os.path.join(UPLOAD_DIR, save_file_name)) | ||
converter = Image2PDF(digits, extension, UPLOAD_DIR) | ||
converter.make_pdf("result.pdf", True) | ||
|
||
with open("result.pdf", "rb") as f: | ||
response.data = f.read() | ||
downloadFileName = "result.pdf" | ||
response.headers["Content-Disposition"] = "attachment; filename=" + downloadFileName | ||
response.mimetype = "application/pdf" | ||
return response | ||
|
||
|
||
@app.errorhandler(werkzeug.exceptions.RequestEntityTooLarge) | ||
def handle_over_max_file_size(error): | ||
""" | ||
Args: | ||
error: | ||
Returns: | ||
""" | ||
print("werkzeug.exceptions.RequestEntityTooLarge" + error) | ||
return 'result : file size is overed.' | ||
|
||
|
||
if __name__ == "__main__": | ||
print(app.url_map) | ||
app.run(host='localhost', port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="ja"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="/static/css/style.css"> | ||
<link rel="stylesheet" href="/static/css/bootstrap.min.css"> | ||
<title>File</title> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="row"> | ||
{% block content %} | ||
{% endblock %} | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
<a href="upload">Create PDF</a> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
<form action="/data/upload" method="post" enctype="multipart/form-data"> | ||
<p>Img files: <input type="file" name="uploadFiles"/ multiple="multiple"></p> | ||
<p>Digits: <input type="text" name="digits"></p> | ||
<p>Extension: <input type="text" name="extension"></p> | ||
<input type="submit" value="submit"/> | ||
</form> | ||
{% endblock %} |