[英]function not working with using window.location.href
函数html2pdf(element)
(生成页面的pdf)工作正常。 执行该功能后,我想直接转到另一个页面。 在我的情况下添加window.location.href[...]
页面只是重定向到下一页但功能html2pdf
不再工作。
<script>
$(function() {});
function printPDF() {
var element = document.getElementById("element-to-print");
var opt = {
margin: 1,
filename: "EFS10Laptop.pdf",
image: { type: "jpeg", quality: 1.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: "in", format: "letter", orientation: "portrait" }
};
// New Promise-based usage:
//html2pdf().from(element).set(opt).save();
html2pdf(element);
window.location.href = "index.php";
}
</script>
Ankur是对的 - 无论是否带有基于promise的API使用的html2pdf
本质上都是异步的,所以它不会阻止你的代码在处理请求时继续,并且因为设置window.location.href
接近瞬时,在html2pdf有机会完成之前达到重定向。
因为html2pdf
具有基于promise的API,所以很容易避免 - 只需等到保存承诺解析为重定向,如下所示:
html2pdf().from(element).set(opt).save().then(()=>{
window.location.href = "index.php";
});
请注意,在保存对话框仍处于打开状态时,这仍会重定向用户。 我没有看到在文档中关闭它的时候列出的承诺(可能是因为JS方面不知道它),但它不会阻止html2pdf工作,因为文件blob的生成发生在页面重定向之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.