2

Is there a way to find out when printing ends using JQuery. I don't want to find when the printing window is closed but when the actual printing is done. My problem is , when the user prints a page on my web app and quickly navigates from the page, the printing gets cut off and does not give the entire print result.(I was thinking if I could block the UI till the actual printing ends, it would resolve the issue).

Imprfectluck
  • 654
  • 6
  • 26
  • 7
    Are you sure navigating away is the cause of the problem? I would have thought that by the time the user is done with the print dialog and control returns back to the browser, the print job is already off into the printer spool? – James Thorpe Mar 14 '16 at 15:44
  • Well actually this is kinda like a label printer . This issue happens only with that printer and only when you navigate away fast. I have not experienced this issue unless I navigate fast from the page. I was sure that's how it worked before I saw this issue. – Imprfectluck Mar 14 '16 at 15:51
  • 1
    Check the configuration of the printer... how does it manage the print stack, does it transfer all the data before start printing or it does it as it go? Is just an idea that could be affecting... – DIEGO CARRASCAL Mar 14 '16 at 18:06
  • 3
    Watch for the `window.onafterprint` event: http://stackoverflow.com/questions/18325025/how-to-detect-window-print-finish – Daniel Beck Mar 30 '16 at 19:32
  • 1
    Or, rather, don't do that. Poor browser support on that, it turns out (FF and IE only). Some of the answers on the question I linked to include very hacky-looking workarounds for other browsers but I wouldn't count on them... – Daniel Beck Mar 30 '16 at 19:34
  • I think the window.onafterprint might be useful. I am testing that out .I did it try it before and could not make it handle the events . Maybe i will test it one more time – Imprfectluck Mar 30 '16 at 20:17
  • Check this out:- http://stackoverflow.com/questions/18325025/how-to-detect-window-print-finish?lq=1 – Mohit Verma Apr 02 '16 at 20:48

3 Answers3

5

You can use a combination of window.onafterprint (supported by IE 5+ and FireFox 6+) and use window.matchMedia (supported by Chrome 9+ and Safari 5.1+). Unfortunately Opera doesn't support either. Here's an article that goes into detail about the approach.

Here's the code just in case the link becomes deactivated:

(function() {
    var beforePrint = function() {
        console.log('Functionality to run before printing.');
    };
    var afterPrint = function() {
        console.log('Functionality to run after printing');
    };

    // supported by Chrome 9+ and Safari 5.1+
    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

    // supported by IE 5+ and FireFox 6+
    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;
}());
Anthony
  • 1,439
  • 1
  • 19
  • 36
3

No there isn't. The jQuery http://api.jquery.com API link doesn't list any printer methods. Sorry, but you're out of luck trying to access both the Mac & the Windows printer queues via jQuery. The queues are part of the operating systems & not web browsers (OS applications). So unless you're working with C or C++ or Java, there isn't a way to build an events bridge back from the printer queues to multiple browsers.

Not only that, but Microsoft & Apple would have to support multiple 3rd party companies, like Google. They're not likely to do that, when they have their own web browsers, eg. IE & Safari, respectively. The way that free web browsers work is that they attempt to route default traffic into their search engines, so that they can serve up the ads & profit from the ad dollars. Microsoft wants to put money into their bank account by serving their ads up from Bing (default IE search engine) & not into Google's pockets.

So we're not likely to see an OS API to cross-browser jQuery solution for printing anytime soon. They're not going to make that feature simply because it makes web developers lives easier. They'd only want to make that feature available, if they could find a way to make millions of dollars from it.

Clomp
  • 3,168
  • 2
  • 23
  • 36
  • I know there isnt a way using Jquery to access the print queues. Just wondering if there is a workaround for the problem. Because user want to click print and navigate from the web app quickly and doing so just truncates the print. – Imprfectluck Mar 30 '16 at 20:15
  • they always click the link. So I open a new page which does the printing. Hmm that sounds promising. I think datatables Jquery plugin does it the same way. Maybe it could . Let me test it out. Thanks! – Imprfectluck Mar 30 '16 at 20:20
  • OP is not asking for access to OS printing queue, but a way to detect when browser sent all printing information. As @Anthony says, it seems that most browsers trigger different events for that. – sinuhepop Apr 06 '16 at 13:30
  • @sinuhepop I disagree. The OP doesn't mention anything about triggering an event when the browser _sends_ all of the info to the print queue, before the first page is printed. It's asking for an event which triggers after all of the pages are printed from the print queues. These are the key phrases from the OP: "but when the actual printing is done" and "block the UI till the actual printing ends". – Clomp Apr 06 '16 at 16:37
0

$(document).ready(function () {
var delays='';
        $('#PrintInput').on('keyup', function () {
        clearTimeout(delays); 
        delays = setTimeout(function () {
          // do something when printing stop after 5 sec.
        },5000);
        });
});
Navin Bhandari
  • 182
  • 1
  • 9