如何防止HTML2PDF自动下载pdf



我的JavaScript代码中有一个HTML2PDF方法。一方面,代码工作得很好,因为我可以在一个新的blob选项卡中打开渲染的pdf并打开打印窗口。然而,当我这样做的时候,pdf也会自动下载。我希望阻止该方法下载.pdf文件,只在新选项卡中打开打印窗口。下面的代码就是我所拥有的。如有任何帮助,我们将不胜感激!

html2pdf(body, {
filename: 'test.pdf',
jsPDF: {
orientation: 'portrait',
}
})
.from('element-to-print')
.get('pdf')
.then(function (pdfObj) {
pdfObj.autoPrint();
window.open(pdfObj.output('bloburl'), 'F');
});

试试这个代码:

html2pdf() // move your config in the .set({...}) function below
.set({
filename: 'test.pdf',
jsPDF: {
orientation: 'portrait',
}
})
.from('element-to-print')
.outputPdf()  // add this to replace implicite .save() method, which triggers file download
.get('pdf')
.then(function (pdfObj) {
pdfObj.autoPrint();
window.open(pdfObj.output("bloburl"), "F")
});

最新更新