如何在响应中生成的base64字符串中查看我的angular 6模板中的PDF

时间:2019-03-25 00:20:10

标签: pdf base64 angular6

我正在努力显示在http调用html div部分时从base 64字符串生成的pdf文件,该部分卡在我可以显示pdf的部分,我可以得到响应,还可以下载PDF,现在,我想在div部分的模板上显示它。 以下是我的服务和组件文件 我想要一个可以查看PDF文件的部分。 服务端

policyPDF(formValuePolicyPDF): Observable<PolicyData[]> {

  const headers = new HttpHeaders({ 'Content-Type': 'application/json'});
  let body  = {"auth": { "accesskey": "TTIBI","signature": this.cookieService.get('Test')},"data": formValuePolicyPDF } ;
  return this.http
  .post<PolicyData[]>(this.apiUrl + this.javaPolPDFUrl, body, { headers: headers })
    .pipe(
      tap(data => console.log('polpdfPreview: ' + JSON.stringify(data))),
      catchError(this.handleError),

    );
}

组件侧

onSubmitPolicyPDF() {
    let formValuePolicyPDF: any = {
        // policy_number: this.submitprop.data.policy_number,
        policy_number: 72600964,

    }
    //console.log(this.submitprop.data.policy_number);
    this.policyService.policyPDF(formValuePolicyPDF) 
        .pipe()
        .subscribe(poldownloaddata => {
            this.poldownloaddata = poldownloaddata;
                console.log('poldownloaddata: ', poldownloaddata);
            // console.log(atob(this.poldownloaddata.data.pdf_ccnt));
            const linkSource =  'data:application/pdf;base64,' + this.poldownloaddata.data;
            // const linkSource = (atob(this.previewdata.data.pdf_ccnt));
        console.log(this.poldownloaddata.data)
            const downloadLink = document.createElement("a");
            const fileName = "policyJava.pdf";
            downloadLink.href = linkSource;
            downloadLink.download = fileName;
            downloadLink.click();
            (error: any) => this.errorMessage = <any>error
        }
        )
}

0 个答案:

没有答案