Skip to content

Instantly share code, notes, and snippets.

@arturfsousa
Forked from seocam/barcode_template.html
Created September 13, 2012 18:56
Show Gist options
  • Save arturfsousa/3716729 to your computer and use it in GitHub Desktop.
Save arturfsousa/3716729 to your computer and use it in GitHub Desktop.
Generate a interleaved 2 of 5 barcode in pure HTML and CSS (tested only in Firefox and Chrome)
<html>
<head>
<style>
#barcode {height: 60px;}
#barcode span {margin: 0;padding-bottom: 34px;height: 16px;}
.n {border-left: 1px solid;}
.w {border-left: 3px solid;}
.n, .w {border-color: #000;}
.s {border-color: #fff;}
</style>
</head>
<body>
<div id="barcode">
$barcode
</div>
</body>
</html>
#!/usr/bin/env python
from string import Template
from itertools import izip_longest, chain
DIGITS = [
['n', 'n', 'w', 'w', 'n'],
['w', 'n', 'n', 'n', 'w'],
['n', 'w', 'n', 'n', 'w'],
['w', 'w', 'n', 'n', 'n'],
['n', 'n', 'w', 'n', 'w'],
['w', 'n', 'w', 'n', 'n'],
['n', 'w', 'w', 'n', 'n'],
['n', 'n', 'n', 'w', 'w'],
['w', 'n', 'n', 'w', 'n'],
['n', 'w', 'n', 'w', 'n'],
]
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
def interleave2of5(code):
digits = ['n', 'n s', 'n', 'n s']
if len(code) % 2 != 0:
code = '0' + code
for digt1, digt2 in grouper(2, code):
digt1_repr = DIGITS[int(digt1)]
digt2_repr = map(lambda x: x + ' s', DIGITS[int(digt2)])
digits.extend(chain(*zip(digt1_repr, digt2_repr)))
digits.extend(['w', 'n s', 'n'])
result = []
for digit in digits:
result.append('<span class="%s"></span>' % digit)
return ''.join(result)
if __name__ == '__main__':
template_f = open('barcode_template.html')
template = Template(template_f.read())
template_f.close()
print template.substitute(
dict(barcode=interleave2of5('123456789123124'))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment