<?php

	define('FPDF_FONTPATH','font/');
	require('fpdf/fpdf.php');

	class PDF extends FPDF{
		function EAN13($x,$y,$barcode,$h=16,$w=.35){
			$this->Barcode($x,$y,$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','',12);
		   $this->Text($x,$y+$h+11/$this->k,substr($barcode,-$len));
		}
	}
	
?>

##### PAGINA codigo_barra.php #####

<?php
	
	require('inc/code13.php');
		
	//ARQUIVO DE CONEXAO DO BANCO DE DADOS
	require("../setup.php"); 
	
	$sql = mysql_query("SELECT CO_PRODUTO
						FROM 00_tb_produto
						WHERE ST_PRODUTO = 'A'
						ORDER BY CO_PRODUTO");
	$row	=   mysql_num_rows($sql);
	
	//VERIFICA SE RETORNOU ALGUMA LINHA
	if(!$row){
		echo "<script>
				  alert('[Erro] - NÃO FOI ENCONTRADO NENHUM REGISTRO!');
				  window.location = 'inicio.php';
			  </script>";
		die; 
	}		 
	
	//NUMERO DE RESULTADOS POR PÁGINA						 
	$por_pagina  =  60;	 
	
	//CALCULA QUANTAS PÁGINAS VÃO SER NECESSÁRIAS
	$paginas   =  ceil($row/$por_pagina);   
	
	$pdf = new PDF();
	
	//INICIALIZA AS VARIÁVEIS
	$linha_atual =  0;
	$inicio	  =  0;
	
	//PÁGINAS
	for($x=1; $x<=$paginas; $x++) {
	   //VERIFICA
	   $inicio	  =  $linha_atual;
	   $fim		 =  $linha_atual + $por_pagina;
	   if($fim > $row) $fim = $row;
	   
	   $pdf->Open();					
	   $pdf->AddPage();	
	   
	   $top  = 5;
	   $left = 5;		 
	  
	   //EXIBE OS REGISTROS	  
	   for($i=$inicio; $i<$fim; $i++) {
		   			
		   $pdf->EAN13($left,$top,mysql_result($sql, $i, "CO_PRODUTO"));
		   
		   $top  = $top+25;
								
		   $linha_atual++;
		   
	   }
	}
				  
	$pdf->Output();

?>