0

I am trying to convert download a pdf file generated by PHP. The problem which I am facing is My PHP file is getting converted into PDF but it is happening in another tab. For Suppose the user Click in download button which is navigating in another page where the PHP file is getting converted into PDF and its getting closed after PDF conversion.

My hyperlink is :

<td><a href="b2b/electricity_invoice.php?id=<?= $v1['id'] ?>" target="_blank"><i class="fa fa-download" aria-hidden="true"></i></a></td>

And Electricity Invoice file is :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
              <script>
            $('#cmd2').click(function() {
                var options = {
                    //'width': 800,
                };
                var pdf = new jsPDF('p', 'pt', 'a4');
                pdf.addHTML($("#content2"), 0, 0, options, function() {
                    pdf.save('<?php echo 'ECTY/0'.$details['id'].'/2017-'.date("Y") ?>.pdf');
                });
            });
            </script>
<body id="content2" onLoad="document.getElementById('cmd2').click();">
    <br><br><br>
        <table width="100%">
            <tr>
                <td colspan="2"><font size="3"><center>Electricity Bill Payment Receipt</center></font></td>
            </tr>
            <tr>
                <td colspan="2"><center>Receipt for Payment of Bill. This Receipt is generated from {{Project Name}}</center></td>
            </tr>
            <tr>
                <td>Receipt ID: ECTY/0<?= $details['id'].'/2017-'.date("Y") ?></td>
                <td>Date: <?= $details['date']; ?></td>
            </tr>
            <tr>
                <td>Customer Number: <?= $details['name']; ?> </td>
                <td>Collection Month: N/A</td>
            </tr>
            <tr>
                <td>Mobile No : <?= $details['cmobile']; ?></td>
                <td>Board Name: <?= getOperatorsNameById($details['opId']); ?> </td>
            </tr>
            <tr>
                <td>Amount : <?= $details['amount']; ?></td>
                <td>Status: <?= $details['status']; ?></td>
            </tr>
        </table>
        <p> <center>For and on Behalf of {{Project Name}}</center> </p>
           <button class="btn btn-info" id="cmd2" onclick="setTimeout(function(){var ww = window.open(window.location, '_self'); ww.close(); }, 500);">Download Token</button>
            
    </body>
halfer
  • 19,824
  • 17
  • 99
  • 186
Nikita Agrawal
  • 235
  • 1
  • 11

1 Answers1

0

Please don't use target="_blank" if you don't want to open in new tab.

PPL
  • 6,357
  • 1
  • 11
  • 30