简体   繁体   中英

access clients printer on local network from a local server

I am testing a website by connecting 2 computers via router and Ethernet cables where one of the PCs has the website with WAMP and is acting as a server.

Everything is fine and working but there is one problem. I have written PHP to print the final receipt via a thermal printer which works perfectly when printing on the server computer where that thermal printer is connected via USB.
Now the problem is when the second computer working as a client accesses the website how will it print the receipt through a thermal printer connected to it via USB? The server won't find the printer connected to the client.

Of course I thought JavaScript is the best way to do it if I want to access the client's printer but that has a one big flaw.
When I load the printable content in a new window using window.open() and then write data to it. I then call window. print(); window. print(); and it does print the content but it doesn't stop. After printing the data on the receipt it keeps on pushing empty receipts out the printer and never stops.

PHP works fine, it just prints the data and then stops so that I can tear the receipt. Why won't the printer stop after printing the data when using JavaScript.

Please help. It's the only thing left..

Answer for blank pages;
The problem may be caused by closing the window before the page is printed, causing the memory of the page to be freed and resulting in a blank print.

You can close the window as follows;

<script type="text/javascript">
window.print();
window.onfocus=function(){ window.close();}
</script>

For print from other computer printe;

You can share your printer with client and make it default.
For silent print See this answer

Ok I found the solution. Thermal printer has four basic functions

1- Read the data

2- Print it on the paper

3- decide the length of paper to come out

4- cut the paper after printing is completed.

I tried to do it with window.print() as I couldn't find anything regarding "print to client's printer from a local server with php".

With google chrome here is what happened

after clicking the print preview button it showed me the data zoomed out to such a level that it was barely visible. To be more specific, the data looked like a thick black line where the rest was a long white page with almost infinit scroll. after hitting the print button the printer started printing and it never stoped printing untill the compete roll of paper was finished. The head of the paper contained the actual data while the rest of the whole big length of paper was empty white..

With Internet Explorer it was even worse than google chrome as even after the roll was finished it kept asking for new roll of paper to print some more white empty page...

Then I came across the champ. MOZILLA FIREFOX. When I clicked the print preview button it showed me the exact size of the paper which was needed for the data to fit in... and the most important thing that I was not expecting is when the firefox finished printing the receipt it invoked the cut() function of the thermal printer to cut the receipt so that the receipt can be plucked easily from it.

then I typed about:config in the search bar of the Firefox and searched for "print". I was really amazed to see so many options regarding printing that it was more than what I wanted to see in a receipt printed from a browser page. I can literally design the template of the receipt and save it. After that when ever I will print the receipt it will be printed inside that template.

Thanks guys for looking at my question. Hope this helps someone...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM