G M Kibria
G M Kibria

Reputation: 21

Printing POS (EPSON TM-U220D) Printer Using Mike42/Escpos-php PHP Library

We are working on a PHP based web application for a money exchanger. The money exchanger we are working for use POS (EPSON TM-U220D) Printer for customer receipt, So we are looking for a way to print from the web application to the POS Printer and We tried mike42/escpos-php PHP Library and Use This Getting a USB receipt printer working on Windows Tutorial but it doesn't work. This is the first time of our working with POS printer, So we are totally lost. Can anyone help up with this? Thanks in advance. Detail :

Printer : [EPSON TM-U220D][1]
PHP Library : [Mike42/Escpos-php][1]
Tutorial : [Getting a USB receipt printer working on Windows][1]
Programming Languages : [PHP 7.2.0][1]
Web Server : [Apache][1]
Database : [MySQL][1]

Code :

    /*
    // Call this file 'hello-world.php' 
    require __DIR__ . '/inc/escpos/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();


    */
    /* Change to the correct path if you copy this example! */
    require __DIR__ . '/inc/escpos/vendor/autoload.php';
    use Mike42\Escpos\Printer;
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    /**
    * Install the printer using USB printing support, and the "Generic / Text     Only" driver,
    * then share it (you can use a firewall so that it can only be seen locally).
    *
    * Use a WindowsPrintConnector with the share name to print.
    *
    * Troubleshooting: Fire up a command prompt, and ensure that (if your printer is shared as
    * "Receipt Printer), the following commands work:
    *
    *  echo "Hello World" > testfile
    *  copy testfile "\\%COMPUTERNAME%\Receipt Printer"
    *  del testfile
    */
    try {
    // Enter the share name for your USB printer here
    $connector = null;
    //$connector = new WindowsPrintConnector("Receipt Printer");
    /* Print a "Hello world" receipt" */
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();

    /* Close printer */
    $printer -> close();
    } catch (Exception $e) {
    echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
    }

Upvotes: 2

Views: 5711

Answers (2)

Meika Welliana
Meika Welliana

Reputation: 1

You supply null to your Printer.

$connector = null;
$printer = new Printer($connector);

Instead, you should try this:

$printername="EPSON_LX_310";        
$connector = new WindowsPrintConnector($printername);
$printer = new Printer($connector);

Upvotes: 0

buffallo_soja
buffallo_soja

Reputation: 41

Hope this will help someone else. You need to first install drivers. Then write your code as below. The WindowsPrintConnector is the one used to create the connector object. This works for me with Laravel.

use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\Printer;
require 'C:\wamp64\www\myappname\vendor\autoload.php';
try {
    // Enter the share name for your USB printer here


    $connector = null;
        $connector = new WindowsPrintConnector("E-PoS 80mm Thermal Printer");
        /* Print a "Hello world" receipt" */
        $printer = new Printer($connector);
        $printer -> text("Hello World\n");
        $printer -> cut();

        /* Close printer */
        $printer -> close();
    } catch (Exception $e) {
        echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
    }

Upvotes: 1

Related Questions