2 /*************************************************************************
3 * http://www.fpdf.org/en/script/script5.php
7 * 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).
8 * EAN13(float x, float y, string barcode [, float h [, float w]])
9 * x: abscissa of barcode.
10 * y: ordinate of barcode.
11 * barcode: value of barcode.
12 * h: height of barcode. Default value: 16.
13 * w: width of a bar. Default value: 0.35.
15 * UPC_A(float x, float y, string barcode [, float h [, float w]])
19 * 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, it will be automatically computed.
20 ************************************************************************/
22 /*************************************************************************
23 * http://www.fpdf.org/en/script/script46.php
27 * This script implements Code 39 barcodes. A Code 39 barcode can encode a string with the following characters: digits (0 to 9), uppercase letters (A to Z) and 8 additional characters (- . space $ / + % *).
28 * Code39(float xpos, float ypos, string code [, float baseline [, float height]])
29 * xpos: abscissa of barcode
30 * ypos: ordinate of barcode
31 * code: value of barcode
33 * baseline: corresponds to the width of a wide bar
34 ************************************************************************/
37 if (!defined('__CLASS_FPDF_CODEBAR__'))
39 define('__CLASS_FPDF_CODEBAR__', true);
41 require_once(dirname(__FILE__
).'/../_fpdf/fpdf.php');
43 class FPDF_Codebar
extends FPDF
45 var $footer_param = array();
47 function FPDF_Codebar($sens = 'P', $unit = 'mm', $format = 'A4')
49 $this->FPDF($sens, $unit, $format);
52 function BARCODE_EAN13($x,$y,$w,$h, $barcode,$label=true)
54 return $this->Barcode($x,$y,$w,$h, $barcode,13,$label);
57 function BARCODE_UPC_A($x,$y,$w,$h, $barcode,$label=true)
59 return $this->Barcode($x,$y,$w,$h, $barcode,12,$label);
62 function GetCheckDigit($barcode)
64 //Compute the check digit
66 for($i=1;$i<=11;$i+
=2)
68 for($i=0;$i<=10;$i+
=2)
76 function TestCheckDigit($barcode)
78 //Test validity of check digit
80 for($i=1;$i<=11;$i+
=2)
82 for($i=0;$i<=10;$i+
=2)
84 return ($sum+
$barcode{12})%10
==0;
87 function Barcode($x,$y,$code_w,$code_h,$barcode,$len, $label=true)
90 $barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT
);
92 $barcode='0'.$barcode;
93 //Add or control the check digit
94 if(strlen($barcode)==12)
95 $barcode.=$this->GetCheckDigit($barcode);
96 elseif(!$this->TestCheckDigit($barcode))
97 $this->Error('Incorrect check digit');
98 //Convert digits to bars
101 '0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
102 '5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
104 '0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
105 '5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
107 '0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
108 '5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
111 '0'=>array('A','A','A','A','A','A'),
112 '1'=>array('A','A','B','A','B','B'),
113 '2'=>array('A','A','B','B','A','B'),
114 '3'=>array('A','A','B','B','B','A'),
115 '4'=>array('A','B','A','A','B','B'),
116 '5'=>array('A','B','B','A','A','B'),
117 '6'=>array('A','B','B','B','A','A'),
118 '7'=>array('A','B','A','B','A','B'),
119 '8'=>array('A','B','A','B','B','A'),
120 '9'=>array('A','B','B','A','B','A')
123 $p=$parities[$barcode{0}];
125 $code.=$codes[$p[$i-1]][$barcode{$i}];
127 for($i=7;$i<=12;$i++
)
128 $code.=$codes['C'][$barcode{$i}];
133 $w = $code_w / strlen($code);
134 for($i=0;$i<strlen($code);$i++
)
137 $this->Rect($x+
$i*$w,$y,$w,$code_h,'F');
140 $code_t = substr($barcode,-$len);
141 $code_f = $code_w/strlen($code_t)*$this->k
/0.60;
145 $this->SetFont('Arial','',$code_f);
146 $this->Text($x,$y+
$code_h+
0.90*$code_f/$this->k
,$code_t);
147 //Print text uder barcode
148 $code_h+
= $code_f/$this->k
;
152 return array($code_w, $code_h);
155 function BARCODE_CODE39($x,$y,$w,$h, $barcode, $label=true)
157 $barChar['0'] = 'nnnwwnwnn';
158 $barChar['1'] = 'wnnwnnnnw';
159 $barChar['2'] = 'nnwwnnnnw';
160 $barChar['3'] = 'wnwwnnnnn';
161 $barChar['4'] = 'nnnwwnnnw';
162 $barChar['5'] = 'wnnwwnnnn';
163 $barChar['6'] = 'nnwwwnnnn';
164 $barChar['7'] = 'nnnwnnwnw';
165 $barChar['8'] = 'wnnwnnwnn';
166 $barChar['9'] = 'nnwwnnwnn';
167 $barChar['A'] = 'wnnnnwnnw';
168 $barChar['B'] = 'nnwnnwnnw';
169 $barChar['C'] = 'wnwnnwnnn';
170 $barChar['D'] = 'nnnnwwnnw';
171 $barChar['E'] = 'wnnnwwnnn';
172 $barChar['F'] = 'nnwnwwnnn';
173 $barChar['G'] = 'nnnnnwwnw';
174 $barChar['H'] = 'wnnnnwwnn';
175 $barChar['I'] = 'nnwnnwwnn';
176 $barChar['J'] = 'nnnnwwwnn';
177 $barChar['K'] = 'wnnnnnnww';
178 $barChar['L'] = 'nnwnnnnww';
179 $barChar['M'] = 'wnwnnnnwn';
180 $barChar['N'] = 'nnnnwnnww';
181 $barChar['O'] = 'wnnnwnnwn';
182 $barChar['P'] = 'nnwnwnnwn';
183 $barChar['Q'] = 'nnnnnnwww';
184 $barChar['R'] = 'wnnnnnwwn';
185 $barChar['S'] = 'nnwnnnwwn';
186 $barChar['T'] = 'nnnnwnwwn';
187 $barChar['U'] = 'wwnnnnnnw';
188 $barChar['V'] = 'nwwnnnnnw';
189 $barChar['W'] = 'wwwnnnnnn';
190 $barChar['X'] = 'nwnnwnnnw';
191 $barChar['Y'] = 'wwnnwnnnn';
192 $barChar['Z'] = 'nwwnwnnnn';
193 $barChar['-'] = 'nwnnnnwnw';
194 $barChar['.'] = 'wwnnnnwnn';
195 $barChar[' '] = 'nwwnnnwnn';
196 $barChar['*'] = 'nwnnwnwnn';
197 $barChar['$'] = 'nwnwnwnnn';
198 $barChar['/'] = 'nwnwnnnwn';
199 $barChar['+'] = 'nwnnnwnwn';
200 $barChar['%'] = 'nnnwnwnwn';
202 $barcode = '*'.strtoupper($barcode).'*';
204 $w_wide=3; $w_narrow=1; $w_gap=1; $nb_w = 0;
205 for($i=0; $i<strlen($barcode); $i++
)
207 $char = $barcode{$i};
208 if(!isset($barChar[$char]))
209 $this->Error('Invalid character in barcode: '.$char);
211 $seq = $barChar[$char];
214 for($bar=0; $bar<9; $bar++
)
215 $nb_w+
= ($seq{$bar}=='n' ?
$w_narrow : $w_wide);
220 $w_wide = $w/$nb_w*$w_wide;
221 $w_narrow = $w/$nb_w*$w_narrow;
222 $w_gap = $w/$nb_w*$w_gap;
224 for($i=0; $i<strlen($code); $i++
)
227 $w_line = ($code{$i}=='n' ?
$w_narrow : $w_wide);
228 if(!($j%2
)) $this->Rect($xt, $y, $w_line, $h, 'F');
230 if ($j==8) $xt+
= $w_gap;
236 $code_f = $code_w/strlen($code_t)*$this->k
/0.60/2.5;
240 //Print text uder barcode
241 $code_h+
= $code_f/$this->k
;
242 $this->SetFont('Arial','',$code_f);
243 $this->Text($x,$y+
$h+
0.90*$code_f/$this->k
,$code_t);
246 return array($code_w, $code_h);