Silent Print From Browser

12,937

Solution 1

I've looked into the chrome extensions, fire fox addon
- Faizan Afzal Mar 28 at 15:54

In the above comment, you mentioned that you've looked into Chrome extensions and FireFox addons, however there is already the functionality built into these browsers to disable the print dialog.
If the web application you're making will be run in a controlled environment (where you manage which browsers access it and how they're configured), you can fairly easily do this.

Chrome
Firstly, go to chrome://settings/ and change your home page to the web application. Next, make a shortcut for the Chrome browser on your desktop then right click on it to open the properties window. In the 'Target' input field, add --kiosk --kiosk-printing to the end of the location. Apply the changes, close all Chrome windows and click on the shortcut. This should put you in full screen (kiosk mode) and when you attempt t print, it should automatically print on the default printer without a pop-up window being displayed.

FireFox
On FireFox, go to about:config and agree to any warning messages. Then, right click somewhere on the page and create a "New -> Boolean". It will prompt you for a name and a state. For the name, enter print.always_print_silent and for the state, set it to true. Then you will need to save changes and restart any FireFox windows you have open. If you attempt to print something, it will no longer require the pop-up window to be displayed and will automatically print on the default printer.

With either of these browsers configured in this way, you can use the standard window.print(); JavaScript method to print without needing any kind of server-side interaction.

Batch Files?
If you'd like an easier way to do these, you can use these two command prompt scripts which will automatically configure and/or run them to suit your needs:

Chrome:

cd Program Files (x86)\Google\Chrome\Application
chrome.exe --kiosk --kiosk-printing

FireFox:

FOR /D %%G in ("%APPDATA%\Mozilla\Firefox\Profiles\*.default") DO SET prof=%%G
cd %prof%
echo user_pref("print.always_print_silent", true);>>prefs.js
cd \..
cd Program Files (x86)\Mozilla Firefox
firefox.exe

Solution 2

The short of it is, dealing with HTTPS over a socket connection is tricky because of mixed content restrictions and changing SSL standards so authoring this from scratch to work on all platforms can be daunting.

I also thought a solution to send the print request using web sockets while a java socket client application installation over the user system.

This is exactly how QZ Tray works.

qz.websocket.connect().then(function() { 
   return qz.printers.find("zebra");              // Pass the printer name into the next Promise
}).then(function(printer) {
   var config = qz.configs.create(printer);       // Create a default config for the found printer
   var data = ['^XA^FO50,50^ADN,36,20^FDRAW ZPL EXAMPLE^FS^XZ'];   // Raw ZPL
   return qz.print(config, data);
}).catch(function(e) { console.error(e); }

The above example is specific to raw printing, but the app will work for other formats as well (HTML, PDF, images)

Project page: https://github.com/qzind/tray

Full disclaimer.... as the author of the above plugin, I find it fair to mention that PrintNode does a near identical task. Both plugins are open source but backed by commercial services which support them.

Project page: https://github.com/PrintNode

Share:
12,937
Faizan Afzal
Author by

Faizan Afzal

Updated on June 24, 2022

Comments

  • Faizan Afzal
    Faizan Afzal about 2 years

    I have researched so much in the last few days and have given my enough head in that issue. What I'm trying to achieve is to print directly from web using a print button. I don't want to browser print popup to appear. There will be 2 printers attached to my web application and I want the printer selection automatically.

    I know that it is not possible with PHP or without any browser extension or active x plugin.

    I also thought a solution to send the print request using web sockets while a java socket client application installation over the user system.

    Please suggest me any time saving solution to my problem