简体   繁体   English

从本地服务器访问本地网络上的客户端打印机

[英]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. 我正在通过路由器和以太网电缆连接两台计算机来测试一个网站,其中一台PC上具有WAMP网站并充当服务器。

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. 我已经写了PHP来通过热敏打印机打印最终收据,当在通过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? 现在的问题是,当第二台作为客户端的计算机访问网站时,它将如何通过通过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. 当然,如果我想访问客户端的打印机,我认为JavaScript是最好的方法,但这有一个很大的缺陷。
When I load the printable content in a new window using window.open() and then write data to it. 当我使用window.open()在新窗口中加载可打印内容时,然后向其中写入数据。 I then call window. print(); 然后我打电话给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. PHP可以正常工作,它只打印数据然后停止,这样我就可以撕下收据。 Why won't the printer stop after printing the data when using JavaScript. 使用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 1-读取数据

2- Print it on the paper 2-在纸上打印

3- decide the length of paper to come out 3-确定要出纸的长度

4- cut the paper after printing is completed. 4-打印完成后切纸。

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". 我尝试使用window.print()来完成此操作,因为我找不到任何有关“使用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... 使用Internet Explorer甚至比Google chrome更糟,因为即使卷完成后,它仍在继续要求新纸卷以打印更多白色的空白页...

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. 当我单击“打印预览”按钮时,它向我显示了要放入数据的纸张的确切尺寸……而我最不希望的是,Firefox在完成打印收据时调用了剪切功能()热敏打印机的功能是切割收据,以便可以轻松地从中拔出收据。

then I typed about:config in the search bar of the Firefox and searched for "print". 然后在Firefox的搜索栏中输入about:config并搜索“打印”。 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... 希望这可以帮助某人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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