1

I'm currently having an issue trying to print a report. We are using an Angularjs solution. We display the report for the user, and when they hit print we have the template switch. Then we call window.print() (which normally stops javascript code execution). Then after printing we swap the template back to the display.

This currently works in chrome and safari on a mac. However when we use an Ipad/Iphone the templates swaps, swaps back and then print appears and the print preview is incorrect.

Searching for answers I found: people talking about using var mql = window.matchMedia('print') and then using add.listener(function) and doing the swap in the function. The below code I was able to get Ipad/Iphone to swap to the correct template and print however it does not swap back.

VcrReportController.prototype.printReport = function () {
    var vm = this;
    var mql = window.matchMedia('print');
    var printHandler = function(mql){
        if (mql.matches){
            console.log('print');
            vm.template = 'displayTemplate'
        }
        else{
            console.log('not print');
        }
    }
    function changeTemplate() {
        var deferred = vm.$q.defer();
        vm.template = 'printTemplate';
        vm.$timeout(function () {
            deferred.resolve(true);
        },250);
        return deferred.promise;
    }
    changeTemplate()
        .then(function (response) {
            mql.addListener(printHandler);
            vm.$window.print();
        });

Not 100% sure about the listener stuff. Any help or other solutions people might have used to deal with this kind of situation would be great.

Thanks,

BraybrookR
  • 11
  • 3
  • What version are you using on iOS? What do you mean with "does not swap back" in matters of your example? Will it not execute `console.log('not print');`? I noticed an similar problem with iOS 10.3, it worked for me with iOS 10.2. See http://stackoverflow.com/questions/43386499/safari-window-matchmedia-handler-not-called for further info. – m4lt3 Apr 25 '17 at 12:59
  • I cannot comment on version the customer phone was currently. The Old shitty IPAD i have is using 9.3.5 and I know they both have the same issue. window.print() does not seem to stop code execution in iphone/ipads. I believe this is because it uses AIRprint(?) behind the scenes. So with above code It works normally outside iphone/ipad. However instead of swapping back to display mode after printing it just sits there and user has to click back (so 50%) right. Wondering if anyone tried/done this before if so how. Swapping templates and print swap back. Have it work on iphone/ipads. – BraybrookR Apr 25 '17 at 14:12
  • We have similar issues. I would recommend to open a new window/tab for print. Sadly we can not do that, but I guess you are also not able to do that. I keep you posted, if we find a solution. – m4lt3 Apr 26 '17 at 07:12
  • Thanks, Currently the above option has been my best solution. If you are not using iphone/ipad it works. If you are using it, then you have to hit Back. Not ideal, however i'll keep messing with it. when I get a chance. – BraybrookR Apr 26 '17 at 13:50

0 Answers0