I tried running from libraries and was printing incorrectly the bars, but all the problem was in the script that for some reason conflicts with CI... I guess in the future I have to adapt the code to CI.
<?php
function bar128($text) { // Part 1, make list of widths
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$char128wid = array(
'212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9
'221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19
'221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29
'212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39
'231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49
'231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59
'314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69
'112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79
'111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89
'214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
'114131','311141','411131','211412','211214','211232','23311120' ); // 100-106
$w = $char128wid[$sum = 104]; // START symbol
$onChar=1;
for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
$w.= $char128wid[$pos];
$sum += $onChar++ * $pos;
}
$w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
//Part 2, Write rows
$html="<table cellpadding=0 cellspacing=0><tr>";
for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>";
return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=1.5>$text</td></tr></table>";
}
//include 'barcode128.php';
$product = $_POST['product'];
$product_id = $_POST['product_id'];
$rate = $_POST['rate'];
for ($i = 1; $i <= $_POST['print_qty']; $i++) {
echo "<p class='inline'><span ><b>$product</b></span>" . bar128(stripcslashes($_POST['product_id'])) . "<span ><b>Precio: " . $rate . " </b><span></p>    ";
}
?>
First, I'm pretty noob on Codeigniter.
I found this php code to generate code128 which I need to print multiple labels from a DB and then send to a Zebra printer.
On pure php it works great, but when integrated to codeigniter using libraries the bars are not printing. Everything is printing except the bars.
On the code in comments you can see all my attempts.
First I tried in the same view "include" "require_once", but then I found that doesn't work on CI or something like that, so I created a new library:
//////The lib//////
<?php
global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$char128wid = array(
'212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9
'221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19
'221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29
'212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39
'231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49
'231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59
'314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69
'112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79
'111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89
'214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
'114131','311141','411131','211412','211214','211232','23311120' ); // 100-106
class Bar128
{
function __construct()
{
function Barra128($cod) { // Part 1, make list of widths
global $char128asc,$char128wid;
$w = $char128wid[$sum = 104]; // START symbol
$onChar=1;
for($x=0;$x<strlen($cod);$x++) // GO THRU TEXT GET LETTERS
if (!( ($pos = strpos($char128asc,$cod[$x])) === false )){ // SKIP NOT FOUND CHARS
$w.= $char128wid[$pos];
$sum += $onChar++ * $pos;
}
$w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
//Part 2, Write rows
$html="<table cellpadding=0 cellspacing=0><tr>";
for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>";
return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=1.5>$cod</td></tr></table>";
}
}
?>
//////////// CONTROLLER ///////////
public function Barcodegenerator()
{
//load library
$this->load->library('Myzend');
//$this->zend->load('Zend/barcode128');
//require ('barcode128.php');
//$this->load->helper('barcode128');
//require_once APPPATH. 'barcode128.php';
//require 'barcode128.php';
//include APPPATH. 'barcode128.php';
//include('assets/barcode128.php');
$this->load->view('barcode',$data);
}
////////////////// My Lib //////////
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
require('Zend/barcode128.php');
class MyZend extends Bar128 {
public function __construct()
{
parent::__construct();
$CI =& get_instance();
}
}
/////////////// THE VIEW //////////
<?php
//define('__ROOT__', dirname(dirname(__FILE__)));
//require_once(__ROOT__.'/barcode128.php');
//require_once('../barcode/barcode128.php');
//require_once dirname( __FILE__ ) . '/barcode128.php';
//require_once($_SERVER['DOCUMENT_ROOT'].'/barcode/barcode128.php');
//echo include(base_url().'../barcode/barcode128.php');
//require_once 'barcode128.php';
//require 'barcode128.php';
//include 'barcode128.php';
$product = $_POST['product'];
$product_id = $_POST['product_id'];
$rate = $_POST['rate'];
for ($i = 1; $i <= $_POST['print_qty']; $i++) {
echo "<p class='inline'><span ><b>$product</b></span>" . Barra128(stripcslashes($_POST['product_id'])) . "<span ><b>Precio: " . $rate . " </b><span></p>    ";
}
?>
General suggestions (might be a problem in codeigniter):
This path is relative to the lib file in your library (or third_party) directory.
require('Zend/barcode128.php');
If you wish to require it from somewhere else is best to use predefined relative paths (BASEPATH, APPPATH, FCPATH, etc.). For example
require(APPPATH . 'libraries/zend/barcode128.php');
will look for /application/libraries/zend/barcode128.php
Also try using only first uppercase name for a lib name, like change this
class MyZend extends Bar128 {
to
class Myzend extends Bar128 {
Sometimes(tm) Codeigniter doesn't play very well with name forming for libs, helpers, etc.
A safe way to create library main files is to name them with first uppercase and rest lowercase, like for your case filename should be "Myzend.php".
As for enabling logs in codeigniter: in config.php:
$config['log_threshold'] = 4;
Sets the logs to be including INFO's, may help finding the error
You don't really need a Myzend library or require any file. You simply need modify the bar128 class file to follow naming conventions for custom CI libraries. A quick way to start is to rename the file using the defined prefix from your config files. Then, in its construtor, initialize the CI instance which should be used by every other method in the library. parent::__construct(); $CI =& get_instance();
Then you can replace every $this with $CI in that classes.