Skip to content

Commit 53aeb41

Browse files
Completed Backend
1 parent 69b882b commit 53aeb41

File tree

7 files changed

+51
-0
lines changed

7 files changed

+51
-0
lines changed

certificate_generator/app/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
6+
from app import routes

certificate_generator/app/routes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from app import app
2+
from app.utils import generate_certificate
3+
from flask import render_template, request, send_file
4+
import os
5+
6+
@app.route('/', methods=['GET', 'POST'])
7+
def home():
8+
return render_template("certificate.html")
9+
10+
11+
@app.route('/download', methods=['GET', 'POST'])
12+
def download_certificate():
13+
if request.method == "POST":
14+
file_name = generate_certificate(request.form['name'])
15+
return render_template('download.html', file_name=file_name)
16+
17+
@app.route('/download_certificate', methods=['GET'])
18+
def method_name():
19+
if request.method == "GET":
20+
filename = request.args.get("filename")
21+
filepath = os.path.join("static/certificates/generated", filename)
22+
return send_file(filepath, as_attachment=True, cache_timeout=0, attachment_filename=filename)
379 KB
Loading
Binary file not shown.
363 KB
Loading

certificate_generator/app/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from PIL import Image, ImageFont, ImageDraw
2+
from app import app
3+
import os
4+
5+
def generate_certificate(name):
6+
template_dir = os.path.join(app.root_path, "static/certificates/template")
7+
template_filename = "template.png"
8+
9+
output_dir = os.path.join(app.root_path, "static",
10+
"certificates/generated/")
11+
output_filename = f"{name}.png"
12+
13+
img = Image.open(os.path.join(template_dir, template_filename))
14+
draw = ImageDraw.Draw(img)
15+
font = ImageFont.truetype(os.path.join(template_dir, "DroidSansMono.ttf"), 150)
16+
draw.text((1000, 1390), name, (0, 0, 0), font=font)
17+
18+
img.save(os.path.join(output_dir, output_filename))
19+
20+
return output_filename

certificate_generator/run.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from app import app
2+
3+
app.run(port=5000, debug=True)

0 commit comments

Comments
 (0)