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

16,010

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. ;)

Share:
16,010
lxsparks
Author by

lxsparks

Updated on June 17, 2022

Comments

  • lxsparks
    lxsparks about 2 years

    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).

  • Pitchinnate
    Pitchinnate almost 11 years
    Not going to lie this is one of the most informative answers on SO I have ever seen. I have never even heard of kiosk mode.
  • 1234567
    1234567 almost 11 years
    @Pitchinnate Thank you for the compliment. I try to help people whenever I can, regardless of how informative the answers are. There's no such thing as a stupid question. Once again, thank you for the constructive feedback. :)
  • lxsparks
    lxsparks almost 11 years
    Thank you for a very informative and interesting answer, I never knew Chrome had this function! :o) Unfortunately for me there I have two issues 1)as windows.print does not accept any arguments it can't be set to print to a particular printer (in my case a ticket printer). 2) I often have the browser resized to be able to read emails for orders at the same time. Thank you though for the insight into Kiosk mode!
  • 1234567
    1234567 almost 11 years
    @lxsparks Thanks for the constructive feedback! I also included in the answer above a way to choose the page to be printed. By the way, if my answer solved your question, click the green checkmark to the left of it. It will award me reputation (which I need, so I can eventually post comments to get more information such as versions, etc), and it will mark it as the answer that was correct, or provided the most information. Thanks! :)
  • Dan Mastromonaco
    Dan Mastromonaco over 9 years
    @NetworkNerd Nice solution. I run a similar app as the OP and instead of opening additional pages, I load a pdf in an iframe and call the print function from kiosk mode for that iframe instead. I have run into the problem where sometimes it prints a blank label/receipt as the pdf has not loaded by the time the command to print goes through. As a stop-gap, I've used setTimeout to wait an arbitrary amount of time, but I'd like to be able to know definitively when the pdf is available to print. Does your solution with a <link> tag solve this issue?
  • alex
    alex over 8 years
    @AustinK i am almost sure your answer is part of my solution, but in my case I have just an one page project (a ticket scanner, which checks barcode, returns pdf + qrcode+name+company). I have got the scan and create pdf part going, but I am not sure where I should put <script> window.print(); </script> and <link rel="alternate" media="print" href="LINK TO PDF FILE">. Because, onload the pdf is not yet created, only after the scan and the user returns (with ajax) the pdf is created.
  • alex
    alex over 8 years
    btw in theory the following should print test.pdf, correct? in my case it doesn't <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="alternate" media="print" href="http://localhost/mydomain.com/test/test.pdf"> <script> window.print(); </script> </head> <body> <div>TODO write content</div> </body> </html>
  • 1234567
    1234567 over 8 years
    @alex Try moving the <script>window.print();</script> piece to outside the <head> element. Also - what browser are you using to test it?