afunnydev
afunnydev

Reputation: 13

Escpos-php with serial printer

I'm trying to print a QR Code with my Epson TM-T88IV serial printer using php. However, my php file installed on a server and I'm able to call it succesfully from a html file. I'm using a library called ESCPOS-PHP (https://github.com/mike42/escpos-php) and the computer is running Windows XP Professional. Here's my php snippet (there's more in the middle, but not needed for the printing action):

<?php
require __DIR__. '/escpos-php-master/Escpos.php'; 
use Mike42\Escpos\Printer; 
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

[...]

try {
    $connector = new WindowsPrintConnector("EPSON TM-T88IV Receipt");
    $printer = new Escpos($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";
}
?>

Seems like I just can't connect to the printer. I also tried with

$connector = new FilePrintConnector("/dev/ttyS0");
$printer = new Printer($connector);

Which is supposed to be the way with serial printer (I'm not sure what I should put instead of "/dev/ttsyS0"). Maybe I shouldn't try to trigger it via the server ? I'm doing it because I can't modify his POS system (Maitre D) and I need an easy way to print QR code on bills. If you know any workaroung, any advice would be appreciated ! Thanks

Upvotes: 0

Views: 5900

Answers (2)

Armando Ferrante
Armando Ferrante

Reputation: 1

via console it works: echo "Hello world" > foo.txt net use "\ARMANDO-PC\pos1" /user:Armando 1235 copy testfile "\ARMANDO-PC\pos1" del testfile

via php code doesn't work:

$connector = new WindowsPrintConnector("smb://Armando:12345@ARMANDO-PC/pos1");

/* Print a "Hello world" receipt" */
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();

/* Close printer */
$printer -> close();

Upvotes: 0

mike42
mike42

Reputation: 1688

Author of escpos-php here.

The escpos-php README suggests that you should try to send data to your printer on the command-line first, because it will allow you to determine how you are going to print before attempting to use the driver.

For example, if you intended to set up your printer on COM1, you can try to type:

echo "Hello world" > COM1

Which corresponds with:

<?php
$connector = new FilePrintConnector("COM1");
$printer = new Escpos($connector);
$printer -> text("Hello World\n");

The WindowsPrintConnector is for connecting to Windows shared printers. This example has some helpful commands to make sure you can print before you open PHP. Eg,

echo "Hello world" > foo.txt
net use "\\computername\Receipt Printer" /user:Bob secret
copy testfile "\\computername\Receipt Printer"
del testfile

This corresponds with:

<?php
$connector = new WindowsPrintConnector("smb://bob:secret@computername/Receipt Printer");
$printer = new Escpos($connector);
$printer -> text("Hello World\n");

In any case, two gotchas:

  • Only use this with Generic/Text only driver. escpos-php will output raw ESC/POS, which will confuse any driver that expects a document.
  • This is server-side. Ensure that PHP is running on the computer that will do the printing, or that you are at least on the internal network if using network printing.

Upvotes: 2

Related Questions