最后,我最终使用普通的 javascript 打印功能而不是使用库,因为在打印对话框关闭后立即重定向到另一个页面是我页面的必备功能。我还向 PrintJs 团队报告了这些问题,以便他们进行审查。我目前对这个问题的解决方案是使用正常的 onafterprint函数实现,如下所示。
主页 (index.html)
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js">
</script>
<style>
.print-area {
width: 300px;
}
.print-area table {
width: 100%;
}
.print-area table,
th,
td {
border-collapse: collapse;
border: 1px solid;
text-align: center;
}
.print-area th {
background-color: grey;
color: white;
}
@media print {
.print-area {
width: 300px;
height: 400px;
}
.print-area table {
width: 100%;
}
.print-area table,
th,
td {
border-collapse: collapse;
border: 1px solid;
text-align: center;
}
.print-area th {
background-color: grey;
color: white;
}
.no-print {
display: none;
}
}
</style>
</head>
<body>
<div class="print-area">
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
</tr>
<tr>
<td>Si Thu </td>
<td>28</td>
<td>Yangon</td>
</tr>
<tr>
<td>WWK </td>
<td>27</td>
<td>Yangon</td>
</tr>
</table>
</div>
<div class="no-print" style="margin-top:50px;">
<button id="btnPrint">Print</button>
</div>
<script>
$(document).ready(function () {
$("#btnPrint").on("click", function () {
window.onafterprint = function () {
console.log("onafterprint function fired");
window.location = "nextpage.html";
};
window.print();
});
});
</script>
</body>
</html>
打印对话框关闭后要重定向的页面 (nextpage.html)
<html>
<head>
<title>Next Page</title>
</head>
<body>
<h2>This is redirected page</h2>
</body>
</html>