【问题标题】:Export pdf works on all browsers except internet explorer 11 in angular 5导出 pdf 适用于除 Internet Explorer 11 以外的所有浏览器,角度为 5
【发布时间】:2020-07-01 10:12:17
【问题描述】:

我尝试在所有网络浏览器(chrome、FireFox)上测试“导出 PDF”角度,它可以正常工作,但在 Internet Explorer 11 上除外

 //******************************************".html"******************************************//
     <button title="{{'purchase request'| translate}} PDF" mat-button class="header-button" 
    (click)="downloadPdf(prPurchaseRequest)">
     <mat-icon>cloud_download</mat-icon>{{'purchase request'|translate}} PDF
    </button>
//******************************************".ts"******************************************//
 /**
 *
 * Download PDF prPurchaseRequest
 */
downloadPdf(prPurchaseRequest) {
    this.spinnerstate = true;
    this.extraService.getCollection('api/pr-purchase-requests/generate_pdf/' + 
prPurchaseRequest.id).subscribe((response) => {
        setTimeout(() => { this.spinnerstate = false; }, 2000);
        const name = prPurchaseRequest.reference + '_purchase-request.pdf';
        const linkSource = 'data:application/pdf;base64,' + response.data;
        const downloadLink = document.createElement('a');

        downloadLink.href = linkSource;
        downloadLink.download = name;
        downloadLink.click();
    });
}

//****************************** Internet Explorer 中的错误控制台 ********** //

【问题讨论】:

    标签: angularjs internet-explorer export-to-pdf


    【解决方案1】:

    导出正常,但 pdf 已损坏:

    downloadPdf(prPurchaseRequest) {
        this.spinnerstate = true;
        this.extraService.getCollection('api/pr-purchase-requests/generate_pdf/' + prPurchaseRequest.id).subscribe((response) => {
            setTimeout(() => { this.spinnerstate = false; }, 2000);
    
            const name = prPurchaseRequest.reference + '_purchase-request.pdf';
            const linkSource = 'data:application/pdf;base64,' + response.data;
    
            if(window.navigator.msSaveBlob) {
    
                  const blob = new Blob([linkSource], {type: "application/pdf;base64," });
                  navigator.msSaveBlob(blob,name);
            }
            else{
                const downloadLink = document.createElement('a');
                downloadLink.href = linkSource;
                downloadLink.download = name;
                downloadLink.click();
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      其实IE的控制台没有错误。只有两个警告,没关系。点击链接无法下载PDF,因为IE不支持download attribute

      你可以use Blob and msSaveBlob to save files in IEmsSaveBlob 为用户提供了一个保存按钮,用户可以点击它来保存PDF文件。

      IE 中的示例用法:

      if(window.navigator.msSaveBlob) {
          //IE11
          window.navigator.msSaveBlob(blobData, fileName); 
      }
      else{
          //Other browsers
          //your function to download pdf
      }
      

      更多信息,您也可以参考this thread

      ----------------------------------- - - - - - - - - -编辑 - - - - - - - - - - - - - - - - --------------------------

      你得到的linkSource 是base64 字符串而不是blob 数据。您需要将 base64 转换为 blob,然后使用 msSaveBlob 函数。

      base64转blob的详细信息,请参考this thread

      【讨论】:

      • downloadPdf(prPurchaseRequest) {this.spinnerstate = true;this.extraService.getCollection('api/pr-purchase-requests/generate_pdf/' +prPurchaseRequest.id).subscribe((response) => {setTimeout(() => { this.spinnerstate = false; }, 2000); const name = prPurchaseRequest.reference + '_purchase-request.pdf'; const linkSource = 'data:application/pdf;base64,' + response。数据; if(window.navigator.msSaveBlob) { window.navigator.msSaveBlob(linkSource,name); } else {const downloadLink = document.createElement('a'); downloadLink.href = linkSource;down​​loadLink.download =name;down​​loadLink .click();}});}
      • 我尝试这个例子时问题仍然存在
      猜你喜欢
      • 2013-11-21
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 2013-02-23
      • 2013-07-12
      • 2020-05-10
      • 2013-08-09
      相关资源
      最近更新 更多