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