mike42 / escpos-php

PHP library for printing to ESC/POS-compatible thermal and impact printers
Other
2.54k stars 862 forks source link

Print receipt from webserver application #688

Closed itajackass closed 5 years ago

itajackass commented 5 years ago

I don't find an answer about it in documentation.

I'd like to create a web application in php hosted on www.myshopexample.com/adminarea/

Inside phisical shop i've a PC connected to internet where each employee is connected to web application and he can simulate a cash pointer / draw pointer where on each sale confirm, my quantity of products is updated following # of sale.

Now i'd like to connect a printer (thermal printer) on each PC in the shop.

My question is: can my webapp hosted in a remote server, tell to print a receipt on local printer installed on PC shop? If yes, is there an example? thanks

RenanGaleno commented 5 years ago

It's possible. I have a setup with something like yours one. Each client has a USB connected printer, and runs a print server connected to the printer (search for RawPrintServer), then the app uses NetworkPrintConnector to connect to the client IP.

$connector = new NetworkPrintConnector($request->ip(), 9100);
$printer = new Printer($connector);

With a local server, it is as simple as this. Running on web, your mileage may vary...

mike42 commented 5 years ago

The above comment is correct: LAN printing is simple, but internet printing is a different beast.

I think that you need to understand that by designing your app to work within a browser, you have chosen to have no raw real hardware access on the client side (which you would be writing ing Javascript, not PHP). Running your PHP web app on the LAN is by far the simplest way to get that hardware access: you can hit port 9100 or shared printers on your POS terminals over the LAN, or you can even run a USB cable to your server if it's something like a Raspberry Pi under the desk.

There are more complex alternatives which I won't go into, since we have an FAQ for that.

mutiemule commented 2 years ago

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. This is because PHP is a server-side scripting language, so once you put this online, it won't be able to communicate to the local printer. Thus, you need a client service running on the local machine and this is where electron js and node js come into play.

I resolved this by developing an electron js service based on node-escpos package which can receive commands from the online print command and then communicate those commands to the printer through USB.

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.

You can check out the node-escpos printer driver package for some inspiration on how to go about this.

Check the FAQs on how to print online to have some pointers too.