How to get AngularJS BLOB to Download PDF?

后端 未结 5 2073
甜味超标
甜味超标 2021-01-12 08:54

Hello everyone I am really new to developing with AngularJS and I am trying to figure out how to use BLOB to download a PDF locally to a machine. I already got it to work wi

5条回答
  •  滥情空心
    2021-01-12 09:48

    This code worked for me in angular 9, Yii2, while using mpdf

    this._gService.download_post(`controller/action`, postData).pipe(takeUntil(this._unsubscribeAll)).subscribe(result => {
    
      const fileURL = URL.createObjectURL(result);
    
      // Direct print preview
    
      // const iframe = document.createElement('iframe');
      // iframe.style.display = 'none';
      // iframe.src = fileURL;
      // document.body.appendChild(iframe);
      // iframe.contentWindow.print();
    
      // Direct Download
    
      const fileName = 'Patient Report';
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.href = fileURL;
      a.download = fileName;
      a.click();
    
    
    }, error => {
       // show error message
    });
    

提交回复
热议问题