Skip to content

Commit

Permalink
Creates PDF/ZIP only if user ticks on it. (#72)
Browse files Browse the repository at this point in the history
merge_badges.py file is taken from #71 and further tweaking is done on it. index.html is changed to be updated with the new changes. and main.py is also changed. style.css is also changed with accordance to it.
  • Loading branch information
gabru-md authored and jajodiaraghav committed Sep 16, 2017
1 parent 0bf4b17 commit 9498df1
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
21 changes: 17 additions & 4 deletions app/main.py
Expand Up @@ -22,8 +22,13 @@ def index():
return render_template('index.html', default_background = default_background)


def generate_badges():
os.system(SCRIPT)
def generate_badges(_zip=False,_pdf=False):
if _zip == True and _pdf == True:
os.system('python merge_badges.py -z -p')
elif _zip == True and _pdf == False:
os.system('python merge_badges.py -z')
else:
os.system('python merge_badges.py -p')


def empty_directory():
Expand Down Expand Up @@ -51,6 +56,8 @@ def upload():
csv = request.form['csv'].strip()
img = request.form['img-default']
file = request.files['file']
_pdf = True if request.form.get('pdf') == 'on' else False
_zip = True if request.form.get('zip') == 'on' else False

# If default background is selected
if img != '':
Expand Down Expand Up @@ -95,7 +102,7 @@ def upload():
filename = secure_filename(filename)
if filename != "default.png.csv":
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
generate_badges()
generate_badges(_zip,_pdf)

# Remove the uploaded files after job in done
os.unlink(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Expand All @@ -104,8 +111,14 @@ def upload():
os.unlink(os.path.join(app.config['UPLOAD_FOLDER'], imgname))
except Exception:
traceback.print_exc()

if _zip and _pdf:
flash(filename, 'success')
elif _zip and not _pdf:
flash(filename,'success-zip')
else:
flash(filename,'success-pdf')

flash(filename, 'success')
return redirect(url_for('index'))
else:
flash('Only CSV file is accepted!', 'error')
Expand Down
76 changes: 76 additions & 0 deletions app/merge_badges.py
@@ -0,0 +1,76 @@
#!usr/bin/python3

import os
import argparse

parser = argparse.ArgumentParser(description='Argument Parser for merge_badges')
parser.add_argument('-p',dest='pdf',action='store_true')
parser.add_argument('-z',dest='zip',action='store_true')
parser.set_defaults(pdf=False,zip=False)
arguments = parser.parse_args()
_pdf = arguments.pdf
_zip = arguments.zip

print(_pdf,_zip)

if os.system('which rsvg-convert') != 0:
os.system('sudo apt-get -y install librsvg2-bin')
if os.system('which python3') != 0:
os.system('sudo apt-get -y install python3')
if os.system('which pdftk') != 0:
os.system('sudo apt-get -y install pdftk')

os.system('python3 generate-badges.py')

BADGES_FOLDER = 'static/badges'


input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

def generate_pdfs(folder_path):
svgs = [file for file in os.listdir(folder_path)
if file.lower().endswith('.svg')]
for svg in svgs:
svg_path = os.path.join(folder_path, svg)
pdf_path = os.path.splitext(svg_path)[0] + '.pdf'
print('svg: {}'.format(svg_path))
print('pdf: {}'.format(pdf_path))
os.system('rsvg-convert -f pdf -o {} {}'.format(pdf_path, svg_path))

# Generating PDF files from svg.
if _pdf:
for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
generate_pdfs(folder_path)

# Merge badges of different types
input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

print ('Merging badges of different types.')

for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
out = folder + '.pdf'
out_path = os.path.join(BADGES_FOLDER, out)
os.system('pdftk ' + folder_path + '/*.pdf cat output ' + out_path)

final_path = os.path.join(BADGES_FOLDER, 'all-badges.pdf')
os.system('pdftk ' + BADGES_FOLDER + '/*.pdf cat output ' + final_path)

if _zip:
print ("Created {}".format(final_path))
print ("Generating ZIP file")

os.system('find static/badges/ \( -iname \*.svg -o -iname \*.pdf \) | zip -@ static/badges/all-badges.zip')

print ('Generating ZIP of SVG files')

input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
file_name = folder_path + '.svg.zip'
os.system('find ' + folder_path + ' -type f -name *.svg | zip -@ ' + file_name)
5 changes: 4 additions & 1 deletion app/static/css/style.css
Expand Up @@ -155,4 +155,7 @@ textarea {
bottom: -50px;
border-top: 1px solid #bfbfbf;
background-color: #f9f9f9;
}
}
.options{
margin-bottom: 20px;
}
25 changes: 24 additions & 1 deletion app/templates/index.html
Expand Up @@ -13,14 +13,37 @@
<a href="static/badges/{{msg}}.badges.pdf" class="btn btn-success" download="download">Download as PDF</a>
<a href="static/badges/{{msg}}.badges.svg.zip" class="btn btn-success" download="download">Download as ZIP of SVGs</a>
</div>
{% endif %}

{% if cat == 'success-pdf' %}
<div class="text-center">
<div class="flash-{{cat}}">Your badges has been successfully generated.</div>
<a href="static/badges/{{msg}}.badges.pdf" class="btn btn-success" download="download">Download as PDF</a>
</div>
{% endif %}

{% if cat == 'success-zip' %}
<div class="text-center">
<div class="flash-{{cat}}">Your badges has been successfully generated.</div>
<a href="static/badges/all-badges.zip" class="btn btn-success" download="download">Download as ZIP</a>
<a href="static/badges/{{msg}}.badges.svg.zip" class="btn btn-success" download="download">Download as ZIP of SVGs</a>
</div>
{% endif %}

{% else %}
<div class="flash-{{cat}}">{{msg}}</div>
{% endif %}
{% endfor %}
{% endif %}
{% endwith %}
<form action="{{ url_for('upload') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<span class="options">
<label>Tick the download type(s):</label>
<label for="zip">Zip</label>
<input type="checkbox" id="zip" name="zip">
<label for="pdf">PDF</label>
<input type="checkbox" id="pdf" name="pdf">
</span>
<label for="inputFile">Upload a CSV file to generate badges</label>
<input type="file" id="inputFile" name="file">
<div class="text-center">OR</div>
Expand Down

0 comments on commit 9498df1

Please sign in to comment.