【问题标题】:How to print pdf from server after button click in Angular 2+在Angular 2+中单击按钮后如何从服务器打印pdf
【发布时间】:2020-07-29 14:26:16
【问题描述】:
在我的页面上,我有一个按钮,应该从 backend 下载具体的 pdf 文件并打开打印窗口。我在这里尝试了包含一些 blob 内容的答案。它不起作用。尝试更改路线并将文件嵌入 HTML 中,然后下载文件以在页面上调用 window.print(),但页面为空白。也尝试了 printJS,但无法使其工作,因为它一直显示 printJS is not a part of onclick function 或类似的东西。任何建议都会有所帮助。
【问题讨论】:
标签:
javascript
html
angular
typescript
printing
【解决方案1】:
我想出的唯一解决方案是这样做:
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
);
});
}