0

I have this method:

  print(data): void{
  let printContents, popupWin;
    let trHtml = '';
    for (let entry of data) {
        trHtml += `<tr>
                        <td>` + entry.aaabroj + `</td>
                        <td>` + entry.bbbbroj + `</td>
                        <td>` + entry.sifratipa + `</td>
                        <td>` + entry.vrijemepoziva + `</td>
                        <td>` + entry.trajanjepoziva + `</td>
                        <td>` + entry.iznoskm + `</td>
                    </tr>`;
    }
     window.open('', 'popupWin', '');
     window.print();
    popupWin.document.write(`
    <html>
        <head>
        <title>Detaljni izvještaj</title>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
        </head>
    <body>
       <table>
        <tr>
            <th>Pretp.broj</th>
            <th>Pozvani broj</th>
            <th>Tip poziva</th>
            <th>Start</th>
            <th>Količina</th>
            <th>Cijena</th>
        </tr>
        `+trHtml+`
        </table>
    </body>
    </html>`);
   }

Now i open table in print dialog but when user click on cancel it close print dialog but tab is still open. Any suggestion how can i close that tab also?

None
  • 8,817
  • 26
  • 96
  • 171

1 Answers1

0

You can not access Chrome's internal windows (printing dialog in this case) directly from a regular web page.

(function () {

    var beforePrint = function () {
        alert('Functionality to run before printing.');
    };

    var afterPrint = function () {
        alert('Functionality to run after printing');
    };

    if ($window.matchMedia) {
        var mediaQueryList = $window.matchMedia('print');

        mediaQueryList.addListener(function (mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

   $window.onbeforeprint = beforePrint;
   $window.onafterprint = afterPrint;

}());

Reference: How to capture the click event on the default print menu called by Javascript window.print()?

Haven't tested this particular code here, but did something similar before.

manneJan89
  • 1,004
  • 8
  • 27