【发布时间】:2018-02-26 11:16:45
【问题描述】:
我正在尝试转换下载由 PHP 生成的 pdf 文件。我面临的问题是我的 PHP 文件正在转换为 PDF,但它发生在另一个选项卡中。假设用户点击下载按钮,该按钮正在另一个页面中导航,PHP 文件正在转换为 PDF 并在 PDF 转换后关闭。
我的超链接是:
<td><a href="b2b/electricity_invoice.php?id=<?= $v1['id'] ?>" target="_blank"><i class="fa fa-download" aria-hidden="true"></i></a></td>
而电费发票文件是:
<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>
【问题讨论】:
-
如果您不想在新标签页中打开
target="_blank",请删除它 -
但是如果我不使用它也会关闭我的项目。
-
那么您可能应该删除
ww.close();部分 -
如果对您有帮助,请点赞。@NikitaAgrawal
标签: javascript php pdf-generation