Skip to content

Instantly share code, notes, and snippets.

@olasitarska
Created March 26, 2013 13:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olasitarska/5245471 to your computer and use it in GitHub Desktop.
Save olasitarska/5245471 to your computer and use it in GitHub Desktop.
This is a file we use to generate badges for HACKWAW.com event
# -*- encoding: utf-8 -*-
import StringIO
import csv
from reportlab.platypus import Table, TableStyle, Paragraph
from reportlab.lib import colors
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import getSampleStyleSheet
from pyPdf import PdfFileWriter, PdfFileReader
# fonts import
pdfmetrics.registerFont(TTFont('ApexNew-Bold', 'ApexNew-Bold.ttf'))
pdfmetrics.registerFont(TTFont('ApexNew-Book', 'ApexNew-Book.ttf'))
# data import
data = csv.reader(open('hackers.csv'))
# read the column names from the first line of the file
fields = data.next()
for row in data:
item = dict(zip(fields, row))
print item['First Name'] + " " + item['Last Name']
# importing 'base' file on which we write stuff
input = PdfFileReader(file("template_black.pdf", "rb"))
packet = StringIO.StringIO()
c = canvas.Canvas(packet, pagesize=(263.52,340.08))
width, height = (263.52,340.08)
# word number 1
stylesheet=getSampleStyleSheet()
styleN = stylesheet['Normal']
# changing the size of the font depending on the lenght of the word
if len(item['First Name'])>7:
styleN.fontSize = 20
else:
styleN.fontSize = 30
styleN.fontName = 'ApexNew-Bold'
p = Paragraph(u'<font color="white">'+item['First Name'].upper().decode('utf-8')+u'</font>', styleN)
# width/height of the paragraph
w,h = p.wrap(126, height)
# x/y axis when the paragraph should be added
p.drawOn(c, 37.8, height-80.8)
# word number 2
stylesheet=getSampleStyleSheet()
styleN = stylesheet['Normal']
if len(item['Last Name'])>13 and "-" not in item['Last Name']:
styleN.fontSize = 16
elif len(item['Last Name'])>11:
styleN.fontSize = 18
else:
styleN.fontSize = 20
styleN.fontName = 'ApexNew-Bold'
offset = 0
p = Paragraph(u'<font color="white">'+item['Last Name'].upper().decode('utf-8')+u'</font>', styleN)
w,h = p.wrap(126, height)
p.drawOn(c, 37.8, height-122.8)
# word number 3
stylesheet=getSampleStyleSheet()
styleN = stylesheet['Normal']
styleN.fontSize = 20
styleN.fontName = 'ApexNew-Bold'
if item['Ticket Type'] == 'Developer ticket':
p = Paragraph(u'<font color="white">hacker</font>', styleN)
else:
p = Paragraph(u'<font color="white">designer</font>', styleN)
w,h = p.wrap(126, height)
p.drawOn(c, 37.8, height-149.8-offset)
if item['Ticket Type'] == 'Developer ticket':
type = item['What kind of project are you going to hack during HACKWAW?'].lower()
# word number 4
stylesheet=getSampleStyleSheet()
styleN = stylesheet['Normal']
styleN.fontSize = 14
styleN.fontName = 'ApexNew-Book'
styleN.leading = 16
if type == 'hardware':
p = Paragraph(u'<para spaceAfter="20" spaceBefore="20"><font color="white">hardware</font></para>', styleN)
w,h = p.wrap(126, height)
p.drawOn(c, 37.8, height-209.8-offset)
elif type == 'software only':
languages = item['What platform are you developing for?'].replace(' | ',', ').lower()
p = Paragraph(u'<para spaceAfter="20" spaceBefore="20"><font color="white">'+languages+'</font></para>', styleN)
w,h = p.wrap(126, height)
p.drawOn(c, 37.8, height-209.8-offset)
# saving canvas
c.save()
packet.seek(0)
text = PdfFileReader(packet)
output = PdfFileWriter()
# merging text with base
page = input.getPage(0)
page.mergePage(text.getPage(0))
output.addPage(page)
# saving file
outputStream = file("badge_spad/badge_"+item['First Name']+item['Last Name']+".pdf", "wb")
output.write(outputStream)
outputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment