Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

php - POS: get a website to print directly to a defined local printer/s

I have a website which runs a box office service which issues tickets and reports. I am trying to figure out how to get tickets (currently PDFs) sent directly to a specified printer on a local/client PC.

I have followed many old/dead/useless links and have not found any up-to-date solutions to this although many tantalising glimmers of hope.

The scenario is this: Remote hosted website - 1 or more users connected - web page generates ticket/s (PDF) which is sent to a specified printer on the user pc (not the default printer) silently (no extra clicking through of print prompts).

I know PHP does not connect to a clients pc but is there a way for a web page (via jQuery perhaps) to connect to an installed script/service?

I was thinking the script on a local PC (which establishes a trusted link, sets the printer to use, etc) would receive data/file from the web server and then process it as long as it was in the same place on every machine. This (local) script then can be added to any PC that needs to run the service. The work would need to be trigged by the file being sent and not having a script/service which is sat polling a location every few seconds.

Any pointers would be greatly appreciated.

Edit: I have tried the jZebra java applet but only got it printing once before it broke. Interested if anyone has actually got it to work (and how).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ran into the EXACT same question recently myself. Google Chrome has what's called a "kiosk" mode. Therefore, it will print without user intervention.

To do this, open Google Chrome with the following command (you need to find the chrome executable, or chrome command for *nix machines):

chrome.exe "http://www.example.com/mypage.php" --kiosk --kiosk-printing

This will open a window without any toolbars, address bars, omniboxes, etc.

Next, you need to make a page print. To do this, automatically open a print dialog (for demonstration, I'll use simple Javascript):

<script>
  window.print();
</script>

Before you jump over to your development environment, window.print() does NOT allow any arguments (i.e. a URL).

This code opens a print dialog. However, in kiosk mode, the print dialog will be bypassed, and the page will be automatically printed to the default printer.

Now you mentioned a PDF, and chances are, your generating it via PHP (if you are printing issued/generated files), your probably thinking "oh, well I can't put HTML in the PDF to execute the javascript". You don't need to! To solve the issue of printing the correct page, here's how:

Insert the following into an HTML/PHP page that the user is sent to (for this solution, the user does not need to visit the .pdf), in the <head> of the landing/success page:

<link rel="alternate" media="print" href="LINK TO PDF FILE">

If you have the above code in your page, when you execute window.print();, it will print the page specified above. If you don't save the PDF locally, you can put it in a temporary directory that is somehow (out of the scope of this question) cleared on a time based or action based schedule, to prevent disk space buildup.

Keep the following in mind:

  • Kiosk mode doesn't have an exit button. To exit, press ALT + F4.
  • When printing in kiosk mode, you need both --kiosk AND --kiosk-printing. The printing argument requires the --kiosk argument.
  • When printing in kiosk mode, it is normal for the print dialog to appear and then suddenly disappear. It can't be prevented without advanced window layering and whatnot.

I'm sure that other browsers have similar functionality to bypass the print dialog, however, I have found that Google Chrome works best in this kind of functionality. If your on a Linux machine, Google has a .deb file that you can install Google Chrome on Linux with, by using the command sudo dpkg -i (package / downloaded .deb file path). Chromium --might-- support this kind of functionality. As far as I know, it should.

If you need additional help, leave your question in the comments below, I'll respond ASAP.

I hope I helped. If I did, feel free to give me a green check to your left. ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...