Angular : converting blob into pdf

后端 未结 1 672
南旧
南旧 2020-12-22 13:39

i created a SPRING BOOT service which can store different type of files. when i tried to consume this service into ANGULAR , the images works as well but when i try to displ

相关标签:
1条回答
  • 2020-12-22 13:55

    You cant pass the blob file as src in pdf viewer, you have to convert it to safeUrl to preview. Hope this will help.

    import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; // import
    constructor(private sanitizer: DomSanitizer) // include in constructor
    
     if (this.retrieveResonse.fileType == "pdf") {
              var blob = new Blob([this._base64ToArrayBuffer(this.base64Data)], {
            type: "application/doc"
          });
    
    
          const url = URL.createObjectURL(blob);
    
          this.retrievedFile = window.open(url);
    

    the base64ToArrayBuffer methods:

    _base64ToArrayBuffer(base64) {
        const binary_string = window.atob(this.base64Data);
        const len = binary_string.length;
        const bytes = new Uint8Array(len);
        for (let i = 0; i < len; i++) {
          bytes[i] = binary_string.charCodeAt(i);
        }
        return bytes.buffer;
      }
    
    0 讨论(0)
提交回复
热议问题