Skip to content

Commit

Permalink
fixes #19 - Convert shell script to python
Browse files Browse the repository at this point in the history
Fixes issue #19, added python script to generate pdfs and zip
and merge them
  • Loading branch information
djmgit committed Sep 16, 2017
1 parent 0bf4b17 commit 0649363
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions app/merge_badges.py
@@ -0,0 +1,61 @@
import os

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.
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)

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)

0 comments on commit 0649363

Please sign in to comment.