Created
August 18, 2012 21:36
-
-
Save seocam/3390031 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
#barcode {height: 60px;} | |
#barcode span {margin: 0;padding-bottom: 34px;height: 16px;} | |
.n {padding-right: 1px;} | |
.w {padding-right: 3px;} | |
.n, .w {background-color: #000;} | |
.s {background-color: #fff;} | |
</style> | |
</head> | |
<body> | |
<div id="barcode"> | |
$barcode | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"/>' % 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