Skip to content

Instantly share code, notes, and snippets.

@ozzpy
Created January 18, 2018 15:05
Show Gist options
  • Save ozzpy/068d6281f02c41ff9a2962749e7f44f4 to your computer and use it in GitHub Desktop.
Save ozzpy/068d6281f02c41ff9a2962749e7f44f4 to your computer and use it in GitHub Desktop.
EXEMPLO PHP - Bematech 4200 TH - NAO FISCAL - USB - octoBema - MP2032.DLL - WINDOWS - pt_BR
<?php
class Invizzo_Bematech
{
private $_registro = "310DBDAC-85FF-4008-82A8-E22A09F9460B";
private $_impressora = 7;
private $_porta = "USB";
private $_texto;
private $_texto_barcode;
private function arrumaAcentos($texto) {
$acentos = array(
"Ç","Ã","Õ","Â","Ô","Á","À","Ó","Ò","Í","Ì","Ú","Ù",
"É","È","Ê","ê","é","è",
"ç","ã","õ","â","ô","á","à","ó","ò","í","ì","ú","ù"
);
$acentosCHR = array(
chr(199),chr(195),chr(213),chr(194),chr(212),chr(193),chr(192),chr(211),chr(210),chr(205),chr(204),chr(218),chr(217),
chr(201),chr(200),chr(202),chr(234),chr(233),chr(232),
chr(231),chr(227),chr(245),chr(226),chr(244),chr(225),chr(224),chr(243),chr(242),chr(237),chr(236),chr(250),chr(249)
);
return str_replace($acentos,$acentosCHR,$texto);
}
private function arrumaFormatacao($texto)
{
//$formata = array("[NEGRITO]","[/NEGRITO]","[SUBLINHADO]","[/SUBLINHADO]");
//$formataHEX = array("\x1B\x45","\x1B\x46","\x1B\x2D\x01","\x1B\x2D\x00");
$formata = array("[NEGRITO]","[/NEGRITO]");
$formataHEX = array("\x1B\x45","\x1B\x46");
return str_replace($formata,$formataHEX,$texto);
}
private function error($error) {
throw new Exception($error);
}
public function setTextoBarcode($textoBar) {
$this->_texto_barcode = str_replace('.','-',$textoBar);
}
public function getTextoBarcode() {
return (string) $this->_texto_barcode;
}
public function setTexto($string) {
$this->_texto = $string;
}
public function getTexto(){
return (string) substr($this->_texto,1,strlen($this->_texto));
}
public function init() {
# alguns comandos
$centerON = "\x1B\x61\x01";
$centerOFF = "\x1B\x61\x00";
# drivers
$COM = new COM("{".$this->_registro."}");
$COM->ConfiguraModeloImpressora ($this->_impressora);
# inicia comunicacao
$init = $COM->IniciaPorta( $this->_porta );
if( $init <= 0 ){
$this->error( "SEM COMUNICACAO" );
} else{
if( !isset($this->_texto) ){
$this->error(" Nenhum texto para impressao");
} else{
if( false !== strpos($this->_texto,"[CORTAR]") ){
$partes = explode("[CORTAR]",$this->_texto);
for($i=0;$i<=(count($partes)-1);$i++):
$COM->ConfiguraCodigoBarras(150,0,0,0,50);
$COM->ComandoTX($centerON,strlen($centerON));
$COM->ImprimeCodigoBarrasCODE39($this->getTextoBarcode());
$COM->ComandoTX($centerOFF,strlen($centerOFF));
$COM->BematechTX("\n\r");
$partes[$i] = $this->arrumaAcentos($partes[$i]);
$partes[$i] = $this->arrumaFormatacao($partes[$i]);
$COM->BematechTX( $partes[$i] . "\n\r\\o/\n\r");
$COM->AcionaGuilhotina(0);
endfor;
} else {
$COM->ConfiguraCodigoBarras(150,0,0,0,50);
$COM->ComandoTX($centerON,strlen($centerON));
$COM->ImprimeCodigoBarrasCODE39($this->getTextoBarcode());
$COM->ComandoTX($centerOFF,strlen($centerOFF));
$COM->BematechTX("\n\r");
# para a impressora
$this->_texto = $this->arrumaAcentos($this->_texto);
$this->_texto = $this->arrumaFormatacao($this->_texto);
$COM->BematechTX( $this->_texto . "\n\r\\o/\n\r");
# aciona corte
$COM->AcionaGuilhotina(0);
}
# fecha comunicacao
$COM->FechaPorta();
}
}
}
}
?>
@Angel28693
Copy link

Mi WhatsApp es +584120578099 si pudieras contactarme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment