How to print from the webserver to the local network printer directly using phone or tablet browser?

女生的网名这么多〃 提交于 2021-02-08 11:37:59

问题


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 Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;

try {
    $connector = new NetworkPrintConnector("192.168.1.87", 9100);
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
} catch (Exception $e) {
    echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}
?>

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.


回答1:


I haven't found a proper solution to my question regarding the VPN and SSH methods.

I also asked if there any simple way to send printing instructions from the webserver to the local network printer just using the printer's IP and Port? Even If that means exposing the printer to the internet.

I've found a solution for this part which is port forward but it's not recommend to use with defaults port as it might bring some security concerns. Here is a workaround:

Let's assume your Public IP is 204.269.97.261 and a random Port is 51000 and your printer's Private IP is 192.168.1.87 and Port is 9100.

When user is sending instruction from any web browser from any device, the instruction will go to Public IP: 204.269.97.261 and Port: 51000. Now you'll need to configure your router to forward this instruction to Private IP: 192.168.1.87 and Port: 9100.

To learn how to do port forwarding for your router model, please visit https://portforward.com/ and search for your router model.

Once done, you can check if the port forward is working by visiting https://www.canyouseeme.org/ and if you are getting a error like "Connection Refused" then that is a different problem entirely and might need to ask your ISP if that port is blocked or something else.

NOTE: Do not not use default ports with Public IP and use a random port to avoid security risks.



来源:https://stackoverflow.com/questions/65873089/how-to-print-from-the-webserver-to-the-local-network-printer-directly-using-phon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!