Skip to content

Commit 6a37939

Browse files
committed
allow imgs to be shown in the pdfs
1 parent a6a31f6 commit 6a37939

File tree

4 files changed

+54
-74
lines changed

4 files changed

+54
-74
lines changed

front-end/download.js

-31
This file was deleted.

front-end/upload_success.html

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
!<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
@@ -70,13 +70,24 @@
7070
h1{
7171
color: green;
7272
}
73+
a {
74+
margin: .4em;
75+
padding: .5em;
76+
background-color: #007bff;
77+
color: white;
78+
text-decoration: none;
79+
border-radius: .2em;
80+
}
81+
a:hover {
82+
background-color: #025ab9;
83+
}
84+
ul {
85+
margin-top: 1em;
86+
}
7387
</style>
7488
<h1>Upload successful</h1>
75-
<form id="upload-form"
76-
action="/upload"
77-
method="post"
78-
enctype="multipart/form-data">
79-
<p>Files uploaded successfully</p>
89+
90+
<p>Files uploaded successfully</p>
8091

8192
{% if downloaded_files %}
8293
<a href="/">Upload more</a>
@@ -88,8 +99,6 @@ <h1>Upload successful</h1>
8899
{% endfor %}
89100
</ul>
90101
<a id="download-link" href="/download">Download file</a>
91-
</form>
92102

93-
<script src="download.js"></script>
94103
</body>
95104
</html>

src/app.py

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import os
22
import csv
3-
# import tempfile
43
from fpdf import FPDF
54
import tarfile
65
from flask import Flask, request, render_template, redirect, url_for, flash
76
from flask import send_file
87

9-
108
app = Flask(__name__, template_folder='../front-end')
11-
app.secret_key = 'Your_secret_key'
12-
""" ALLOWED_EXTENSIONS = set(['tar.gz', 'csv'])
13-
ALLOWED_EXTENSIONS = set(['tar.gz', 'csv', 'md'])
14-
15-
16-
def allowed_file(filename):
17-
return '.' in filename and \
18-
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS """
199

2010

2111
# make directories
@@ -84,11 +74,7 @@ def download():
8474
as_attachment=True)
8575

8676

87-
def extract_gz(file_path):
88-
with tarfile.open(file_path, "r:gz") as tar:
89-
for member in tar.getmembers():
90-
member.name = os.path.basename(member.name)
91-
tar.extract(member, path=UPLOADS_PATH)
77+
9278

9379

9480
def process_files(file_path):
@@ -113,7 +99,7 @@ def process_files(file_path):
11399
Cannot proceed with processing.")
114100

115101

116-
def process_extracted_files():
102+
def process_extracted_files():
117103
for extracted_file in os.listdir(UPLOADS_PATH):
118104
extracted_file_path = os.path.join(UPLOADS_PATH, extracted_file)
119105
if extracted_file.endswith('.csv'):
@@ -124,18 +110,14 @@ def process_extracted_files():
124110
MARKDOWN_TEMPLATE = extracted_file_path
125111

126112

127-
def create_pdfs():
128-
for mdfile in os.listdir(MD_PATH):
129-
if mdfile.endswith(".md"):
130-
md_file_path = os.path.join(MD_PATH, mdfile)
131-
convert_to_pdf(md_file_path)
132-
113+
def extract_gz(file_path):
114+
with tarfile.open(file_path, "r:gz") as tar:
115+
for member in tar.getmembers():
116+
member.name = os.path.basename(member.name)
117+
tar.extract(member, path=UPLOADS_PATH)
133118

134-
def create_tar_file():
135-
with tarfile.open(os.path.join(PROCESSED_PATH, 'certificates.tar.gz'),
136-
"w:gz") as tar:
137-
tar.add(PDF_PATH, arcname=os.path.basename(PDF_PATH))
138119

120+
### functions dealing with the processing
139121

140122
def read_csv_file():
141123
print("Reading CSV file...")
@@ -163,23 +145,43 @@ def modify_and_write_markdown(data):
163145
print("Markdown files generated.")
164146

165147

166-
def convert_to_pdf(md_file):
148+
def convert_to_pdf(md_file): # https://py-pdf.github.io/fpdf2/Tutorial.html fpdf
167149
pdf = FPDF()
168150
pdf.add_page()
169-
ntnu_logo_path = os.path.join("./uploads", "NTNU-logo.png")
170-
pdf.image(ntnu_logo_path, x=10, y=10, w=50)
171-
172-
signature_path = os.path.join("./uploads", "signature.png")
173-
pdf.image(signature_path, x=10, y=10, w=50)
174151

175152
with open(md_file, 'r') as md:
176153
lines = md.readlines()
177154
for line in lines:
178-
pdf.set_font("Arial", size=12)
179-
pdf.multi_cell(0, 10, line)
155+
156+
placeholder = line.strip().endswith('.png)') # getting the line where the images are located on the md file
157+
if placeholder:
158+
159+
# find the images in uploads
160+
if "NTNU-logo.png" in line:
161+
image_path = os.path.join(UPLOADS_PATH, "NTNU-logo.png")
162+
elif "signature.png" in line:
163+
image_path = os.path.join(UPLOADS_PATH, "signature.png")
164+
165+
if os.path.exists(image_path):
166+
pdf.image(image_path, x=pdf.get_x(), y=pdf.get_y(), w=50)
167+
pdf.ln(20)
168+
else:
169+
pdf.set_font("Arial", size=12)
170+
pdf.multi_cell(0, 10, line)
171+
180172
pdf_file = os.path.basename(md_file).replace(".md", ".pdf")
181173
pdf.output(os.path.join(PDF_PATH, pdf_file))
182174

175+
def create_pdfs():
176+
for mdfile in os.listdir(MD_PATH):
177+
if mdfile.endswith(".md"):
178+
md_file_path = os.path.join(MD_PATH, mdfile)
179+
convert_to_pdf(md_file_path)
180+
181+
def create_tar_file():
182+
with tarfile.open(os.path.join(PROCESSED_PATH, 'certificates.tar.gz'),
183+
"w:gz") as tar:
184+
tar.add(PDF_PATH, arcname=os.path.basename(PDF_PATH))
183185

184186
if __name__ == '__main__':
185187
port = int(os.environ.get('PORT', 5000))

src/dummy/template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Certificate of excellence
2-
![NTNU-logo](../files/NTNU-logo.png)
2+
![NTNU-logo](NTNU-logo.png)
33
{{FirstName}} {{LastName}} have completed the course IDG2001 Cloud Technologies at
44
(NTNU). As part of their course, they have blabla skills, lists, etc.
55
- SaaS, PaaS, IaaS

0 commit comments

Comments
 (0)