views:

327

answers:

1

/*

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);
};

var value_0 = '0'.charCodeAt(0);

function GetCheckDigit(param)

{
    var sum=0;
    var odd_parity=true;
    for(var i=param.length-1; i>=0; i--)


    {
        if ( odd_parity )
            sum += 3*(param.charCodeAt(i)-value_0);
        else
            sum += param.charCodeAt(i)-value_0;
        odd_parity = !odd_parity;
    }
    var check_digit = 10 - (sum%10);
    if (check_digit==10) check_digit=0; 
    return String.fromCharCode(check_digit+value_0 );
}

//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;}

    //Fix 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));
}
A: 

The code is in the question

Jeremy N