简体   繁体   中英

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. I tried answers here that included some blob stuff. 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. 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. 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
        );

    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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