简体   繁体   English

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

[英]How to print pdf from server after button click in Angular 2+

On my page I have button that should download concrete pdf file from backend and open the printing window.在我的页面上,我有一个按钮,应该从后端下载具体的 pdf文件并打开打印 window。 I tried answers here that included some blob stuff.我在这里尝试了包含一些 blob 内容的答案。 It did not work.那没起效。 Tried to change route and embed the file in the HTML and after files would be downloaded to call window.print() on the page, but page was blank.尝试更改路线并将文件嵌入 HTML 中,然后下载文件以在页面上调用 window.print(),但页面为空白。 Tried also printJS, but wasn't able to make it work, since it kept showing printJS is not a part of onclick function or something like that.也尝试过 printJS,但无法使其工作,因为它一直显示printJS is not a part of onclick function Any advice would be helpful.任何意见将是有益的。

The only solution I came up with was to do it like this:我想出的唯一解决方案是这样做:

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
        );

    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM