public function testBarcodeLib() { $pad = BarcodeLib::padUPC('1'); $this->assertEquals('0000000000001', $pad, 'BarcodeLib::padUPC failed'); $checks = array('12345678901' => '2', '123456789012' => '8', '1234567890123' => '1'); foreach ($checks as $barcode => $check_digit) { $calc = BarcodeLib::getCheckDigit($barcode); $this->assertEquals($check_digit, $calc, 'Failed check digit calculation for ' . $barcode); $with_check = $barcode . $check_digit; $without_check = $barcode . ($check_digit + 1) % 10; $this->assertEquals(true, BarcodeLib::verifyCheckdigit($with_check)); $this->assertEquals(false, BarcodeLib::verifyCheckdigit($without_check)); } $upc_a = BarcodeLib::UPCACheckDigit('12345678901'); $this->assertEquals('123456789012', $upc_a, 'Failed UPC A check digit calculation'); $ean_13 = BarcodeLib::EAN13CheckDigit('123456789012'); $this->assertEquals('1234567890128', $ean_13, 'Failed EAN 13 check digit calculation'); $norm = BarcodeLib::normalize13('12345678901'); $this->assertEquals('0123456789012', $norm, 'Failed normalizing UPC-A to 13 digits'); $norm = BarcodeLib::normalize13('123456789012'); $this->assertEquals('1234567890128', $norm, 'Failed normalizing EAN-13 to 13 digits'); }
/** Draw barcode on given PDF @param $upc [string] barcode value (UPC or EAN) @param $pdf [object] FPDF instance @param $x [numeric] x-coordinate of barcode @param $y [numeric] y-coordinate of barcode @param $args [keyed array] of extra options - height [default 16] height of the barcode - width [default 0.35] width of *each* bar - align [default C] horizontal alignment of barcode number (L/C/R) - valign [default B] vertical alignment of barcode number (T, "top", above barcode) or (B, "botton", below barcode) - prefix [default empty] prepend value to barcode number - suffix [default empty] append value to barcode number - font [default Arial] name of font for barcode number - fontsize [default 9] size of font for barcode number */ public function drawBarcode($upc, $pdf, $x, $y, $args = array()) { $height = isset($args['height']) ? $args['height'] : 16; $width = isset($args['width']) ? $args['width'] : 0.35; $align = isset($args['align']) ? $args['align'] : 'C'; $valign = isset($args['valign']) ? $args['valign'] : 'B'; $prefix = isset($args['prefix']) ? $args['prefix'] : ''; $suffix = isset($args['suffix']) ? $args['suffix'] : ''; $font = isset($args['font']) ? $args['font'] : 'Arial'; $fontsize = isset($args['fontsize']) ? $args['fontsize'] : 9; $upc = ltrim($upc, '0'); $is_ean = false; if (strlen($upc) == 12) { // must be EAN $check = \BarcodeLib::getCheckDigit($upc); $upc .= $check; $is_ean = true; } else { $upc = str_pad($upc, 11, '0', STR_PAD_LEFT); $check = \BarcodeLib::getCheckDigit($upc); $upc = '0' . $upc . $check; } //Convert digits to bars $code = $this->upcToBitString($upc); //Draw bars $full_width = 0; for ($i = 0; $i < strlen($code); $i++) { if ($code[$i] == '1') { $pdf->Rect($x + $i * $width, $y, $width, $height, 'F'); } $full_width += $width; } // Print text under barcode // omits first digit; should always be zero if ($fontsize > 0) { $pdf->SetFont($font, '', $fontsize); if ($valign == 'T') { $pdf->SetXY($x, $y - 5); } else { $pdf->SetXY($x, $y + $height); } $pdf->Cell($full_width, 5, $prefix . substr($upc, $is_ean ? -13 : -12) . $suffix, 0, 0, $align); } return $pdf; }