问题
I am using TCPDF to generate documents for an online application. I want the user to return to the form after print because the user will capture data again.
I have tried opening the pdf in a new tab / window and calling window.close()
after print, but to use window.close()
the form must open using window.open()
which is not the case. Form action opens the window.
I have then tried to open the form in the same tab and calling history.back()
, but also not working (by not working I refer to the page remains on the pdf.
Code I have tried (amongst many other)
$js = <<<EOD
window.print();
document.location.href = 'http://www.google.com';
EOD;
and
$js = <<<EOD
window.print();
window.history.back();
EOD;
and
$js = <<<EOD
window.print();
history.go(-1);
EOD;
and
$js = <<<EOD
window.print();
window.addEventListener("afterprint", "window.history.back();");
EOD;
This code works when not in TCPDF:
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
window.print();
window.onafterprint = function(){
window.history.go(-1);
}
});
</script>
This must work in TCPDF
Any suggestions?
回答1:
The problem is that if you load a PDF file directly, then that page does not have JS running on it, so you can not close it with JS
The best solution I can suggest is to use ViewerJS, which a PDF reader written in JS, so you can still run code at the same time as displaying the document.
来源:https://stackoverflow.com/questions/59186544/tcpdf-return-to-previous-page-after-print-dialog-close