0

In a development environment, my website can make a local request to print on a networked printer through javascript.

But once my website is hosted online I can no longer access the local printer. I understand why this is the case if a stranger were to access my website over the web, but can I not do that if I am accessing my own website (where my printer is local to that computer)?

EDIT: This is the request I make to print in development and it works because my printer is local: 'http://192.168.1.100/StarWebPRNT/SendMessage'

echan00
  • 2,788
  • 2
  • 18
  • 35
  • Your website doesn't really have access to any printer, it's the OS that does the printing from the browser ? – adeneo Dec 10 '16 at 03:07
  • @adeneo i am making a web request to print on my printer. It is not printing from the OS, yes its not normal, the receipt printer i have provides this technology to allow web request printing. – echan00 Dec 10 '16 at 03:09
  • Unless your website is also hosted from `http://192.168.1.100/`, the request won't pass the [Same-Origin Policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) (Local-Area Networks don't alter the outcome of the SOP) and will have to satisfy [one of a few exceptions](https://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy), or be made server-side where the SOP doesn't apply. Note that least two of the options, CORS and JSON-P, the printer's web server will have to offer support explicitly to use. – Jonathan Lonowski Dec 10 '16 at 03:16
  • @JonathanLonowski I think the problem I have right now (before SOP) is I can't get the browser to find the printer when the website is hosted on the web. Because 192.168.1.100 (IP of the local printer) is only a local IP address... can I access that local address via the web when surfing a webpage hosted elsewhere? – echan00 Dec 10 '16 at 03:25
  • @echan00 If the request is made client-side, it will originate within the network your computer is connected to (I assume, the same LAN the printer is connected to). To make the request from an external server, you'll have to provide a public route to your printer – e.g. reference it by the public IP your LAN is identified by to the rest of the internet and configure port forwarding in your network devices to route connections to the printer. (Note: this can open your printer to any user.) – Jonathan Lonowski Dec 10 '16 at 03:30
  • @JonathanLonowski okay, so port forwarding is the solution right? I tried it but it did not work. I setup port forwarding for port 80 to forward to the printer IP address 192.168.1.100, and tried to do http://-router-ip-address/StarWebPRNT/SendMessage but no luck – echan00 Dec 10 '16 at 03:33

1 Answers1

0

@echan00,

I think the reason is that your printer is located on the same network that you're developing on. The main issue over here is the IP Addresses, Most local networks have an IP Address starting with 192.168.XXX.XXX , so your printer which is located on the network also contains the IP address in the same range.

Lets assume : Printer => 192.168.1.100 and Development machine => 192.168.1.45

Since both of these machines are on the same network, they can communicate with each other. As soon as you host your website with a provider, the IP address of the website will no longer be the same as the IP Address of your machine, Let's assume it receives a new IP Address 10.1.57.221.

So now your website is hosted an IP outside your LAN and the printer is still on the LAN. If you can notice the response given when a request to print has been made http://192.168.1.100/StarWebPRNT/SendMessage from the hosted website, there's a very high chance that you'd end up hitting a 404 or connection timed out instead of the 200 success request.

One way to fix this problem is to host the website on a server in your LAN but provide that computer with a public IP Address so that others can access the website from outside the network. When the request is made to print, it can definitely find the printer in this case.

Another way is to continue hosting with a hosting provider but assign a public IP to the printer so that anyone knowing the IP address can issue a print request to the printer. That way, your website will be able to make a request to the printer for a print.

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
  • that's what I'm thinking too. Since my website has to be hosted with my current provider, I guess I will need to assign a public IP to the printer. How would I go about doing that? I tried to do port forwarding, but no luck, is that the resolution? FYI I have admin access to the router and everything in my LAN.. – echan00 Dec 10 '16 at 03:30
  • Public IPs are provided by the ISP or the cloud service that you're using. To get a device on your network onto the public internet network, you need to request your ISP or if you have an IT team , you should put in a request to the network administrator. Generally to keep servers available public and privately, the network admins install a proxy such as squid especially in universities since many universities have their web servers publicly available and also have internal LAN/vLAN networks. – Sudheesh Singanamalla Dec 10 '16 at 07:55