1

I am working on to read PDF data (not pdf url) from a post api and open the pdf using the pdf viewers installed on phone in my IONIC 4 android app. My File transfer plugin gives error with error code 2 on downloading that file. Here is my code which I have written. May I know where I went wrong?

my.ts file

  var apiData="https://kairavforex.com/media/documents/reports/hello_TYUtZF5.pdf";
        var sdsn={
           path:apiData
        }

   this.http.post('https://kairavforex.com/api/download_report/',sdsn,{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})
        .then(data=>{
              console.log(data.data)              
              const fileTransfer: FileTransferObject = this.transfer.create();
              fileTransfer.download(data.data, this.file.externalDataDirectory + 'file.pdf').then((entry) => {
                this.fileOpener.showOpenWithDialog(entry.toURL(), 'application/pdf')
                .then(() => console.log('File is opened'))
                .catch(e => console.log('Error opening file', e));
              }, (error) => {
                 console.log(error)
              });
        })

Here is the image of the error and my pdf response which is correct because when I save the same to my pc aspdf and open it works fine enter image description here

Console error enter image description here

Ravi Shah
  • 105
  • 1
  • 11

2 Answers2

0

Do below steps , hope it will help

Step 1: Install PDF Generator from https://ionicframework.com/docs/native/pdf-generator

Step 2 : Install File opener to open the file https://ionicframework.com/docs/native/file-opener

Step 3 : Install doucment viewer to view the documents https://ionicframework.com/docs/native/document-viewer

Kevin Jose
  • 856
  • 8
  • 22
0

Solved this issue by using Files and fileOpener plugin. After getting the pdf data from api response, I have written it and saved it to a file and then read the data using fileOpener plugin which solved the issue.

this.urldatanam="hello.pdf";
  this.http.post('https://kaiabc.com/api/download_report/',downloadURl,{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})
        .then(data=>{
          console.log(data)
              var pdfread=data.data;
              this.file.writeFile(this.file.externalDataDirectory, this.urldatanam, pdfread, { replace: true }).then(data=>{
                console.log(data)
                this.fileOpener.showOpenWithDialog(this.file.externalDataDirectory+this.urldatanam, 'application/pdf')
                .then(() => console.log('File is opened'))
                .catch(e => console.log('Error opening file', e));
              })  
        })
Ravi Shah
  • 105
  • 1
  • 11