Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript print blocked by Chrome, Workaround?

Tags:

I know it has been discussed here before, yet I found no practical solution/workaround for this, I'm hoping if someone has any idea how to resolve this problem!

Here's it is:

If you try to call window.print() method frequently within a single page(as if a user clicks on a print button) in google Chrome, the browser throws a warning message in the console, stating:

Ignoring too frequent calls to print()

And nothing happens! After several seconds, things go back to normal and print dialog appears the moment you call window.print() command again! To make matters worse, the good Chrome folks use exponential wait time for a page that calls print command, meaning the more user clicks on a button to print, the more he has to wait for the print dialog to appear!

This issue has been in chrome for quite some time (14 subsequent versions) and it is confirmed as being an Area-UI bug, I posted it again for google team yesterday hoping if someone from Chrome team can verify when this incredible annoying feature is going to be fixed!

However, what I'm looking for here is a workaround for this problem, is there anything I can do be able to get this working? My company is developing a highly transactional financial system with lots of reports that needs printing, and for just this one little glitch, the whole project is at risk of running in my favorite google Chrome browser!

Update:

Here's the code in Chrome browser that causes this feature and it looks like that at least 2 seconds is needed before someone calls print command again, so a timer of 2 seconds interval in UI could possibly prevent getting into an infinite wait callback! any other thoughts?

like image 669
Kamyar Nazeri Avatar asked May 01 '12 17:05

Kamyar Nazeri


People also ask

How do I allow Chrome to print?

1. To access the print settings, please see the upper, right-hand corner of the Chrome window, and select the Open Window icon (highlighted in blue below) that allows the user to “Customize and control Google Chrome.” 2. Select the Print option.

Why can I not print a PDF from Chrome?

I can see that Google Chrome won't allow you to print and save the documet as PDF. The best fix for this is by uninstalling and reinstalling Chrome. To uninstall an app from Windows, Open Settings by pressing Windows key + I, select Apps then look for Google Chrome, click on it and press the uninstall button.


3 Answers

You could conditionally replace the window.print() function:

// detect if browser is Chrome
if(navigator.userAgent.toLowerCase().indexOf("chrome") >  -1) {
    // wrap private vars in a closure
    (function() {
        var realPrintFunc = window.print;
        var interval = 2500; // 2.5 secs
        var nextAvailableTime = +new Date(); // when we can safely print again

        // overwrite window.print function
        window.print = function() {
            var now = +new Date();
            // if the next available time is in the past, print now
            if(now > nextAvailableTime) {
                realPrintFunc();
                nextAvailableTime = now + interval;
            } else {
                // print when next available
                setTimeout(realPrintFunc, nextAvailableTime - now);
                nextAvailableTime += interval;
            }
        }
    })();
}

Instead of using an external safety timer/wrapper, you can use an internal one. Just add this and window.print behaves safely in Chrome and normally everywhere else. (Plus, the closure means realPrintFunc, interval and nextAvailableTime are private to the new window.print

If you need to coordinate calls to window.print between multiple framed pages, you could set nextAvailableTime in the parent page, rather than in the closure, so that all the frames could access a shared value using window.parent.nextAvailableTime.

like image 197
apsillers Avatar answered Oct 27 '22 07:10

apsillers


I've been bumping up against the same issue, and the most direct solution for me was to just create a new window, write what I need to it, close it, and print it. I haven't had to deal with Chrome's limit since I changed it to work this way, and I don't need to do any tracking.

print_window= window.open();
print_window.document.write(print_css + divToPrint[0].outerHTML+"</html>");
print_window.document.close();
print_window.focus();
print_window.print();
print_window.close();
like image 40
abathur Avatar answered Oct 27 '22 07:10

abathur


My solution would be to call the window.print() less frequently. You could try wrapping window.print() into a method of your own and put a minimum time interval that has to pass before the window.print() is executed again. You can have some kind of change/animation in UI to indicate that work is being done.

"Working"

Unless you think it really think pressing print-button more than once a second/every few seconds helps? I mean if the window.print() operation takes that long it's not your codes fault and there should be a "wait and be patient" -indicator in UI anyway.

like image 28
AP-Green Avatar answered Oct 27 '22 09:10

AP-Green