Implements EAN13 and UPC-A barcode in PDF using fpdf in classic ASP

Posted by Jeremy N on Stack Overflow See other posts from Stack Overflow or by Jeremy N
Published on 2010-05-24T20:08:22Z Indexed on 2010/05/24 20:11 UTC
Read the original article Hit count: 1506

Filed under:
|
|
|

/*

FPDF library for ASP can be downloaded from: http://www.aspxnet.it/public/default.asp

INFORMATIONS:

Translated by: Jeremy

Author: Olivier

License: Freeware

DESCRIPTION:

This script implements EAN13 and UPC-A barcodes (the second being a particular case of the first one). Bars are drawn directly in the PDF (no image is generated)

function EAN13(x,y,barcode,h,w)

-x = x coordinate to start drawing the barcode

-y = y coordinate to start drawing the barcode

-barcode = code to write (must be all numeric)

-h = height of the bar

-w = the minimum width of individual bar

function UPC_A(x,y,barcode,h,w)

Same parameters

An EAN13 barcode is made up of 13 digits, UPC-A of 12 (leading zeroes are added if necessary). The last digit is a check digit; if it's not supplied or if it is incorrect, it will be automatically computed.

USAGE:

Copy all of this text and save it in a file called barcode.ext file under fpdf/extends folder

EXAMPLE:



Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF "P","mm","letter"
pdf.SetPath("fpdf/")
pdf.LoadExtension("barcode")
pdf.Open()
pdf.AddPage()
'set the fill color to black
pdf.setfillcolor 0,0,0
pdf.UPC_A 80,40,"123456789012",16,0.35
pdf.Close()
pdf.NewOutput "" , true, "test.pdf"


*/


this.EAN13=function (x,y,barcode,h,w)
{
    return this.Barcode(x,y,barcode,h,w,13);
};

this.UPC_A=function (x,y,barcode,h,w)
{
    return this.Barcode(x,y,barcode,h,w,12);
};

function GetCheckDigit(barCode)
{
bc = barCode.replace(/[^0-9]+/g,'');
    total = 0;

    //Get Odd Numbers
    for (i=bc.length-1; i>=0; i=i-2) 
    {
        total = total + parseInt(bc.substr(i,1));
    }

    //Get Even Numbers
    for (i=bc.length-2; i>=0; i=i-2) 
    {
        temp = parseInt(bc.substr(i,1)) * 2;
        if (temp > 9) {
        tens = Math.floor(temp/10);
        ones = temp - (tens*10);
        temp = tens + ones;
        }
        total = total + temp;
    }

    //Determine the checksum
    modDigit = (10 - total % 10) % 10;

    return modDigit.toString();
}

//Test validity of check digit
function TestCheckDigit(barcode)
{
    var cd=GetCheckDigit(barcode.substring(0,barcode.length-1));
    return cd==parseInt(barcode.substring(barcode.length-1,1));
}

this.Barcode=function Barcode(x,y,barcode,h,w,len)
{
    //Padding
    while(barcode.length < len-1)
    {
        barcode = '0' + barcode;
    }

    if(len==12) {barcode='0' + barcode;}

    //Add or control the check digit
    if(barcode.length==12)
    {
        barcode += GetCheckDigit(barcode);
    }
    else
    {   //if the check digit is incorrect, fix the check digit.
        if(!TestCheckDigit(barcode))
        {
            barcode = barcode.substring(0,barcode.length-1) + GetCheckDigit(barcode.substring(0,barcode.length-1));
        }
    }

    //Convert digits to bars
    var codes=[['0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'],
        ['0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'],
        ['1110010','1100110','1101100','1000010','1011100','1001110','1010000','1000100','1001000','1110100']
        ];
    var parities=[[0,0,0,0,0,0],
        [0,0,1,0,1,1],
        [0,0,1,1,0,1],
        [0,0,1,1,1,0],
        [0,1,0,0,1,1],
        [0,1,1,0,0,1],
        [0,1,1,1,0,0],
        [0,1,0,1,0,1],
        [0,1,0,1,1,0],
        [0,1,1,0,1,0]
        ];
    var code='101';

    var p=parities[parseInt(barcode.substr(0,1))];

    var i;
    for(i=1;i<=6;i++)
    {
        code+= codes[p[i-1]][parseInt(barcode.substr(i,1))]; 
    }
    code+='01010';


    for(i=7;i<=12;i++)
    {
        code+= codes[2][parseInt(barcode.substr(i,1))]; 
    }
    code+='101';

    //Draw bars
    for(i=0;i<code.length;i++)
    {
        if(code.substr(i,1)=='1')
        {
            this.Rect(x+i*w,y,w,h,'F');
        }
    }

    //Print text uder barcode. 
    this.SetFont('Arial','',12);
    //Set the x so that the font is centered under the barcode
    this.Text(x+parseInt(0.5*barcode.length)*w,y+h+11/this.k,barcode.substr(barcode.length-len,len));
}

© Stack Overflow or respective owner

Related posts about pdf

Related posts about asp