4

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Faizan Afzal
  • 413
  • 2
  • 11
  • 19
  • 1
    You're not going to be able to override the print functionality of a client browser. PHP/Java will have nothing to do with it. – kevingreen Mar 28 '16 at 15:39
  • The idea is to let java client application handle the print request – Faizan Afzal Mar 28 '16 at 15:40
  • You want to build a Java web client, that calls a web site and will print the page? – kevingreen Mar 28 '16 at 15:41
  • I've looked into the chrome extensions, fire fox addon, a php web client tool which install a little application on the client pc and then you can install from the web.. But none of the solution seems reliable to me. I've an idea in my mind to build a PHP Socket Server and an build a Java Socket Client.. The Client will establish a connection with the Server and whenever Server will get the print request, it will pass the print data to Java Client where the entire print functionality will be handled – Faizan Afzal Mar 28 '16 at 15:54
  • What's the point of a socket server to print? You can render HTML to a printable format pretty easy in Java. You just need 1 application to call it. – kevingreen Mar 28 '16 at 15:57
  • The point is to send the print request from the PHP Client to PHP Socket Server and the PHP Socket Server will pass the request to Java Client – Faizan Afzal Apr 04 '16 at 08:07
  • this answer may be help full for you http://stackoverflow.com/questions/1999108/window-print-not-working-in-firefox/32264059#32264059 – Ramdrupal7 Apr 19 '16 at 10:06
  • Re: "let java client application handle the print request "... @FaizanAfzal if a Java Client application (NOT applet) is acceptable, here's a product that can do it and can handle HTTPS https://github.com/qzind/tray. – tresf Nov 19 '16 at 20:13

2 Answers2

11
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
Nathangrad
  • 1,426
  • 10
  • 25
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

tresf
  • 7,103
  • 6
  • 40
  • 101