naeemqaswar
8/6/2018 - 3:40 PM

POS Print Driver for PHP

<?php
// Plugin: ESC/POS Print Driver for PHP (https://github.com/mike42/escpos-php)
function print_receipt( $invoice_data, $invoice_items ) {
  
  try {
  	// Enter the share name for your USB printer here
  	$connector = new WindowsPrintConnector( "CITIZEN CBM1000 Type II" );
  
  	/* Print a "Hello world" receipt" */
  	$printer = new Printer( $connector );
  
  	$printer->setEmphasis( true );
  	$printer->setPrintLeftMargin( 128 );
  	$printer->setTextSize( 2, 6 );
  	$printer->text( "Point of Sale\n" );
  	$printer->setPrintLeftMargin( 0 );
  	$printer->setEmphasis( false );
  
  	$printer->text( "------------------------------------------------". "\n" );
  	$printer->setPrintLeftMargin( 4 );
  
  	$printer->setJustification();
  	$printer->text( "\n" );
  	$printer->text( "Invoice Number : " . $invoice_data['invoice_number'] . "\n" );
  	$printer->text( "===============================================\n" );
  	$printer->text( "Item Name          QTY     Price    Total\n" );
  	$printer->text( "===============================================\n" );
  
  	foreach ( $invoice_items as $item ) {
  		$product_details = Product::where( 'id', $item['product_id'] )->get();
  		$product_details = $product_details[0];
  
  		$printer->text( $product_details->title . " \n" );
  		$printer->text( $item['qty'] . " | $" . $product_details->sale_price . " | $" . ( $item['qty'] * $product_details->sale_price ) . " \n" );
  		$printer->setJustification();
  		$printer->text( "-----------------------------------------------". "\n" );
  	}
  
  	$printer->setJustification(1);
  	$printer->text( "             SUB TOTAL  : $   " . $invoice_data['sub_total'] . " \n" );
  	$printer->text( "                 DISC   : %   " . $invoice_data['discount'] . " \n" );
  	$printer->text( "           Grand TOTAL  : $   " . $invoice_data['total'] . " \n" );
  	$printer->text( "-----------------------------------------------". "\n" );
  	$printer->text( "Thank you for visiting us !\n" );
  	$printer->setJustification();
  	$printer->text( "-----------------------------------------------". "\n" );
  	$printer->text( "Cashier : " . $invoice_data['employee_name'] . "       \n" );
  	$printer->text( date( "d/m/Y H:i:s" ) . "\n" );
  	$printer->text( "\n" );
  	$printer->text( "\n" );
  	$printer->cut();
  
  	$printer->close();
  
  } catch ( Exception $e ) {
  	$message = $e->getMessage();
  	echo $message;die;
  }
}
?>