如何在Angular 2+中点击按钮后从服务器打印pdf



在我的页面上,我有一个按钮,应该从后端下载具体的pdf文件,并打开打印窗口。我在这里尝试了一些包含斑点的答案。它不起作用。尝试更改路由并将文件嵌入HTML中,然后下载文件以调用页面上的window.print((,但页面为空。也尝试过打印JS,但没能让它工作,因为它一直显示printJS is not a part of onclick function或类似的东西。任何建议都会有帮助。

我想到的唯一解决方案是这样做:

printPdf(){
this.network.getPdfHash()
.pipe(take(1))
.subscribe((res) => {
//res === url to the location of the PDF 
let win = window.open(res, "_blank");
win.focus();
win.addEventListener(
"load",
() => {
setTimeout(() => {
//to give time for the browser to load the pdf
win.window.print();
}, 250);
},
true
);
});
}

最新更新