0

I am currently working in angular 6. i have to download pdf file from bytearray. when i tried to download an pdf file. it downloads but when i open it. it shows error. please help me with it.

In the download.component.ts i am getting response as two values. first pdfname, second pdfbytearray. i am getting those values from component response.

sample response:

{
pdfName: "test.pdf",
pdfByteArray: "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9Qcm9kdWNlcij"
}

download.service.ts:

    generatePdf(id) {
    const httpOptions = {
     'responseType': 'arraybuffer' as 'json'
    };
    return this.http.post<any>(URL.BASE_URL + 'application/generate?id='+ id,
     { httpOptions },
    );
   }

download.component.ts:

import { saveAs } from 'file-saver/FileSaver';
generatePdf() {
    this.downloadApiService.generatePdf('4')
    .subscribe( pdf => {
      console.log('pdf response: ', pdf);    
      var mediaType = 'application/pdf';
      var blob = new Blob([pdf.pdfByteArray], {type: mediaType});
      let fileName = pdf.pdfName;
      saveAs(blob, fileName);
    }, err => {
      console.log('Pdf generated err: ', JSON.stringify(err));
    });
  }
  • You are missing a base-64 decoding step. See [this question](https://stackoverflow.com/q/16245767) for approaches to convert a base-64 string to a Blob. When I do that, I get something that looks vaguely like a PDF file (it contains `%PDF-1.4`), though my PDF viewer still claims the file is corrupt. – Matt McCutchen Sep 25 '18 at 14:27
  • thanks man. i got it. that worked for me. – Kumaresan Perumal Sep 27 '18 at 05:53

0 Answers0