I have got a Network Printer (POS Printer) connected to my router and I have the printer's IP and Port. I want to send printing instructions directly from the webserver. For example, if I visit example.com/print.php it’ll send the instruction to my local network printer and start printing.
It is important that this web page can be visited from any device on the same network and able to send printing instructions to my local network printer.
This is what I’ve so far, using localhost/print.php I can send printing instructions to local network printer successfully using any browser because they are all in the same network. But localhost doesn’t work on iPad.
This is what I have so far, printing via localhost using computer browsers.
I’m using this GitHub code escpos-php to print from localhost to local network printer successfully and here is the code example:
<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42EscposPrinter;
use Mike42EscposPrintConnectorsNetworkPrintConnector;
try {
$connector = new NetworkPrintConnector("192.168.1.87", 9100);
$printer = new Printer($connector);
$printer -> text("Hello World!
");
$printer -> cut();
$printer -> close();
} catch (Exception $e) {
echo "Couldn't print to this printer: " . $e -> getMessage() . "
";
}
?>
Now, this code is very easy to execute and works like a charm but when I tried to run the same code from my server which it couldn’t connect to the printer because they are not in the same network. I checked this code's authors FAQ section and found this answer.
Solution 1: Architect your application so that the server can see your printer.
Solution 2: Use an application that runs client-side to deliver print data.
The second solution is a good solution to this problem but this is what I’m trying to achieve.
This is what I'm looking for, printing via the webserver using any device within the local network.
Note that the computer connected to the router is no longer in action.
When I access the example.com/print.php web page, it sends instructions to the printer via the webserver but as you can see in this scenario the server failed to communicate with the printer.
In the first solution, the author provides these options:
Option 1: Run your server on the LAN instead, and read the section above about printing over the network
Option 2: Set up a VPN so that your cloud-hosted server can also access the LAN
Option 3: Expose the printer via some other secure tunnel to the server, via SSH or TLS
The first option is not for me I guess but the second option is setting up a VPN so that my cloud-hosted server can also access the LAN. This is a good solution but at some point, there will be hundreds of users like me using hundreds of printers via the same webserver and I’m guessing using a VPN doesn’t work very well in this scenario.
The third option is promising at this point how do I expose the printer via a secure tunnel to the server via SSH or TLS, if the computer is no longer in action.
Is there any simple way to send printing instructions from the webserver to the local network printer just using the printer's IP and Port? If that means exposing the printer to the internet or removing firewalls.
question from:
https://stackoverflow.com/questions/65873089/how-to-print-from-the-webserver-to-the-local-network-printer-directly-using-phon