1
1
import os
2
2
import csv
3
- # import tempfile
4
3
from fpdf import FPDF
5
4
import tarfile
6
5
from flask import Flask , request , render_template , redirect , url_for , flash
7
6
from flask import send_file
8
7
9
-
10
8
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 """
19
9
20
10
21
11
# make directories
@@ -84,11 +74,7 @@ def download():
84
74
as_attachment = True )
85
75
86
76
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
+
92
78
93
79
94
80
def process_files (file_path ):
@@ -113,7 +99,7 @@ def process_files(file_path):
113
99
Cannot proceed with processing." )
114
100
115
101
116
- def process_extracted_files ():
102
+ def process_extracted_files ():
117
103
for extracted_file in os .listdir (UPLOADS_PATH ):
118
104
extracted_file_path = os .path .join (UPLOADS_PATH , extracted_file )
119
105
if extracted_file .endswith ('.csv' ):
@@ -124,18 +110,14 @@ def process_extracted_files():
124
110
MARKDOWN_TEMPLATE = extracted_file_path
125
111
126
112
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 )
133
118
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 ))
138
119
120
+ ### functions dealing with the processing
139
121
140
122
def read_csv_file ():
141
123
print ("Reading CSV file..." )
@@ -163,23 +145,43 @@ def modify_and_write_markdown(data):
163
145
print ("Markdown files generated." )
164
146
165
147
166
- def convert_to_pdf (md_file ):
148
+ def convert_to_pdf (md_file ): # https://py-pdf.github.io/fpdf2/Tutorial.html fpdf
167
149
pdf = FPDF ()
168
150
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 )
174
151
175
152
with open (md_file , 'r' ) as md :
176
153
lines = md .readlines ()
177
154
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
+
180
172
pdf_file = os .path .basename (md_file ).replace (".md" , ".pdf" )
181
173
pdf .output (os .path .join (PDF_PATH , pdf_file ))
182
174
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 ))
183
185
184
186
if __name__ == '__main__' :
185
187
port = int (os .environ .get ('PORT' , 5000 ))
0 commit comments