Created
November 27, 2013 15:47
-
-
Save patrickmaciel/7677891 to your computer and use it in GitHub Desktop.
Solução para imprimir de maneira simplificada utilizando a classe EscPos.php uma nota fiscal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class EscPos { | |
// Prepare c_EscPos Object | |
// ====================== | |
/* Printer Driver Settings */ | |
var $os; // operating system | |
var $printer; // printer name or port | |
var $output; // output mode | |
var $handle; // printer- / file- handle | |
var $width; // output width (characters) | |
var $translate; // use character translation | |
var $printer_char_set; // printer character set | |
/* Data */ | |
var $document; // printer document | |
var $translator_char_set; // translator character set name | |
var $translate_find; // find those characters (array) | |
var $translate_replace; // and replace with these (array) | |
/* Printer Capability Settings */ | |
var $pcp_color; // can print colors | |
var $pcp_barcode; // can print bar codes | |
var $pcp_cut; // can cut paper | |
var $pcp_logo; // can print logo | |
var $pcp_drawer; // has drawer kick | |
var $pcp_fontselect; // has font selection | |
var $pcp_reverse; // can reverse feed | |
/* Port Settings */ | |
var $baud; | |
var $parity; | |
var $data; | |
var $stop; | |
var $xon; | |
// Constructor | |
// =========== | |
public function __construct( $printer_setup = false, $printer_name = false, $reset = true, $com_setup = true ) { | |
// require_once "print_config.inc"; | |
if ( substr( php_uname(), 0, 7 ) == "Windows" ) $this -> os = "windows"; | |
else $this -> os = "unix"; | |
$this -> printer = 'COM1'; | |
$this -> output = 'port'; | |
$this -> handle = false; | |
$this -> document = false; | |
// Load Printer Setup | |
// ================== | |
$this -> LoadSetup( $printer_setup ); | |
// Open Printer? | |
// ============= | |
if ( $printer_name == true ) $this -> Open( $printer_name, $reset, $com_setup ); | |
} | |
// Error Message | |
// ============= | |
public function error( $message ) { | |
echo "c_EscPos ERROR: $message\n"; | |
} | |
// Use Character Translation | |
// ========================= | |
public function UseTranslation( $use = true ) { | |
if ( $use == true ) $this -> translate = true; | |
else $this -> translate = false; | |
} | |
// Character Translation Setup | |
// =========================== | |
public function TranslationSetup( $name, $find, $replace ) { | |
if ( !is_array( $find ) && !is_array( $replace ) ) { | |
$this -> error( '$find and $replace must be arrays!' ); | |
return false; | |
} | |
$this -> translate_find = $find; | |
$this -> translate_replace = $replace; | |
$this -> translator_char_set = $name; | |
return true; | |
} | |
// Select Printer-Character-Table | |
// ============================== | |
public function PrinterCharacterTable( $char_table ) { | |
if ( $this -> handle != false ) { | |
$this -> error( 'Character tables of opened printers can\'t be altered.' ); | |
return false; | |
}elseif ( !is_numeric( $char_table ) ) { | |
$this -> error( 'Value to select character table must be a number!' ); | |
return false; | |
} | |
$this -> printer_char_set = $char_table; | |
return true; | |
} | |
// Set Width (Characters) | |
// ====================== | |
public function SetWidth( $width ) { | |
if ( !is_numeric( $width ) ) { | |
$this -> error( 'Width must be a number.' ); | |
return false; | |
} | |
$this -> width = $width; | |
return true; | |
} | |
// Capability Setup | |
// ================ | |
public function CapSetup( $set_color = false, $set_barcode = false, $set_cut = false, $set_logo = false, $set_drawer = false, $set_fontselect = false, $set_reverse = false ) { | |
$this -> pcp_color = $set_color; | |
$this -> pcp_barcode = $set_barcode; | |
$this -> pcp_cut = $set_cut; | |
$this -> pcp_logo = $set_logo; | |
$this -> pcp_drawer = $set_drawer; | |
$this -> pcp_fontselect = $set_fontselect; | |
$this -> pcp_reverse = $set_reverse; | |
} | |
// Communication Setup | |
// =================== | |
public function ComSetup( $set_baud = false, $set_parity = false, $set_data = false, $set_stop = false, $set_xon = false ) { | |
if ( $this -> handle != false ) { | |
$this -> error( 'Communication settings of opened devices can\'t be altered.' ); | |
return false; | |
} | |
if ( $set_baud != false ) $this -> baud = $set_baud; | |
if ( $set_parity != false ) $this -> parity = $set_parity; | |
if ( $set_data != false ) $this -> data = $set_data; | |
if ( $set_stop != false ) $this -> stop = $set_stop; | |
if ( $set_xon != false ) $this -> xon = $set_xon; | |
return true; | |
} | |
// Load Printer Setup | |
// ================== | |
public function LoadSetup( $printer = false ) { | |
// global $EscPos_printer; | |
$EscPos_printer = Configure::read( 'printer.config' ); | |
if ( $printer == false ) { | |
$printer = 'DEFAULT'; | |
$EscPos_printer['DEFAULT'] = array( | |
'width' => 40, | |
'translate' => true, | |
'p_char_set' => false, | |
'color' => false, | |
'barcode' => false, | |
'cut' => false, | |
'logo' => false, | |
'drawer' => false, | |
'fontselect' => false, | |
'reverse' => false, | |
'baud' => 9600, | |
'parity' => 'N', | |
'data' => 8, | |
'stop' => 1, | |
'xon' => 'off', | |
't_char_set' => false, | |
'find' => false, | |
'repl' => false, | |
); | |
} | |
if ( !isset( $EscPos_printer[$printer] ) ) { | |
$this -> error( 'Unknown printer setup.' ); | |
return false; | |
} | |
$this->width = $EscPos_printer[$printer]['width']; | |
$this->translate = $EscPos_printer[$printer]['translate']; | |
$this->printer_char_set = $EscPos_printer[$printer]['p_char_set']; | |
$this->pcp_color = $EscPos_printer[$printer]['color']; | |
$this->pcp_barcode = $EscPos_printer[$printer]['barcode']; | |
$this->pcp_cut = $EscPos_printer[$printer]['cut']; | |
$this->pcp_logo = $EscPos_printer[$printer]['logo']; | |
$this->pcp_drawer = $EscPos_printer[$printer]['drawer']; | |
$this->pcp_fontselect = $EscPos_printer[$printer]['fontselect']; | |
$this->pcp_reverse = $EscPos_printer[$printer]['reverse']; | |
$this->baud = $EscPos_printer[$printer]['baud']; | |
$this->parity = $EscPos_printer[$printer]['parity']; | |
$this->data = $EscPos_printer[$printer]['data']; | |
$this->stop = $EscPos_printer[$printer]['stop']; | |
$this->xon = $EscPos_printer[$printer]['xon']; | |
if ( $EscPos_printer[$printer]['find'] != false && $EscPos_printer[$printer]['repl'] != false ) { | |
if ( $EscPos_printer[$printer]['t_char_set'] == false ) $EscPos_printer[$printer]['t_char_set'] = $printer; | |
$this -> TranslationSetup( $EscPos_printer[$printer]['t_char_set'], $EscPos_printer[$printer]['find'], $EscPos_printer[$printer]['repl'] ); | |
} | |
return true; | |
} | |
// Get Settings | |
// ============ | |
public function GetSettings( $setting = false, $echo = false ) { | |
$settings = array(); | |
$settings['os'] = $this -> os; | |
$settings['printer'] = $this -> printer; | |
$settings['output'] = $this -> output; | |
$settings['width'] = $this -> width; | |
$settings['translate'] = $this -> translate; | |
$settings['p_char_set'] = $this -> printer_char_set; | |
$settings['t_char_set'] = $this -> translator_char_set; | |
$settings['color'] = $this -> pcp_color; | |
$settings['barcode'] = $this -> pcp_barcode; | |
$settings['cut'] = $this -> pcp_cut; | |
$settings['logo'] = $this -> pcp_logo; | |
$settings['drawer'] = $this -> pcp_drawer; | |
$settings['fontselect'] = $this -> pcp_fontselect; | |
$settings['reverse'] = $this -> pcp_reverse; | |
$settings['baud'] = $this -> baud; | |
$settings['parity'] = $this -> parity; | |
$settings['data'] = $this -> data; | |
$settings['stop'] = $this -> stop; | |
$settings['xon'] = $this -> xon; | |
if ( $echo == true ) { | |
if ( $setting == false ) { | |
foreach ( $settings as $name => $value ) { | |
if ( $value === false ) $text = 'false'; | |
elseif ( $value === true ) $text = 'true'; | |
else $text = $value; | |
echo sprintf( "%' 12s", $name ) . ": $text\n"; | |
} | |
return true; | |
}else { | |
if ( isset( $settings[$setting] ) ) { | |
echo "$setting: " . $settings[$setting] . "\n"; | |
return true; | |
}else { | |
$this -> error( 'There\'s no such setting.' ); | |
return false; | |
} | |
} | |
}else { | |
if ( $setting == false ) { | |
return $settings; | |
}else { | |
if ( isset( $settings[$setting] ) ) { | |
return $settings[$setting]; | |
}else { | |
$this -> error( 'There\'s no such setting.' ); | |
return false; | |
} | |
} | |
} | |
} | |
// Open | |
// ==== | |
public function Open( $printer_name, $reset = true, $com_setup = true ) { | |
// Check Opening / Output Method | |
// ============================= | |
switch ( $printer_name ) { | |
case "PRN": | |
case "COM1": | |
case "COM2": | |
case "LPT1": | |
case "LPT2": | |
if ( $this -> os == 'unix' ) { | |
$this -> error( 'Invalid Unix port.' ); | |
return false; | |
} | |
$this -> output = 'port'; | |
break; | |
case "ttyS0": | |
case "ttyS1": | |
case "ttyS2": | |
case "lp0": | |
case "lp1": | |
case "lp2": | |
case "usb/lp0": | |
case "usb/lp1": | |
case "usb/lp2": | |
if ( $this -> os == 'windows' ) { | |
$this -> error( 'Invalid Windows port.' ); | |
return false; | |
} | |
$this -> output = 'port'; | |
break; | |
default: | |
$this -> output = 'printer_system'; | |
} | |
$this -> printer = $printer_name; | |
// Open Printer | |
// ============ | |
if ( $this -> output == 'printer_system' ) { | |
if ( $this -> os == 'windows' ) { | |
// Open With Windows Printer Functions | |
// =================================== | |
if ( !extension_loaded( 'printer' ) ) { | |
$this -> error( 'PHP\'s windows printer extension must be loaded.' ); | |
return false; | |
} | |
$this -> handle = printer_open( $this -> printer ); | |
if ( $this -> handle == false ) { | |
$this -> error( 'Printer couldn\'t be opened.' ); | |
return false; | |
} | |
printer_set_option( $this -> handle, PRINTER_MODE, "raw" ); | |
}else { | |
// Send Document to CUPS lpr later | |
// =============================== | |
$this -> handle = true; | |
} | |
}else { | |
if ( $this -> output == 'port' && $this -> os == 'windows' ) { | |
// Open Port DOS / Windows | |
// ======================= | |
if ( $this -> printer == "PRN" ) { | |
$device = $this -> printer; | |
}else { | |
if ( $com_setup == true ) { | |
// Communications Setup | |
// ================================================================= | |
// shell_exec("mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off") | |
shell_exec( "mode " . strtolower( $this -> handle ) . ": BAUD=" . $this -> baud . " PARITY=" . $this -> parity . " data=" . $this -> data . " stop=" . $this -> stop . " xon=" . $this -> xon ); | |
} | |
$device = $this -> printer; | |
} | |
$this -> handle = fopen( $device, 'w' ); | |
if ( $this -> handle == false ) { | |
$this -> error( 'Printer couldn\'t be opened.' ); | |
return false; | |
} | |
}elseif ( $this -> output == 'port' && $this -> os == 'unix' ) { | |
// Open Serial Port Unix | |
// ===================== | |
if ( $com_setup == true ) { | |
// Communications Setup | |
// ==================================================== | |
// system("stty 9600 cs8 -parenb -cstopb < /dev/ttyS0") | |
$udevice = ' < /dev/' . $this -> printer; | |
$ubaud = $this -> baud; | |
if ( $this -> data == 8 ) $udata = ' cs' . $this -> data; // number of data bits | |
if ( $this -> parity == 'N' ) $uparity = ' -parenb'; // no parity bit | |
else $uparity = ' parenb'; // parity bit | |
if ( $this -> stop == 1 ) $ustop = ' -cstopb'; // one stop bit | |
else $ustop = ' cstopb'; // two stop bits | |
if ( $this -> xon == 'on' ) $uxon = ' ixon'; // activate XON | |
else $uxon = ' -ixon'; // deactivate XON | |
system( "stty " . $ubaud . $udata . $uparity . $ustop . $udevice ); | |
} | |
$device = "/dev/" . $this -> printer; | |
$this -> handle = fopen( $device, 'w' ); | |
if ( $this -> handle == false ) { | |
$this -> error( 'Printer couldn\'t be opened.' ); | |
return false; | |
} | |
}else { | |
return false; | |
} | |
} | |
// Re-Initialize printer (reset buffer)? | |
// ===================================== | |
if ( $reset == true ) $esc_pos_command = $this->kbyte( 27 ) . "@"; | |
else $esc_pos_command = ""; | |
// Select ESC/POS Character Code Table? | |
// ==================================== | |
if ( $this -> printer_char_set !== false ) $esc_pos_command .= $this->kbyte( 27 ) . "t" . $this->kbyte( $this -> printer_char_set ); | |
// Append to Document | |
// ================== | |
if ( isset( $esc_pos_command ) ) $this -> document = $esc_pos_command; | |
return true; | |
} | |
// Append to Document | |
// ================== | |
public function Append( $string ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> document === false ) $this -> document = $string; | |
else $this -> document .= $string; | |
return true; | |
} | |
// Select Color | |
// ============ | |
// $color: integer (color id 0 or 1) | |
public function Color( $color = 0 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_color != true ) return false; | |
switch ( $color ) { | |
case 1: | |
$select = $this->kbyte( 49 ); | |
break; | |
default: | |
$select = $this->kbyte( 48 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "r" . $select ); | |
} | |
// Double Strike | |
// ============= | |
// $double_strike: bool | |
public function Double( $double_strike = false ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $double_strike ) { | |
case true: | |
$set = $this->kbyte( 49 ); | |
break; | |
default: | |
$set = $this->kbyte( 48 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "G" . $set ); | |
} | |
// Emphasize | |
// ========= | |
// $select: bool | |
public function Emphasize( $select = false ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $select ) { | |
case true: | |
$set = $this->kbyte( 49 ); | |
break; | |
default: | |
$set = $this->kbyte( 48 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "E" . $set ); | |
} | |
// Underline | |
// ========= | |
// $underline: bool | |
public function Underline( $underline = false ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $underline ) { | |
case true: | |
$set = $this->kbyte( 49 ); | |
break; | |
default: | |
$set = $this->kbyte( 48 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "-" . $set ); | |
} | |
// Invert Black/White | |
// ================== | |
// $side: integer (0 <=> 1) | |
public function InvertBW( $side = 0 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $side > 1 or $side < 0 ) $side = 0; | |
return $this -> Append( $this->kbyte( 29 ) . "B" . $this->kbyte( $side ) ); | |
} | |
// Upside Down Printing | |
// ================================== | |
// (c) 2009 by 'smacedo' on my forum, | |
// modified by 'Kovu' 01.04.2010. | |
// Thanks for your contribution! | |
// ================================== | |
// $side: integer (0 <=> 1) | |
public function UpsideDown( $side = 0 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $side > 1 or $side < 0 ) $side = 0; | |
return $this -> Append( $this->kbyte( 27 ) . "{" . $this->kbyte( $side ) ); | |
} | |
// Character Size | |
// ================================== | |
// (c) 2009 by 'smacedo' on my forum, | |
// modified by 'Kovu' 01.04.2010. | |
// Thanks for your contribution! | |
// ================================== | |
// $size: integer (0 <=> 255) | |
public function Size( $size = 0 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $size < 0 ) $size = 0; | |
elseif ( $size > 255 ) $size = 255; | |
return $this -> Append( $this->kbyte( 29 ) . "!" . $this->kbyte( $size ) ); | |
} | |
// Select Font | |
// =========== | |
// $font: string (type character A/B/*C) | |
public function Font( $font = "B" ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_fontselect != true ) return false; | |
if ( eregi( "TM-U220", $font ) && $font == "C" ) $font = "B"; | |
switch ( $font ) { | |
case "A": | |
$set = $this->kbyte( 48 ); | |
break; | |
case "C": | |
$set = $this->kbyte( 50 ); | |
break; | |
default: | |
$set = $this->kbyte( 49 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "M" . $set ); | |
} | |
// Select Character Height | |
// ======================= | |
// $size: string (double / normal) | |
public function Height( $size = "double" ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $size ) { | |
case "double": | |
$set = $this->kbyte( 16 ); | |
break; | |
default: | |
$set = $this->kbyte( 0 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "!" . $set ); | |
} | |
// Select Character Width | |
// ====================== | |
// $size: string (double / normal) | |
public function Width( $size = "double" ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $size ) { | |
case "double": | |
$set = $this->kbyte( 32 ); | |
break; | |
default: | |
$set = $this->kbyte( 0 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "!" . $set ); | |
} | |
// Align | |
// ===== | |
// $position: string (left/center/right) | |
public function Align( $position = "left" ) { | |
if ( $this -> handle == false ) return false; | |
switch ( $position ) { | |
case "right": | |
$set = $this->kbyte( 50 ); | |
break; | |
case "center": | |
$set = $this->kbyte( 49 ); | |
break; | |
default: | |
$set = $this->kbyte( 48 ); | |
} | |
return $this -> Append( $this->kbyte( 27 ) . "a" . $set ); | |
} | |
// XFeed | |
// ===== | |
// NOTE: could lead to unexpected behavior! | |
// ===== | |
// $lines: integer (lines to feed) | |
public function XFeed( $lines = 1 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $lines > 1 ) $feed = $this->kbyte( 2 ); | |
else $feed = $this->kbyte( $lines ); | |
return $this -> Append( $this->kbyte( 27 ) . "d" . $feed ); | |
} | |
// XFeed Reverse | |
// ============= | |
// NOTE: could lead to unexpected behavior! | |
// ============= | |
// $lines: integer (lines to feed) | |
public function XrFeed( $lines = 1 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_reverse != true ) return false; | |
if ( $lines > 1 ) $feed = $this->kbyte( 2 ); | |
else $feed = $this->kbyte( $lines ); | |
return $this -> Append( $this->kbyte( 27 ) . "e" . $feed ); | |
} | |
// Cut | |
// === | |
// $lines: integer (lines to feed before cutting) optional | |
public function Cut( $lines = false ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_cut != true ) return false; | |
if ( $lines != false ) $feed = $this->kbyte( $lines ); | |
else $feed = $this->kbyte( 0 ); | |
return $this -> Append( $this->kbyte( 29 ) . "V" . $this->kbyte( 65 ) . $feed ); | |
} | |
// Drawer Kick | |
// =========== | |
// $pin: 2, 5 or 'both' | |
public function Drawer( $pin = 2 ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_drawer != true ) return false; | |
if ( $pin == 'both' ) { | |
$this -> Drawer( 2 ); | |
return $this -> Drawer( 5 ); | |
}else { | |
if ( $pin == 5 ) $pin = $this->kbyte( 49 ); | |
else $pin = $this->kbyte( 48 ); | |
return $this -> Append( $this->kbyte( 27 ) . "p" . $pin . $this->kbyte( 49 ) . $this->kbyte( 50 ) ); | |
} | |
} | |
// Translate | |
// ========= | |
// $string: string (text data) | |
public function Translate( $string ) { | |
if ( $this -> translate != true ) return $string; | |
if ( !is_array( $this -> translate_find ) or !is_array( $this -> translate_replace ) ) { | |
$this -> error( 'Translation isn\'t set up correctly.' ); | |
return $string; | |
} | |
return str_replace( $this -> translate_find, $this -> translate_replace, $string ); | |
} | |
// Add a Line | |
// ========== | |
// $string: string (print data) | |
// $feed: bool (feed after printing line) | |
public function Line( $string, $feed = true ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_drawer != true ) return false; | |
if ( $this -> translate == true ) $esc_pos_command = $this -> Translate( $string ); | |
else $esc_pos_command = $string; | |
if ( $feed == true ) $esc_pos_command .= $this->kbyte( 10 ); | |
return $this -> Append( $esc_pos_command ); | |
} | |
// NewLine | |
// ======= | |
public function NL( $qnt = 1 ) { | |
if ( $this -> handle == false ) return false; | |
for ( $i=0;$i<$qnt;$i++ ) { | |
$return = $this -> Append( $this->kbyte( 10 ) ); | |
} | |
return (isset($return) ? $return : $this->Append($this->kbyte(10))); | |
} | |
// Print From File | |
// ================================ | |
// (c) 2009 by 'solis' on my forum, | |
// modified by 'Kovu' 01.04.2010. | |
// Thanks for your contribution! | |
// ================================ | |
// $size: integer (0 <=> 255) | |
public function file( $path, $textmode = true ) { | |
if ( $this -> handle == false ) return false; | |
if ( !file_exists( $path ) ) { | |
$this -> error( 'There\'s no such file!' ); | |
return false; | |
} | |
if ( $textmode != true ) { | |
// Send The Entire File | |
// ==================== | |
if ( !$content = file_get_contents( $path ) ) { | |
$this -> error( 'File couldn\'t be opened.' ); | |
return false; | |
} | |
return $this -> Append( $content . $this->kbyte( 10 ) ); | |
}else { | |
// Send as Text-Lines | |
// ================== | |
if ( !$lines = file( $path, FILE_IGNORE_NEW_LINES ) ) { | |
$this -> error( 'File couldn\'t be opened.' ); | |
return false; | |
} | |
foreach ( $lines as $line ) $this -> Line( $line, true ); | |
return true; | |
} | |
} | |
// Barcode Setup | |
// ============= | |
// $height: integer 1 <=> 255 | |
// $width: integer 1 <=> 3 | |
// $text: string 'none', 'above', 'below', 'both' | |
// $font: integer 0 <=> 1 | |
public function BarcodeSetup( $width = false, $height = false, $text = false, $font = false ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_barcode != true ) return false; | |
if ( $width !== false && is_numeric( $width ) ) { | |
$this -> Append( $this->kbyte( 29 ) . "w" . $this->kbyte( $width ) ); | |
} | |
if ( $height !== false && is_numeric( $height ) ) { | |
$this -> Append( $this->kbyte( 29 ) . "h" . $this->kbyte( $height ) ); | |
} | |
if ( $text !== false ) { | |
if ( $text == 'none' ) $text = 0; | |
if ( $text == 'above' ) $text = 1; | |
if ( $text == 'below' ) $text = 2; | |
if ( $text == 'both' ) $text = 3; | |
$this -> Append( $this->kbyte( 29 ) . "H" . $this->kbyte( $text ) ); | |
} | |
if ( $font !== false && is_numeric( $font ) ) { | |
$this -> Append( $this->kbyte( 29 ) . "f" . $this->kbyte( $font ) ); | |
} | |
return true; | |
} | |
// Barcode | |
// ======= | |
// $system: string (system) | |
// $barcode: string (corresponding number of characters) | |
public function Barcode( $barcode, $system = 'UPC-A', $n_chars = '0' ) { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> pcp_barcode != true ) return false; | |
$system = strtoupper( $system ); | |
switch ( $system ) { | |
case 'UPC-A': | |
$m = $this->kbyte( 65 ); | |
if ( $n_chars > 12 ) $n = $this->kbyte( 12 ); | |
elseif ( $n_chars < 11 ) $n = $this->kbyte( 11 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'UPC-E': | |
$m = $this->kbyte( 66 ); | |
if ( $n_chars > 12 ) $n = $this->kbyte( 12 ); | |
elseif ( $n_chars < 11 ) $n = $this->kbyte( 11 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'JAN13': | |
case 'EAN13': | |
$m = $this->kbyte( 67 ); | |
if ( $n_chars > 13 ) $n = $this->kbyte( 13 ); | |
elseif ( $n_chars < 12 ) $n = $this->kbyte( 12 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'JAN8': | |
case 'EAN8': | |
$m = $this->kbyte( 68 ); | |
if ( $n_chars > 8 ) $n = $this->kbyte( 8 ); | |
elseif ( $n_chars < 7 ) $n = $this->kbyte( 7 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'CODE39': | |
$m = $this->kbyte( 69 ); | |
if ( $n_chars > 255 ) $n = $this->kbyte( 255 ); | |
elseif ( $n_chars < 1 ) $n = $this->kbyte( 1 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'ITF': | |
$m = $this->kbyte( 70 ); | |
if ( $n_chars > 255 ) $n = $this->kbyte( 255 ); | |
elseif ( $n_chars < 1 ) $n = $this->kbyte( 1 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'CODABAR': | |
case 'NW7': | |
$m = $this->kbyte( 71 ); | |
if ( $n_chars > 255 ) $n = $this->kbyte( 255 ); | |
elseif ( $n_chars < 1 ) $n = $this->kbyte( 1 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'CODE93': | |
$m = $this->kbyte( 72 ); | |
if ( $n_chars > 255 ) $n = $this->kbyte( 255 ); | |
elseif ( $n_chars < 1 ) $n = $this->kbyte( 1 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
case 'CODE128': | |
$m = $this->kbyte( 72 ); | |
if ( $n_chars > 255 ) $n = $this->kbyte( 255 ); | |
elseif ( $n_chars < 2 ) $n = $this->kbyte( 2 ); | |
else $n = $this->kbyte( $n_chars ); | |
break; | |
default: | |
$this -> error( 'Unknown barcode system.' ); | |
return false; | |
} | |
$this -> Append( $this->kbyte( 29 ) . "k" . $m . $n . $barcode ); | |
return true; | |
} | |
// Send & Print Image | |
// ================== | |
// $bit_array: BitArrayGtk / BitArrayGD object | |
// | |
// or | |
// | |
// $path: string (image path [*.png, *.jpg, *.bmp]) | |
public function Image( $bit_array = false, $path = false ) { | |
if ( $this -> handle == false ) return false; | |
if ( $bit_array == false && $path == false ) return false; | |
if ( $this -> pcp_logo != true ) return false; | |
if ( $bit_array == false ) { | |
// Check File | |
// ========== | |
if ( !file_exists( $path ) ) { | |
$this -> error( 'Invalid file path!' ); | |
return false; | |
} | |
if ( class_exists( 'BitArrayGtk' ) ) { | |
$bit_array = new BitArrayGtk( $path ); | |
}elseif ( class_exists( 'BitArrayGD' ) ) { | |
$bit_array = new BitArrayGD( $path ); | |
}else { | |
$this -> error( 'Please load the PHP-GTK or the GD module.' ); | |
return false; | |
} | |
} | |
// Check Bit-Array! | |
// ================ | |
if ( $bit_array -> width == false or $bit_array -> height == false or | |
!is_array( $bit_array -> dots ) ) { | |
$this -> error( 'Invalid bit-array data!' ); | |
return false; | |
} | |
// Prepare For Printing Image | |
// ========================== | |
$this -> Output(); // empty buffer | |
$this -> Append( $this->kbyte( 27 ) . "3" . $this->kbyte( 24 ) ); // 24 dot line spacing | |
// Calculate Low and High bytes [width = nL + (nH * 256)] | |
// ====================================================== | |
$nhigh = substr( $bit_array -> width / 256, 0, 1 ); | |
$nlow = $bit_array -> width - ( $nhigh * 256 ); | |
// Process 24-bit-wise | |
// =================== | |
$offset = '0'; | |
while ( $offset < $bit_array -> height ) { // walk through lines | |
$this -> Append( $this->kbyte( 27 ) . "*" . $this->kbyte( 33 ) ); // 24 dot double density | |
$this -> Append( $this->kbyte( $nlow ) . $this->kbyte( $nhigh ) ); // low byte and high byte | |
for ( $x = 0; $x < $bit_array -> width; ++$x ) { // walk through columns | |
for ( $k = 0; $k < 3; ++$k ) { // 24 dots = 24 bits = 3 bytes ($k) | |
$byte = 0; // start a byte | |
for ( $b = 0; $b < 8; ++$b ) { // 1 byte = 8 bits ($b) | |
$y = ( ( ( $offset / 8 ) + $k ) * 8 ) + $b; // calculate $y position | |
$i = ( $y * $bit_array -> width ) + $x; // calculate pixel position | |
// check if bit exists, if not, zero it | |
// ==================================== | |
if ( isset( $bit_array -> dots[$i] ) ) $bit = $bit_array -> dots[$i]; | |
else $bit = '0'; | |
$byte |= $bit << ( 7 - $b ); // shift bit and record byte | |
} | |
$this -> Append( $this->kbyte( $byte ) ); // attach the byte | |
} | |
} | |
$offset += 24; | |
$this -> Append( $this->kbyte( 10 ) ); // line feed | |
} | |
// Finish | |
// ====== | |
$this -> Append( $this->kbyte( 27 ) . "2" ); // reset line spacing | |
$cached_file = explode( '/', $path ); | |
$cached_file = $cached_file[count( $cached_file ) -1]; | |
if ( !file_exists( "../img/old/cache/".substr( $cached_file, 0, -3 )."bit" ) ) { | |
$fp = fopen( "../img/old/cache/".substr( $cached_file, 0, -3 )."bit", "w" ); | |
fwrite( $fp, $this->document ); | |
fclose( $fp ); | |
} | |
$this -> Output(); // send to printer | |
} | |
// Print Buffer | |
// ============ | |
public function kbyte($val){ | |
return pack('C', $val); | |
} | |
public function Output() { | |
if ( $this -> handle == false ) return false; | |
if ( $this -> document == false ) return false; | |
if ( $this -> output == 'printer_system' ) { | |
// Print Through Printer System | |
// ============================ | |
if ( $this -> os == "windows" ) { | |
// Windows | |
// ======= | |
if ( printer_write( $this -> handle, $this -> document ) == false ) { | |
$this -> error( 'Couldn\'t write to the printer.' ); | |
return false; | |
} | |
} else { | |
// Unix, CUPS | |
// ========== | |
$command = 'lpr -P "' . $this -> printer . '"'; | |
$pipe = popen( "$command" , 'w' ); | |
if ( !$pipe ) { | |
$this -> error( 'Pipe failed.' ); | |
return false; | |
} | |
fputs( $pipe, $this -> document ); | |
pclose( $pipe ); | |
} | |
}else { | |
// Print On Port Directly | |
// ====================== | |
if ( fwrite( $this -> handle, $this -> document ) == false ) { | |
$this -> error( 'Couldn\'t write to the printer.' ); | |
return false; | |
} | |
} | |
$this -> document = false; | |
return true; | |
} | |
// Close Printer | |
// ============= | |
public function Close( $print = true ) { | |
if ( $this -> handle == false ) return false; | |
if ( $print == true ) $this -> Output(); | |
if ( $this -> output == 'printer_system' && $this -> os == 'windows' ) { | |
printer_close( $this -> handle ); | |
}elseif ( $this -> output == 'port' ) { | |
fclose( $this -> handle ); | |
} | |
$this -> handle = false; | |
} | |
// Destructor | |
// ========== | |
public function __destruct() { | |
$this -> Close( false ); | |
} | |
} | |
// Create Bit Array (GTK-Version) | |
// ================================= | |
// This class uses PHP-GTK's image | |
// manipulation functions to produce | |
// a bit array that can be converted | |
// into Epson's bit image standard. | |
if ( class_exists( 'gtk' ) ) { | |
class BitArrayGtk { | |
var $dots; | |
var $width; | |
var $height; | |
public function __construct( $path ) { | |
if ( !file_exists( $path ) ) { | |
$this -> width = false; | |
$this -> height = false; | |
$this -> dots = false; | |
}else { | |
$pixbuf = GdkPixbuf::new_from_file( $path ); | |
$this -> width = $pixbuf -> get_width(); | |
$this -> height = $pixbuf -> get_height(); | |
$this -> dots = array(); | |
// Check & Store Pixels | |
// ==================== | |
if ( $this -> width > 575 ) $this -> width = 575; // crop image if needed | |
for ( $hi = 0; $hi < $this -> height; $hi++ ) { | |
for ( $wi = 0; $wi < $this -> width; $wi++ ) { | |
$pixel = $pixbuf -> get_pixel( $wi, $hi ); | |
if ( $pixel !== false ) { | |
$r = ( $pixel & 0xff000000 ) >> 24; | |
$g = ( $pixel & 0x00ff0000 ) >> 16; | |
$b = ( $pixel & 0x0000ff00 ) >> 8; | |
$a = ( $pixel & 0x000000ff ); | |
if ( $r < 0 ) $r = 256 + $r; | |
// White composes itself of R255, G255 and B255. | |
// I'm using 200 to avoid problems with JPEG | |
// compressed images. Everything with a lower value | |
// will be interpreted as black (normally R0, G0, B0). | |
if ( $r > 200 && $g > 200 && $b > 200 ) $this -> dots[] = '0'; | |
else $this -> dots[] = '1'; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
// Create Bit Array (GD-Version) | |
// ================================= | |
// This class uses PHP's GD image | |
// manipulation functions to produce | |
// a bit array that can be converted | |
// into Epson's bit image standard. | |
if ( extension_loaded( 'gd' ) ) { | |
class BitArrayGD { | |
var $dots; | |
var $width; | |
var $height; | |
public function BitArrayGD( $path ) { | |
if ( !file_exists( $path ) ) { | |
$this -> width = false; | |
$this -> height = false; | |
$this -> dots = false; | |
}else { | |
list( $width, $height, $type, $attr ) = getimagesize( $path ); | |
$continue = true; | |
switch ( $type ) { | |
case IMG_JPG: | |
$im = imagecreatefromjpeg( $path ); | |
break; | |
case IMG_PNG: | |
$im = imagecreatefrompng( $path ); | |
break; | |
case IMG_WBMP: | |
$im = imagecreatefromwbmp( $path ); | |
break; | |
default: | |
echo "ERROR: Invalid image file type.\n"; | |
$this -> width = false; | |
$this -> height = false; | |
$this -> dots = false; | |
$continue = false; | |
} | |
if ( $continue == true ) { | |
$this -> width = $width; | |
$this -> height = $height; | |
$this -> dots = array(); | |
// Check & Store Pixels | |
// ==================== | |
if ( $this -> width > 575 ) $this -> width = 575; // crop image | |
for ( $hi = 0; $hi < $this -> height; $hi++ ) { | |
for ( $wi = 0; $wi < $this -> width; $wi++ ) { | |
$pixel = imagecolorat( $im, $wi, $hi ); | |
$rgb = imagecolorsforindex( $im, $pixel ); | |
if ( $rgb['red'] < 0 ) $rgb['red'] = 256 + $rgb['red']; | |
// White composes itself of R255, G255 and B255. | |
// I'm using 200 to avoid problems with JPEG | |
// compressed images. Everything with a lower value | |
// will be interpreted as black (normally R0, G0, B0). | |
if ( $rgb['red'] > 200 && $rgb['green'] > 200 && $rgb['blue'] > 200 ) $this -> dots[] = '0'; | |
else $this -> dots[] = '1'; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
/** | |
* printFile method | |
* Print in file | |
* | |
* @access public | |
* @return void | |
* @since 2.2 | |
* @version 2.2 | |
* @author Patrick Maciel | |
* @throws Patrick Maciel | |
*/ | |
public function printFile() | |
{ | |
$file = ''; | |
$file.= "\n\n"; | |
$file.= "\n===============================================================================\n\n"; | |
$par = 1; | |
$length1 = 0; | |
$length2 = 0; | |
$spaces = 0; | |
$max = 72; | |
foreach ( $this->infs as $obj ) { | |
if ($par == 1) { | |
$file.= "{$obj->label}: {$obj->value}"; | |
$length1 = strlen("{$obj->label}: {$obj->value}"); | |
$par++; | |
} else { | |
$length2 = strlen("{$obj->label}: {$obj->value}"); | |
$file.= str_repeat(" ", (50 - ($length1))); | |
$file.= "{$obj->label}: {$obj->value} \n"; | |
$par = 1; | |
} | |
} | |
$file.= "\n-------------------------------------------------------------------------------\n"; | |
$file.= "QTY ITEM "; | |
$file.= str_repeat(" ", 40); | |
$file.= "PRICE "; | |
$file.= str_repeat(" ", 16); | |
$file.= "AMOUNT \n"; | |
foreach ( $this->itens as $obj ) { | |
$file.= "{$obj[0]}"; | |
$file.= str_repeat(" ", (5 - strlen($obj[0]))); | |
$file.= "{$obj[1]}"; | |
$file.= str_repeat(" ", (45 - strlen($obj[1]))); | |
$file.= "{$obj[2]}"; | |
$file.= str_repeat(" ", (22 - strlen($obj[2]))); | |
$file.= "{$obj[3]} \n"; | |
} | |
$file.= "\n-------------------------------------------------------------------------------\n"; | |
foreach ( $this->summary as $obj ) { | |
$file.= str_repeat(" ", 51); | |
$file.= $obj->label; | |
$file.= str_repeat(" ", (22 - strlen($obj->label))); | |
$file.= $obj->value; | |
$file.= "\n"; | |
} | |
$file.= "\n-------------------------------------------------------------------------------\n"; | |
$file.= "\n"; | |
$file.= str_repeat(" ", 51); | |
$file.= $this->messages[0]->message . "\n"; | |
$file.= "\n===============================================================================\n\n"; | |
$file.= "\n\n"; | |
$file.= "\n"; | |
$fh = fopen('C:/workspace/spool.txt', 'a'); | |
fwrite($fh, $file); | |
fclose($fh); | |
// file_put_contents('C:/workspace/spool.txt', $file); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=============================================================================== | |
Table: 14 Check: 381718 | |
Party: 2 Waiter: team01 | |
Date: 11/27/2013 11:27:09 | |
------------------------------------------------------------------------------- | |
QTY ITEM PRICE AMOUNT | |
1 Amstel Light 6.00 6.00 | |
3 Blue Moon 6.00 18.00 | |
------------------------------------------------------------------------------- | |
Total 24 | |
Tax 1.44 | |
Amount Due 25.44 | |
------------------------------------------------------------------------------- | |
18% SUGGESTED TIP: 4.32 | |
=============================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment