Jump to content

barcode in pdf


searls03

Recommended Posts

so I have this code that makes a barcode:

<?php
session_start(); // Must start session first thing
/* 
Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/
// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$userid = $_SESSION['id'];
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE userid='$userid' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$phone = $row["phone"];
$username = $row["username"];
$address = $row["address"];
$city = $row["city"];
$state = $row["state"];
$zip = $row["zip"];
$cell = $row["cell"];
$email = $row["email"];
$accounttype = $row["accounttype"];
$rank = $row["rank"];
$badges = $row["badges"];
}

$sql = mysql_query("SELECT * FROM Events WHERE userid='$userid' && eventid=".$_GET['eventid']."");
while($row = mysql_fetch_array($sql)){
$price1 = $row['price1'];
$price2 = $row["price2"];
$price3 = $row["price3"];
$price4 = $row["price4"];
$price5 = $row["price5"];
$price6 = $row["price6"];
$price7 = $row["price7"];
$price8 = $row["price8"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$barcode1 = $row['barcode'];


}



$total = $price1 + $price2 + $price3 + $price4 +$price5 + $price6 + $price7 + $price8;


require('fpdf.php');
class PDF extends FPDF

{
function EAN13($x=1, $y, $barcode, $h=16, $w=.35)
{
    $this->Barcode(13,200,$barcode,$h,$w,13);
}

function UPC_A($x, $y, $barcode, $h=16, $w=.35)
{
    $this->Barcode($x,$y,$barcode,$h,$w,12);
}

function GetCheckDigit($barcode)
{
    //Compute the check digit
    $sum=0;
    for($i=1;$i<=11;$i+=2)
        $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
        $sum+=$barcode[$i];
    $r=$sum%10;
    if($r>0)
        $r=10-$r;
    return $r;
}

function TestCheckDigit($barcode)
{
    //Test validity of check digit
    $sum=0;
    for($i=1;$i<=11;$i+=2)
        $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
        $sum+=$barcode[$i];
    return ($sum+$barcode[12])%10==0;
}

function Barcode($x, $y, $barcode, $h, $w, $len)
{
    //Padding
    $barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT);
    if($len==12)
        $barcode='0'.$barcode;
    //Add or control the check digit
    if(strlen($barcode)==12)
        $barcode.=$this->GetCheckDigit($barcode);
    elseif(!$this->TestCheckDigit($barcode))
        $this->Error('Incorrect check digit');
    //Convert digits to bars
    $codes=array(
        'A'=>array(
            '0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
            '5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
        'B'=>array(
            '0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
            '5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
        'C'=>array(
            '0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
            '5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
        );
    $parities=array(
        '0'=>array('A','A','A','A','A','A'),
        '1'=>array('A','A','B','A','B','B'),
        '2'=>array('A','A','B','B','A','B'),
        '3'=>array('A','A','B','B','B','A'),
        '4'=>array('A','B','A','A','B','B'),
        '5'=>array('A','B','B','A','A','B'),
        '6'=>array('A','B','B','B','A','A'),
        '7'=>array('A','B','A','B','A','B'),
        '8'=>array('A','B','A','B','B','A'),
        '9'=>array('A','B','B','A','B','A')
        );
    $code='101';
    $p=$parities[$barcode[0]];
    for($i=1;$i<=6;$i++)
        $code.=$codes[$p[$i-1]][$barcode[$i]];
    $code.='01010';
    for($i=7;$i<=12;$i++)
        $code.=$codes['C'][$barcode[$i]];
    $code.='101';
    //Draw bars
    for($i=0;$i<strlen($code);$i++)
    {
        if($code[$i]=='1')
            $this->Rect($x+$i*$w,$y,$w,$h,'F');
    }
    //Print text uder barcode
    $this->SetFont('Arial','B',12);
    $this->Text($x,$y+$h+11/$this->k,substr($barcode,-$len));
}


//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();

//* here is part of barcode
$pdf->EAN13(10, 120, $barcode1);
$pdf->SetFont('Arial','B',12);
$pdf->Output('ticket.pdf', 'I');
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

 

 

How do I make it so that the bars still draw correctly, but they don't draw from the code, they draw from $barcode. or how do I put the value of $barcode into there.........currently it has to be a 8 digit $barcode and I believe 4-5 random numbers either on front or back.  I want to be able to make $barcode have the value of an ean 13 barcode(which is how many numbers? 13 right?)  and so that that is the only thing dictating barcode(I do know how to make it more numbers, but I cant do that because of the 5 random ones).  does this make sense?

Link to comment
Share on other sites

An EAN13 Bar-code is in fact 13 digits, BUT the last digit is a checksum, so only 12 digits are need to generate the bar-code,

As for what your attempting to do, it's quite unclear!

 

Bar-code are shouldn't contain "random number" and EAN13 has 12 usable digits, so that's a trillion bar-codes you can use!

Link to comment
Share on other sites

let me try to explain this better then.  Basically, I need it just to be random digits, it isn't assigned to a product, it is going to be a piece in my database that will show up on a ticket, but right now, the last 4 numbers(besides for the extra) is being a random digit.  but 8 of them are what are in database.  I need to know how I can basically use a function that makes random numbers that I have print out all 12 digits(not the last) that can then use the barcode maker to make the bars.  how do I do this.  does this make more sense?

Link to comment
Share on other sites

Erm... well

 

function getBarcodeNumber($barcode){
$barcode = (int)$barcode;
if(strlen($barcode) > 12) tigger_error('invalid barcode more that 12 digits supplied');
return str_pad($barcode , 12, "0", STR_PAD_LEFT);
}
$assetNo = 123;
$barcode = getBarcodeNumber($assetNo);
$pdf->EAN13(10, 120, $barcode);

Link to comment
Share on other sites

I just need help as to where to put it in that code from before, I have this code, but I am pretty sure it isn't correct seeing as that I keep getting an error.  I can't get the error, because the server is down right now, but I bet it is probably pretty clear as to what it is......

<?php
session_start(); // Must start session first thing
/* 
Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/
// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$userid = $_SESSION['id'];
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE userid='$userid' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$phone = $row["phone"];
$username = $row["username"];
$address = $row["address"];
$city = $row["city"];
$state = $row["state"];
$zip = $row["zip"];
$cell = $row["cell"];
$email = $row["email"];
$accounttype = $row["accounttype"];
$rank = $row["rank"];
$badges = $row["badges"];
}

$sql = mysql_query("SELECT * FROM Events WHERE userid='$userid' && eventid=".$_GET['eventid']."");
while($row = mysql_fetch_array($sql)){
$price1 = $row['price1'];
$price2 = $row["price2"];
$price3 = $row["price3"];
$price4 = $row["price4"];
$price5 = $row["price5"];
$price6 = $row["price6"];
$price7 = $row["price7"];
$price8 = $row["price8"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$barcode1 = $row['barcode'];


}



$total = $price1 + $price2 + $price3 + $price4 +$price5 + $price6 + $price7 + $price8;




require('fpdf.php');
class PDF extends FPDF{

function getBarcodeNumber($barcode){
$barcode = (int)$barcode;
if(strlen($barcode) > 12) tigger_error('invalid barcode more that 12 digits supplied');
return str_pad($barcode , 12, "0", STR_PAD_LEFT);
}}
$assetNo = 123;
$barcode = getBarcodeNumber($assetNo);
$pdf->EAN13(10, 120, $barcode);


//Page header
require('fpdf.php');
class PDF extends FPDF{
function Header()
{

	global $name;
	global $total;
	global $title1;
	global $title2;
	global $title3;
	global $title4;
	global $title5;
	global $title6;
	global $title7;
	global $title8;
	global $price1;
	global $price2;
	global $price3;
	global $price4;
	global $price5;
	global $price6;
	global $price7;
	global $price8;
global $barcode;


	 // get the variable $name1 into the scope of this function
    //Logo
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(70);
    //Title
    $this->Cell(0,0,'Please bring this to the next Scout Meeting',0,0,C);
    //Line break
    $this->Ln(20);
$this->Cell(0,0,'',1,0);
$this->Ln(20);
$this->cell(50,10, 'Registrant:', 1,0);
$this->cell(50,10, 'Events:', 1,0);
$this->cell(50,10, 'Price:', 1,0);
$this->Ln(10);
$this->cell(50,10,$name, 'L,R',0);
	if(!empty($title1)) {


$this->cell(50,10, $title1, 'L',0);
	$this->cell(50,10, $price1, 'L,R',0);

	} 
	if(!empty($title2)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title2, 'L',0);
		$this->cell(50,10, $price2, 'L,R',0);

	} if(!empty($title3)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title3, 'L',0);
		$this->cell(50,10, $price3, 'L,R',0);

	} if(!empty($title4)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title4, 'L',0);
		$this->cell(50,10, $price4, 'L,R',0);

	} if(!empty($title5)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title5, 'L',0);
		$this->cell(50,10, $price5, 'L,R',0);

	} if(!empty($title6)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title6, 'L',0);
		$this->cell(50,10, $price6, 'L,R',0);

	} if(!empty($title7)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title7, 'L',0);
		$this->cell(50,10, $price7, 'L,R',0);

	} if(!empty($title8)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title8, 'L',0);
		$this->cell(50,10, $price8, 'L,R',0);

	}
		$this->Ln(10);
$this->cell(50,10,'','',0);
$this->cell(50,10,'Total:','R', 'L',0);

$this->cell(50,10, $total, 1,0);

}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','B',;
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Output('ticket.pdf', 'I');
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Link to comment
Share on other sites

alright, here is it a bit more modified.....

<?php
session_start(); // Must start session first thing
/* 
Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/
// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$userid = $_SESSION['id'];
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE userid='$userid' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$phone = $row["phone"];
$username = $row["username"];
$address = $row["address"];
$city = $row["city"];
$state = $row["state"];
$zip = $row["zip"];
$cell = $row["cell"];
$email = $row["email"];
$accounttype = $row["accounttype"];
$rank = $row["rank"];
$badges = $row["badges"];
}

$sql = mysql_query("SELECT * FROM Events WHERE userid='$userid' && eventid=".$_GET['eventid']."");
while($row = mysql_fetch_array($sql)){
$price1 = $row['price1'];
$price2 = $row["price2"];
$price3 = $row["price3"];
$price4 = $row["price4"];
$price5 = $row["price5"];
$price6 = $row["price6"];
$price7 = $row["price7"];
$price8 = $row["price8"];
$title1 = $row['title1'];
$title2 = $row['title2'];
$title3 = $row['title3'];
$title4 = $row['title4'];
$title5 = $row['title5'];
$title6 = $row['title6'];
$title7 = $row['title7'];
$title8 = $row['title8'];
$barcode = $row['barcode'];


}



$total = $price1 + $price2 + $price3 + $price4 +$price5 + $price6 + $price7 + $price8;


require('fpdf.php');
class PDF extends FPDF

{
function Header()
{
global $name;
	global $total;
	global $title1;
	global $title2;
	global $title3;
	global $title4;
	global $title5;
	global $title6;
	global $title7;
	global $title8;
	global $price1;
	global $price2;
	global $price3;
	global $price4;
	global $price5;
	global $price6;
	global $price7;
	global $price8;
global $barcode;
function getBarcodeNumber($barcode){
$barcode = (int)$barcode;
if(strlen($barcode) > 12) tigger_error('invalid barcode more that 12 digits supplied');
return str_pad($barcode , 12, "0", STR_PAD_LEFT);


}

function EAN13($x=1, $y, $barcode, $h=16, $w=.35)
{
    $this->Barcode(13,200,$barcode,$h,$w,13);
}

function UPC_A($x, $y, $barcode, $h=16, $w=.35)
{
    $this->Barcode($x,$y,$barcode,$h,$w,12);
}

function GetCheckDigit($barcode)
{
    //Compute the check digit
    $sum=0;
    for($i=1;$i<=11;$i+=2)
        $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
        $sum+=$barcode[$i];
    $r=$sum%10;
    if($r>0)
        $r=10-$r;
    return $r;
}

function TestCheckDigit($barcode)
{
    //Test validity of check digit
    $sum=0;
    for($i=1;$i<=11;$i+=2)
        $sum+=3*$barcode[$i];
    for($i=0;$i<=10;$i+=2)
        $sum+=$barcode[$i];
    return ($sum+$barcode[12])%10==0;
}

function Barcode($x, $y, $barcode, $h, $w, $len)
{
    //Padding
    $barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT);
    if($len==12)
        $barcode='0'.$barcode;
    //Add or control the check digit
    if(strlen($barcode)==12)
        $barcode.=$this->GetCheckDigit($barcode);
    elseif(!$this->TestCheckDigit($barcode))
        $this->Error('Incorrect check digit');
    //Convert digits to bars
    $codes=array(
        'A'=>array(
            '0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
            '5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
        'B'=>array(
            '0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
            '5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
        'C'=>array(
            '0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
            '5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
        );
    $parities=array(
        '0'=>array('A','A','A','A','A','A'),
        '1'=>array('A','A','B','A','B','B'),
        '2'=>array('A','A','B','B','A','B'),
        '3'=>array('A','A','B','B','B','A'),
        '4'=>array('A','B','A','A','B','B'),
        '5'=>array('A','B','B','A','A','B'),
        '6'=>array('A','B','B','B','A','A'),
        '7'=>array('A','B','A','B','A','B'),
        '8'=>array('A','B','A','B','B','A'),
        '9'=>array('A','B','B','A','B','A')
        );
    $code='101';
    $p=$parities[$barcode[0]];
    for($i=1;$i<=6;$i++)
        $code.=$codes[$p[$i-1]][$barcode[$i]];
    $code.='01010';
    for($i=7;$i<=12;$i++)
        $code.=$codes['C'][$barcode[$i]];
    $code.='101';
    //Draw bars
    for($i=0;$i<strlen($code);$i++)
    {
        if($code[$i]=='1')
            $this->Rect($x+$i*$w,$y,$w,$h,'F');
    }
    //Print text uder barcode
    $this->SetFont('Arial','B',12);
    $this->Text($x,$y+$h+11/$this->k,substr($barcode,-$len));
}


//Instanciation of inherited class



	 // get the variable $name1 into the scope of this function
    //Logo
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(70);
    //Title
    $this->Cell(0,0,'Please bring this to the next Scout Meeting',0,0,C);
    //Line break
    $this->Ln(20);
$this->Cell(0,0,'',1,0);
$this->Ln(20);
$this->cell(50,10, 'Registrant:', 1,0);
$this->cell(50,10, 'Events:', 1,0);
$this->cell(50,10, 'Price:', 1,0);
$this->Ln(10);
$this->cell(50,10,$name, 'L,R',0);
	if(!empty($title1)) {


$this->cell(50,10, $title1, 'L',0);
	$this->cell(50,10, $price1, 'L,R',0);

	} 
	if(!empty($title2)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title2, 'L',0);
		$this->cell(50,10, $price2, 'L,R',0);

	} if(!empty($title3)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title3, 'L',0);
		$this->cell(50,10, $price3, 'L,R',0);

	} if(!empty($title4)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title4, 'L',0);
		$this->cell(50,10, $price4, 'L,R',0);

	} if(!empty($title5)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title5, 'L',0);
		$this->cell(50,10, $price5, 'L,R',0);

	} if(!empty($title6)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title6, 'L',0);
		$this->cell(50,10, $price6, 'L,R',0);

	} if(!empty($title7)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title7, 'L',0);
		$this->cell(50,10, $price7, 'L,R',0);

	} if(!empty($title8)) {
$this->Ln(10);
$this->cell(50,10,'', 'L,R',0);

$this->cell(50,10, $title8, 'L',0);
		$this->cell(50,10, $price8, 'L,R',0);

	}
		$this->Ln(10);
$this->cell(50,10,'','',0);
$this->cell(50,10,'Total:','R', 'L',0);

$this->cell(50,10, $total, 1,0);

}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','B',;
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->EAN13(10, 120, $barcode);
$pdf->SetFont('Arial','B',12);
$pdf->Output('ticket.pdf', 'I');
?> 
?>  

 

I am close to figuring it out, but now it cant find function EAN13

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.