Home > other >  Angular download pdf file with blank page
Angular download pdf file with blank page

Time:10-14

I trie to do download a pdf file from a String that I got from my backend and convert to blob in the angular but when I open the pdf, I have all pages of my pdf but they are empty

My angular code

 download() {
    const blob = new Blob([this.responseBody], {
      type: 'application/pdf'
    });
    const fileURL = window.URL.createObjectURL(blob);
    window.open(fileURL);
  }

My string look like this :

%PDF-1.4 %ª«¬­ 1 0 obj << /Creator (Apache FOP Version 2.1) /Producer (Apache FOP Version 2.1) /CreationDate (D:20221014064448Z) >> endobj 2 0 obj << /N 3 /Length 3 0 R /Filter /FlateDecode >> stream xwXSçÇßsNö`$!l{¥@

What wrong on my angular code ?

CodePudding user response:

If data format as base 64 first use bellow code,

 const byteArray = new Uint8Array(
  atob(this.responseBody)
    .split('')
    .map((char) => char.charCodeAt(0))
);
const url = window.URL.createObjectURL(
  new Blob([byteArray], { type: 'application/pdf' })
);
window.open(url, '_blank');

If not use bellow code,

const url = window.URL.createObjectURL(
  new Blob([this.responseBody], { type: 'application/pdf' })
);
window.open(url, '_blank');
  • Related