Nginx Sever with local printers on window

120 Views Asked by At

I have deployed my Laravel App to AWS and using NGINX server. I want to print some receipts from server to the local printers. I am using this package mike42/escpos-php. and here is my code

try {
        $connector = new \Mike42\Escpos\PrintConnectors\WindowsPrintConnector("POS-80C");

        /* Print a "Hello world" receipt" */
        $printer = new \Mike42\Escpos\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";
    }```

I have enabled sharing of this printer *POS-80C* from windows. 

But whenever I try to print, I rceived this error.

Command "smbclient '//ip-172-31-43-229/POS-80C' -c 'print -' -N -m SMB2" failed with exit code 1: do_connect: Connection to ip-172-31-43-229 failed (Error NT_STATUS_CONNECTION_REFUSED)

Here *ip-172-31-43-229* is IP of server on AWS.


*smbclient* is working fine on nginx. What can be the issue?
1

There are 1 best solutions below

0
Theofanis On

What you are trying to achieve is very complicated, hard but possible.

  1. You have to have a way to access the local network from the cloud.
    • For example local network should have a static IP or DDNS.
  2. Setup the appropriate channel to connect to the appropriate port.
    • For example setup NAT on router on local network.
    • You can also achieve this by setting up a tunnel from a machine connected to the printer from the local network to that cloud server.
  3. Now you have should have reached a point where your cloud server can "see"/ping the network printer or the PC the printer is connected to.
    • Find a supporting protocol to connect to the printer/PC. In the past a have a simple printer connected to a Windows PC and from there I shared the printer. The printer was visible to the Ubuntu server using CUPS through the SAMBA protocol. The setup url is pretty weird but there is enough documentation online.
  4. Now you have reached a point where the server can send print jobs to the printer and the printer should print them. In this step you need a way to fire this action through PHP.
    1. Create a file with the data you want to print.
    2. Simply call the appropriate command from PHP to bash, e.g. exec("lpr -l -P $file $printer") or Process::fromShellCommandline("lpr -l -P $file $printer"). No need for a particular composer package or pecl module.

I have built two systems with Laravel on Ubuntu printing on a Windows shared printer using the procedure I described. It took me much time to find this solution. I would suggest implementing it in a smaller scale, meaning printing on another printer/PC in the same network, so you first solve the problems regarding remote printing. Then move to the final step where the server is on the cloud and the printer/PC on a local network. As I said you are trying to implement pretty advanced stuff.