Skip to content

Instantly share code, notes, and snippets.

@andyferra
Created June 29, 2010 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyferra/457809 to your computer and use it in GitHub Desktop.
Save andyferra/457809 to your computer and use it in GitHub Desktop.
// From: http://www.krisbailey.com/2009/01/creating-code-39-barcodes-in-pure-javascript/
function print_html_code39_barcode(
content,
height,
insertCheckDigit,
printContent,
fontPointSize,
thinBarWidth,
barWidthRatio){
content = content.toUpperCase();
if (isNaN(thinBarWidth)) thinBarWidth = 1;
if (isNaN(barWidthRatio)) barWidthRatio = 3;
var thin = thinBarWidth;
var thick = thinBarWidth*barWidthRatio;
var last = thick*0.66;
var whiteImageURL = 'http://www.krisbailey.com/images/white.gif';
var blackImageURL = 'http://www.krisbailey.com/images/black.gif';
var thinWhiteBar = "<img src='"+whiteImageURL+"' height='"+height+"' width='"+thin+"'>";
var thinBlackBar = "<img src='"+blackImageURL+"' height='"+height+"' width='"+thin+"'>";
var thickWhiteBar = "<img src='"+whiteImageURL+"' height='"+height+"' width='"+thick+"'>";
var thickBlackBar = "<img src='"+blackImageURL+"' height='"+height+"' width='"+thick+"'>";
var lastBar = "<img src='"+whiteImageURL+"' height='"+height+"' width='"+last+"'>";
// this is the definition array for what bar
// combinations correspond to what characters.
// definition is as follows:
// 0 = thin white (blank) bar
// 1 = thin black bar
// 2 = thick white (blank) bar
// 3 = thick black bar
// the 10th digit is for calculating the check digit
// so, the letter A is:
// [3,0,1,0,1,2,1,0,3,10]
// which means:
// thick black bar
// thin white
// thin black
// thin white
// thin black
// thick white
// thin black
// thin white
// thick black
// and the check digit calculation value is 10
var codes = {
'0' : [1,0,1,2,3,0,3,0,1,0],
'1' : [3,0,1,2,1,0,1,0,3,1],
'2' : [1,0,3,2,1,0,1,0,3,2],
'3' : [3,0,3,2,1,0,1,0,1,3],
'4' : [1,0,1,2,3,0,1,0,3,4],
'5' : [3,0,1,2,3,0,1,0,1,5],
'6' : [1,0,3,2,3,0,1,0,1,6],
'7' : [1,0,1,2,1,0,3,0,3,7],
'8' : [3,0,1,2,1,0,3,0,1,8],
'9' : [1,0,3,2,1,0,3,0,1,9],
'A' : [3,0,1,0,1,2,1,0,3,10], // <-- used in the example above
'B' : [1,0,3,0,1,2,1,0,3,11],
'C' : [3,0,3,0,1,2,1,0,1,12],
'D' : [1,0,1,0,3,2,1,0,3,13],
'E' : [3,0,1,0,3,2,1,0,1,14],
'F' : [1,0,3,0,3,2,1,0,1,15],
'G' : [1,0,1,0,1,2,3,0,3,16],
'H' : [3,0,1,0,1,2,3,0,1,17],
'I' : [1,0,3,0,1,2,3,0,1,18],
'J' : [1,0,1,0,3,2,3,0,1,19],
'K' : [3,0,1,0,1,0,1,2,3,20],
'L' : [1,0,3,0,1,0,1,2,3,21],
'M' : [3,0,3,0,1,0,1,2,1,22],
'N' : [1,0,1,0,3,0,1,2,3,23],
'O' : [3,0,1,0,3,0,1,2,1,24],
'P' : [1,0,3,0,3,0,1,2,1,25],
'Q' : [1,0,1,0,1,0,3,2,3,26],
'R' : [3,0,1,0,1,0,3,2,1,27],
'S' : [1,0,3,0,1,0,3,2,1,28],
'T' : [1,0,1,0,3,0,3,2,1,29],
'U' : [3,2,1,0,1,0,1,0,3,30],
'V' : [1,2,3,0,1,0,1,0,3,31],
'W' : [3,2,3,0,1,0,1,0,1,32],
'X' : [1,2,1,0,3,0,1,0,3,33],
'Y' : [3,2,1,0,3,0,1,0,1,34],
'Z' : [1,2,3,0,3,0,1,0,1,35],
'-' : [1,2,1,0,1,0,3,0,3,36],
'.' : [3,2,1,0,1,0,3,0,1,37],
' ' : [1,2,3,0,1,0,3,0,1,38],
'*' : [1,2,1,0,3,0,3,0,1,39],
'$' : [1,2,1,2,1,2,1,0,1,40],
'/' : [1,2,1,2,1,0,1,2,1,41],
'+' : [1,2,1,0,1,2,1,2,1,42],
'%' : [1,0,1,2,1,2,1,2,1,43]
};
var tw = 0;
var sum = 0;
for (var ee=0; ee<content.length; ee++){
var tcodes = codes[content.charAt(ee)];
if (typeof tcodes!='undefined'){
sum += tcodes[9];
}
}
if (insertCheckDigit){
var checksum = sum%43;
for (x in codes){
if (codes[x][9]==checksum){
content = content+x;
}
}
}
content = '*'+content+'*';
var htmlout = "<"+"table border='0' cellpadding='1' cellspacing='1'>";
htmlout += "<"+"tr><"+"td align='center'><nobr>";
for (var i=0; i<content.length; i++){
tcodes = codes[content.charAt(i)];
if (typeof tcodes!='undefined'){
for (var xi=0; xi<9; xi++){
switch (tcodes[xi]){
case 0:
htmlout += thinWhiteBar;
break;
case 1:
htmlout += thinBlackBar;
break;
case 2:
htmlout += thickWhiteBar;
break;
case 3:
htmlout += thickBlackBar;
break;
}
}
}
htmlout += lastBar;
}
htmlout += "</nobr><"+"/td><"+"/tr>";
if (printContent){
htmlout += "<"+"tr><"+"td align='center' style='font-size: "+fontPointSize+"pt;'>";
htmlout += "<nobr>"+content+"</nobr><"+"/td><"+"/tr>";
}
htmlout += "<"+"/table>";
return htmlout;
}
/*
Arguments:
content - This is the string you want to have encoded
height - The height in pixels of the barcode
insertCheckDigit - true or false, whether to insert a check digit or not
(not needed for most scanners)
printContent - true or false, whether or not to print the content string below the barcode
fontPointSize - The size of the font to use if printing the content
thinBarWidth - The width in pixels of thin bars, default is 1
barWidthRatio - The ratio of thin to thick bar widths. A good value of 2.8-3.0, default is 3
Use:
Include the javascript function in your page somehow, then you can do something as simple as:
<script type="text/javascript">
document.write(print_html_code39_barcode('this is a barcode', 30, false, true, 8));
</script>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment