1

I'm printing a html retrieved from back end.

 printHtml(htmlTemplate) {
    var printWindow = window.open('', '_blank');
    printWindow.document.write(htmlTemplate);
    setTimeout(function () {
        printWindow.document.close(); // necessary for IE >= 10
        printWindow.focus(); // necessary for IE >= 10*/
        printWindow.print();
        printWindow.close();
    }, 1000);
}

This works fine in all browsers and only thing I can't figure out is how to stop the pop up blocker. Can't use printWindow.location because the html is there in a variable.

PAVITRA
  • 761
  • 2
  • 12
  • 24
  • How are you opening this popup ? After a user action or something else ? _"A browser will only open a tab/popup without the popup blocker warning if the command to open the tab/popup comes from a trusted event."_ See [this question](http://stackoverflow.com/questions/7139103/open-page-in-new-window-without-popup-blocking) for more details. – abhishekkannojia May 05 '17 at 10:36
  • @abhishekkannojia I'm triggering this from a button click and the browser print window is opened. – PAVITRA May 05 '17 at 10:40

1 Answers1

0

For anyone having the same issue,
Issue was I have called this method inside a promise and when that is done, window.open is having a new window instance resulting the popup blocker. This worked fine when I assigned,

var printWindow = window.open('', '_blank');

to a global variable before going in to the promise and used that inside the method.

PAVITRA
  • 761
  • 2
  • 12
  • 24