0

I am working on angular4 print functionality and i want to open print preview in new tab by default along with print popup window. I am not able to figure out how to pass data from parent window to child window to do the same. Can someone suggest something?

alekya
  • 1
  • 1
  • 2

3 Answers3

0

Use Following Code :

This will open this page in another tab and will open print pop up.

Page will take some time to load so use setTimeout according to your need.

var getPrint = window.open(document.URL, '_blank');
setTimeout(getPrint.print(), 3000);
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
0

Had the same situation while trying to allow user to view and print QR image in new tab. Here is how I got it done:

  prepareQRForPrint(source: string) {
    return "<html><head><style type='text/css' media='print'>@media print { @page { size: auto; margin: 0;} body { margin:1.6cm; } }</style><script>function step1(){\n" +
      "setTimeout('step2()', 2);}\n" +
      "function step2(){window.print();window.close()}\n" +
      "</scri" + "pt></head><body onload='step1()'>\n" +
      "<img style='width:800px;height:1000px;' src='" + source + "' /></body></html>"
  }

  printPreviewQR(source: string) {
    let Pagelink = "about:blank";
    var pwa = window.open(Pagelink, "_new");
    pwa.document.open();

    pwa.document.write(this.prepareQRForPrint(source));
    pwa.document.close();
  }

And invoked as:

<a href="javascript: void(0)" (click)="printPreviewQR(QRPath)">QR-Code</a>
Abhijeet_IXR
  • 807
  • 6
  • 15
0

This will work in Angular 12

/* 
Use ViewChild to reference the element that contains the content that should be printed.
*/
@ViewChild('PackageSlipPrint', { static: true }) printableRef?: ElementRef;

print() {
    var printdata = this.docRef.nativeElement.outerHTML;
     
    var tab = window.open('') as Window;
    tab.document.open();
    tab.document.write(printdata);
    setTimeout(() => {
        tab.stop();
        tab.print();
        tab.close();
    }, 300);
}

LH7
  • 1,385
  • 2
  • 12
  • 16