'Printing to a Thermal Receipt Printer using PHP

I have the following code that was supposed to make it able to print something to a thermal receipt printer.

if ($_GET['action'] == 'print')
{
    $printer = "\\\\localhost\\zebra";

    // Open connection to the thermal printer
    $fp = fopen($printer, "w");
    if (!$fp){
      die('no connection');
    }

    $data = " PRINT THIS ";

    // Cut Paper
    $data .= "\x00\x1Bi\x00";

    if (!fwrite($fp,$data)){
      die('writing failed');
    }

}

Although, when I run it nothing happens along with no errors. How does one print from PHP?



Solution 1:[1]

try this ...

<?php 
if ($_GET['action'] == 'print')
{
$printer = "\\\\localhost\\zebra"; 
if($ph = printer_open($printer)) 
{ 
   $data = " PRINT THIS ";  
   // Cut Paper
   $data .= "\x00\x1Bi\x00";
   printer_write($ph, $data); 
   printer_close($ph); 
} 
else die('writing failed');
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1844933