escpos-php icon indicating copy to clipboard operation
escpos-php copied to clipboard

Print from the web

Open azorgh opened this issue 5 years ago • 6 comments

Hello everyone,

I've a app wich need to print to a EPSON TM-T20 from the web. I've read the part of the README where you suggest to use QZ, Chrome Raw Print etc, but I had some ideas before read that.

My base idea was :

  • Generate a TXT file on the server with escpos-php library.
  • Create a litlle app on the client, wich receive a socket event and download the txt file
  • Use the EPSON Send Data Tool application to send the TXT file to the printer

For my firsts tests, I've generated a basic TXT like this :

private function genereate(){
        $connector = new FilePrintConnector(storage_path('1.txt'));
        $printer = new Printer($connector);

        $first_line = $this->addSpaces("TK-0017868", 20) . $this->addSpaces(Carbon::now()->format('d/m/Y H:i'), 20, 'BEFORE') . "\n";
        $printer->text($first_line);
        $printer->feed(3);
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->setEmphasis(true);
        $printer->text("Société\n");
        $printer->text("ZAC de la vallée\n");
        $printer->text("avenue Fief Rose\n");
        $printer->text("95000 Paris\n");
        $printer->text("Tél : 01 16 17 18 19\n");
        $printer->setEmphasis(false);
        $printer->setJustification(Printer::JUSTIFY_LEFT);
        $printer->feed(1);
        $printer->text("Client  : MICHEL\n");
        $printer->text("Vendeur : Responsable magasin\n");
        $printer->feed(2);
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->text("Bonjour\n");
        $printer->setJustification(Printer::JUSTIFY_LEFT);
        $printer->text("----------------------------------------\n");
        $printer->text("----------------------------------------\n");
        $printer->text($this->addSpaces('Total brut HT', 30, 'BEFORE') . $this->addSpaces('113,96', 10, 'BEFORE') . "\n");
        $printer->text($this->addSpaces('Total Net HT', 30, 'BEFORE') . $this->addSpaces('113,96', 10, 'BEFORE') . "\n");
        $printer->text($this->addSpaces('Total TVA', 30, 'BEFORE') . $this->addSpaces('22,79', 10, 'BEFORE') . "\n");
        $printer->text($this->addSpaces('Net TTC', 30, 'BEFORE') . $this->addSpaces('136,75', 10, 'BEFORE') . "\n");
        $printer->text($this->addSpaces('Taux normal sur', 20, 'BEFORE') . $this->addSpaces('113,96', 10, 'BEFORE') . $this->addSpaces('22,79', 10, 'BEFORE') . "\n");
        $printer->text($this->addSpaces('Paiement par Carte bancaire', 30) . $this->addSpaces('136,75', 10, 'BEFORE') . "\n");
        $printer->cut();
        $printer->close();
}

private function addSpaces($string = '', $valid_string_length = 0, $type = "AFTER"){
        if (strlen($string) < $valid_string_length) {
            $spaces = $valid_string_length - strlen($string);
            for ($index1 = 1; $index1 <= $spaces; $index1++) {
                if($type == "AFTER")
                    $string = $string . ' ';
                else
                    $string = ' ' . $string;
            }
        }

        return $string;
    }

When I use the EPSON tool for send the txt file, everything is fine. The printer start to print. But the result is not the one expected !

Here is the visual result (I've hide some private datas ;) ) Printer---EPSON-TM-T20

Numbers aren't displayed, carriage return and spaces too.

Any ideas ? Maybe my workflow is not correct to print datas ?

Thanks for your help <3

azorgh avatar Apr 09 '19 08:04 azorgh

Your idea is valid, to print from the web you would need a client which can see the printer and is actually capable of doing raw printing.

Somewhere between this PHP code and the printer, I'm guessing the binary is being munged.

So first, the transport over the network. I would also suggest reading this example for the PHP side, which will provide the file over HTTP without using a temp file.

Next, you just need to download the print file and send it to the printer raw, but you missed some important details: Which operating system are you on, and how is your printer connected? (USB, raw, ethernet, etc). I don't use the Epson print tool, so I'm not sure if that's a valid part of a raw printing pipeline or not.

mike42 avatar May 04 '19 11:05 mike42

I have the same issue. I was thinking in saving all data in a buffer (like the example, i guess) or file, and then send it to the client using JS, but Idk what libary use for the printer. Do you know if I can use the ePOS Epson library or which type of format should i use to pass data from server to client and print it on a Thermal Epson Printer (TM-T88V). Server: Linux Centos Client: Windows 10

JulioSullivan avatar May 09 '19 19:05 JulioSullivan

check your socket buffer

hamzeh1388 avatar Jun 01 '19 11:06 hamzeh1388

Has anyone figured out this ? I'm still hang in between.

onally01 avatar Aug 05 '19 14:08 onally01

"Create a litlle app on the client, wich receive a socket event and download the txt file" in socket source code ,you must have this line: $header = socket_read($socket, 1024); //read data sent by the socket 1024 is read buffer change to 10240

hamzeh1388 avatar Aug 24 '19 17:08 hamzeh1388

Printing on local will work well with this package.

However, to print online, you will need to develop a desktop service which has to be installed on the local computer where you need printing to be done. Operating System does not matter in this case, you can be hosting on Linux based server and printing on a windows server. This desktop service interfaces between the online service and the local service.

I resolved this by developing an electron js service, which can receive commands from the online print command and then communicate those commands to the printer through USB.

mutiemule avatar Apr 30 '22 09:04 mutiemule