Creating Code 39 Barcodes in Pure Javascript
There are many different barcode standards and code 39 is one that is easy to use, supported by most scanners, and can encode letters, numbers, spaces, and some punctuation. For these reasons it makes it a good choice for many different scenarios.
There are a lot of ways you can create barcodes on your own. You can get a special font for it. You can get special software. You can write server side generation logic in a host of different languages. The server side generators can use a font, or generate it manually. Continue on to learn how to do it on the fly in javascript.
Example:
Images
The the url's for the black and white gif images are pointing to the images on this site, to guarantee that whatever you use this for does not unexpectedly stop working you will want to download the images and put them on your own server. Here they are:
Code
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>
That would output the following:
For the best scanning results make sure you print using a laser printer, or if you are scanning directly from a display, use an LCD instead of a CRT.
NOTE: While it is easily possible to do the same thing without the use of images, I chose to use images so I would not have to worry about the interference of CSS styles that might already be associated with DIV or other tags that could have been used instead. Feel free to convert it to using DIV's instead of images if it suits your purpose.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Leave a comment