0

I am using below code with setTimeout but this is hack. I want to call window.print() once page fully loaded with html and css. Kindly suggest better solutions.

const WindowPrt = window.open(url, '_blank', 'left=0,top=0,width=900,height=900,toolbar=0,scrollbars=0,status=0');
    
setTimeout(() => {
      WindowPrt.focus();
      WindowPrt.print();
      WindowPrt.close();
    }, 10000);

2 Answers2

0

When the page loads, you would need to implement lifecycle hook, like ngOnInit,ngAfterViewInit to be specific inside that particular component level. To learn more about this . see the examples in the below link

https://angular.io/guide/lifecycle-hooks

Adarsh Raj
  • 297
  • 4
  • 14
  • i have tried in ngAfterViewInit but window.print() call before page load(css, banner hevay css) –  Oct 12 '22 at 07:44
  • i hope this is the only thought of way and it should work. can we try opening a new window and have call window.print inside it like in this link https://stackoverflow.com/questions/41379274/print-html-template-in-angular-2-ng-print-in-angular-2/41379912#41379912 – Adarsh Raj Oct 12 '22 at 08:33
0

Angular has a specific function that called when the view is fully initialized. it's called ngAfterViewInit check this link for more docs

Eli Porush
  • 1,339
  • 1
  • 7
  • 18
  • i have tried in ngAfterViewInit but window.print() call before page load(css, banner hevay css) –  Oct 12 '22 at 07:44