1 | <?php
|
---|
2 | /*************************************************************************
|
---|
3 | * http://www.fpdf.org/en/script/script5.php
|
---|
4 | *
|
---|
5 | * @author Olivier
|
---|
6 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * UPC_A(float x, float y, string barcode [, float h [, float w]])
|
---|
16 | *
|
---|
17 | * Same parameters.
|
---|
18 | *
|
---|
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 | ************************************************************************/
|
---|
21 |
|
---|
22 | /*************************************************************************
|
---|
23 | * http://www.fpdf.org/en/script/script46.php
|
---|
24 | *
|
---|
25 | * @author The-eh
|
---|
26 | *
|
---|
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
|
---|
32 | * height: bar height
|
---|
33 | * baseline: corresponds to the width of a wide bar
|
---|
34 | ************************************************************************/
|
---|
35 |
|
---|
36 |
|
---|
37 | if (!defined('__CLASS_FPDF_CODEBAR__'))
|
---|
38 | {
|
---|
39 | define('__CLASS_FPDF_CODEBAR__', true);
|
---|
40 |
|
---|
41 | require_once(dirname(__FILE__).'/../_fpdf/fpdf.php');
|
---|
42 |
|
---|
43 | class FPDF_Codebar extends FPDF
|
---|
44 | {
|
---|
45 | var $footer_param = array();
|
---|
46 |
|
---|
47 | function FPDF_Codebar($sens = 'P', $unit = 'mm', $format = 'A4')
|
---|
48 | {
|
---|
49 | $this->FPDF($sens, $unit, $format);
|
---|
50 | }
|
---|
51 |
|
---|
52 | function BARCODE_EAN13($x,$y,$barcode,$h=10,$w=.35)
|
---|
53 | {
|
---|
54 | return $this->Barcode($x,$y,$barcode,$h,$w,13);
|
---|
55 | }
|
---|
56 |
|
---|
57 | function BARCODE_UPC_A($x,$y,$barcode,$h=10,$w=.35)
|
---|
58 | {
|
---|
59 | return $this->Barcode($x,$y,$barcode,$h,$w,12);
|
---|
60 | }
|
---|
61 |
|
---|
62 | function GetCheckDigit($barcode)
|
---|
63 | {
|
---|
64 | //Compute the check digit
|
---|
65 | $sum=0;
|
---|
66 | for($i=1;$i<=11;$i+=2)
|
---|
67 | $sum+=3*$barcode{$i};
|
---|
68 | for($i=0;$i<=10;$i+=2)
|
---|
69 | $sum+=$barcode{$i};
|
---|
70 | $r=$sum%10;
|
---|
71 | if($r>0)
|
---|
72 | $r=10-$r;
|
---|
73 | return $r;
|
---|
74 | }
|
---|
75 |
|
---|
76 | function TestCheckDigit($barcode)
|
---|
77 | {
|
---|
78 | //Test validity of check digit
|
---|
79 | $sum=0;
|
---|
80 | for($i=1;$i<=11;$i+=2)
|
---|
81 | $sum+=3*$barcode{$i};
|
---|
82 | for($i=0;$i<=10;$i+=2)
|
---|
83 | $sum+=$barcode{$i};
|
---|
84 | return ($sum+$barcode{12})%10==0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | function Barcode($x,$y,$barcode,$h,$w,$len)
|
---|
88 | {
|
---|
89 | //Padding
|
---|
90 | $barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT);
|
---|
91 | if($len==12)
|
---|
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
|
---|
99 | $codes=array(
|
---|
100 | 'A'=>array(
|
---|
101 | '0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
|
---|
102 | '5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
|
---|
103 | 'B'=>array(
|
---|
104 | '0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
|
---|
105 | '5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
|
---|
106 | 'C'=>array(
|
---|
107 | '0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
|
---|
108 | '5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
|
---|
109 | );
|
---|
110 | $parities=array(
|
---|
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')
|
---|
121 | );
|
---|
122 | $code='101';
|
---|
123 | $p=$parities[$barcode{0}];
|
---|
124 | for($i=1;$i<=6;$i++)
|
---|
125 | $code.=$codes[$p[$i-1]][$barcode{$i}];
|
---|
126 | $code.='01010';
|
---|
127 | for($i=7;$i<=12;$i++)
|
---|
128 | $code.=$codes['C'][$barcode{$i}];
|
---|
129 | $code.='101';
|
---|
130 | //Draw bars
|
---|
131 | for($i=0;$i<strlen($code);$i++)
|
---|
132 | {
|
---|
133 | if($code{$i}=='1')
|
---|
134 | $this->Rect($x+$i*$w,$y,$w,$h,'F');
|
---|
135 | }
|
---|
136 |
|
---|
137 | $code_w = strlen($code)*$w;
|
---|
138 | $code_t = substr($barcode,-$len);
|
---|
139 |
|
---|
140 | $code_f = $code_w/strlen($code_t)*$this->k/0.60;
|
---|
141 | $code_h = $h+$code_f/$this->k;
|
---|
142 |
|
---|
143 | //Print text uder barcode
|
---|
144 | $this->SetFont('Arial','',$code_f);
|
---|
145 | $this->Text($x,$y+$h+0.90*$code_f/$this->k,$code_t);
|
---|
146 |
|
---|
147 | return array($code_w, $code_h);
|
---|
148 | }
|
---|
149 |
|
---|
150 | function BARCODE_CODE39($xpos, $ypos, $code,$height=10, $baseline=0.5 )
|
---|
151 | {
|
---|
152 |
|
---|
153 | $wide = $baseline;
|
---|
154 | $narrow = $baseline / 3 ;
|
---|
155 | $gap = $narrow;
|
---|
156 |
|
---|
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';
|
---|
201 |
|
---|
202 | $xpos_dep = $xpos;
|
---|
203 | $code = '*'.strtoupper($code).'*';
|
---|
204 | for($i=0; $i<strlen($code); $i++){
|
---|
205 | $char = $code{$i};
|
---|
206 | if(!isset($barChar[$char])){
|
---|
207 | $this->Error('Invalid character in barcode: '.$char);
|
---|
208 | }
|
---|
209 | $seq = $barChar[$char];
|
---|
210 | for($bar=0; $bar<9; $bar++){
|
---|
211 | if($seq{$bar} == 'n'){
|
---|
212 | $lineWidth = $narrow;
|
---|
213 | }else{
|
---|
214 | $lineWidth = $wide;
|
---|
215 | }
|
---|
216 | if($bar % 2 == 0){
|
---|
217 | $this->Rect($xpos, $ypos, $lineWidth, $height, 'F');
|
---|
218 | }
|
---|
219 | $xpos += $lineWidth;
|
---|
220 | }
|
---|
221 | $xpos += $gap;
|
---|
222 | }
|
---|
223 |
|
---|
224 | $code_w = $xpos-$xpos_dep;
|
---|
225 | $code_t = $code;
|
---|
226 |
|
---|
227 | $code_f = $code_w/strlen($code_t)*$this->k/0.60/3;
|
---|
228 | $code_h = $height+$code_f/$this->k;
|
---|
229 |
|
---|
230 | //Print text uder barcode
|
---|
231 | $this->SetFont('Arial','',$code_f);
|
---|
232 | $this->Text($xpos_dep,$ypos+$height+0.90*$code_f/$this->k,$code_t);
|
---|
233 |
|
---|
234 | return array($code_w, $code_h);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | ?>
|
---|