0

When I tried to load pdf in iframe, initialy it loads 404 error page and then loads the pdf file.

I thought, it would be url issue when iframe loads.

So I have tried with *ngIf. So once url this.sanitizer.bypassSecurityTrustResourceUrl will be ready and then iframe will get loaded but no luck.

Can I get some expert inputs?

Here my try

HTML

<iframe    
        [src]="pdfUrl"    
        [style.width]="600"    
        [style.height]="600"

      ></iframe>

Component TS

    this.marketingService.getMarketingFile(url).subscribe((response) => {        
          if (response) {        
            let blob = this.b64toBlob(response);        
            this.testUrl = URL.createObjectURL(blob);        
            this.testUrl += "#toolbar=0&navpanes=0&scrollbar=0";        
            this.pdfUrl = this.sanitizer.bypassSecurityTrustResourceUrl(        
              this.testUrl        
            );
});


b64toBlob(b64Data) {

    const byteCharacters = atob(b64Data);    
    const byteArrays = [];         

    for (let offset = 0; offset < byteCharacters.length; offset += 512) {    
      const slice = byteCharacters.slice(offset, offset + 512);
      const byteNumbers = new Array(slice.length);    
      for (let i = 0; i < slice.length; i++) {    
        byteNumbers[i] = slice.charCodeAt(i);    
      }         

      const byteArray = new Uint8Array(byteNumbers);    
      byteArrays.push(byteArray);

    }

    if (this.marketingFileUrl && this.marketingFileUrl.endsWith(".pdf")) {    
      this.fileType = "application/pdf";    
    } else if (    
      this.marketingFileUrl &&    
      this.marketingFileUrl.endsWith(".png")    
    ) {

      this.fileType = "image/png";    
    }

    const blob = new Blob(byteArrays, { type: this.fileType });    
    return blob;

  }

Service TS

getMarketingFile(fileName: any): Observable<any> {    
    return this.http.get(    
      `${environment.apiUrl}/marketingContent/getMarketingFileBytes?fileName=` +

        fileName    
    );

  }
klmuralimohan
  • 861
  • 4
  • 23
  • 44

0 Answers0