Badge creator

master
Julian M. Kunkel 2018-06-01 11:29:05 +01:00
parent cee04d353b
commit f175a3177e
4 changed files with 152 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

97
badges/Reader.py 100644
View File

@ -0,0 +1,97 @@
# Copyright by JK 2018
# License: MIT
# https://www.reportlab.com/docs/reportlab-userguide.pdf
# -*- coding: utf-8 -*-
"""
This file and class contains helper methods to paint the resulting PDF
"""
import sys
import os.path
import re
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
def loadData():
people = []
with open("attendees.txt", 'r') as f:
for p in f:
m = re.match("(.*)\((.*)\)", p)
if m:
people.append((m.group(1), m.group(2)))
return people
class PDFDoc():
def __init__(self, name, width, height):
self.dim = (width*cm, height*cm)
self.c = canvas.Canvas(name + '.pdf', pagesize=self.dim)
def newpage(self):
self.c.showPage()
def save(self):
self.c.showPage()
self.c.save()
def subImage(self, x, y, width, height):
return SubImage(self, self.c, x, y, width, height)
def findImg(self, fileimg):
fileimg = fileimg.replace(" ", "-").lower()
for typ in ["png", "jpg"]:
fname = fileimg + "." + typ
if os.path.isfile(fname):
return fname
return None
class SubImage():
def __init__(self, doc, c, x, y, width, height):
self.doc = doc
self.c = c
self.dim = (width*cm, height*cm)
self.off = (x*cm, y*cm)
def addKeyVal(self, value, posx, posy, size=0.1, font= "Helvetica", color=(0,0,0)):
#relative font size
self.c.setFont(font, self.dim[1] * size)
self.c.setStrokeColorRGB(*color)
self.c.setFillColorRGB(*color)
value = value.strip().split("\n")
for l in value:
self.c.drawString(self.dim[0] * posx + self.off[0], self.dim[1] * posy + self.off[1], l)
posy -= size*1.1
return posy
def addKeyValCenter(self, value, posx, posy, size=0.1, font= "Helvetica", color=(0,0,0)):
#relative font size
self.c.setFont(font, self.dim[1] * size)
self.c.setStrokeColorRGB(*color)
self.c.setFillColorRGB(*color)
value = value.strip().split("\n")
for l in value:
self.c.drawCentredString(self.dim[0] * posx + self.off[0], self.dim[1] * posy + self.off[1], l)
posy -= size*1.1
return posy
def drawRect(self, posx, posy, width, height, color=(0,0,0)):
self.c.setFillColorRGB(*color)
self.c.rect(posx*self.dim[0]+ self.off[0], posy*self.dim[1]+ self.off[1], width*self.dim[0], height*self.dim[1], fill=1, stroke=0)
def drawRectBorder(self, posx, posy, width, height, color=(0,0,0)):
self.c.setStrokeColorRGB(*color)
self.c.rect(posx*self.dim[0]+ self.off[0], posy*self.dim[1]+ self.off[1], width*self.dim[0], height*self.dim[1], fill=0, stroke=1)
def addImg(self, fileimg, posx, posy, height):
fname = self.doc.findImg(fileimg)
if fname != None:
im = Image.open(fname)
(w, h) = im.size
scale = h / (self.dim[1] * height)
self.c.drawImage(fname, posx*self.dim[0] + self.off[0], posy*self.dim[1]+ self.off[1], w/scale, height*self.dim[1])
return True
return False

View File

@ -0,0 +1,55 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import traceback
from Reader import *
# Copyright by JK 2018
# License: MIT
# https://www.reportlab.com/docs/reportlab-userguide.pdf
if len(sys.argv) > 1:
event = sys.argv[1]
else:
event = "Use ARG1 as event"
data = loadData()
doc = PDFDoc("badges-A4", 21, 29.7)
c = 0
for p in data:
try:
# check for image
if c % 8 == 0:
offset_x = 0.5
elif c % 8 == 4:
offset_x = 9.5
if c % 4 == 0:
offset_y = 1
else:
offset_y += 6
img = doc.subImage(offset_x, offset_y , 9, 6)
img.addImg("../assets/background", 0, 0, 0.5)
img.addImg("../assets/reading-logo", 0.1, 0.8, 0.1)
img.addKeyVal("Computer Science", 0.37, 0.86, size=0.05, color=(0,0.2,0), font="Helvetica")
img.addImg("logo", 0.7, 0.8, 0.1)
img.drawRectBorder(0, 0, 1, 1)
img.addKeyValCenter(p[0], 0.5, 0.6, font="Helvetica")
img.addKeyValCenter(p[1], 0.5, 0.47, size=0.07, font="Helvetica-Oblique")
img.addKeyValCenter(event, 0.5, 0.2, size=0.07, font="Helvetica-Oblique")
if c % 8 == 7:
doc.newpage()
c+=1
except:
print("Unexpected error when processing %s" %(p))
traceback.print_exc()
doc.save()

BIN
badges/logo.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB