How to download and show a pdf file from remote site?

Hi,
I have a web service that when is called returns a pdf file in a row format.

For example, calling the URL:
http://<mysite>/acquiredoc/8/2809/30/58/c9f1b93bdb89210aa7ea05e2c704be05

I have the file as the response:

H

Which is the best way to manage these raw data with Ionic3 ?
I’d like to show the downloaded file to the customer.

Thank you very much

cld

you can use this plugin

It seems that it allows you to open only local files:
File Opener: This plugin will open a file on your device file system with its default application.

yes you are right so you have to download your file using file transfer plugin

this will download your file in local memory and you just have to give local path to file opener plugin
that’s it !!:sweat_smile:

here is working code :slight_smile:

extToMimes = [
    { ext: 'jpeg', MType: 'image/jpeg' },
    { ext: 'jpg', MType: 'image/jpeg' },
    { ext: 'png', MType: 'image/png' },
    { ext: 'doc', MType: 'application/msword' },
    { ext: 'docx', MType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' },
    { ext: 'xls', MType: 'application/vnd.ms-excel' },
    { ext: 'xlsx', MType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' },
    { ext: 'gif', MType: 'image/gif' },
    { ext: 'pdf', MType: 'application/pdf' }
  ]

 download(name) { //name : any file name
    const fileTransfer: FileTransferObject = this.transfer.create();
    const url = yourserverul + name;
    fileTransfer.download(url, this.filePlug.dataDirectory + name).then((entry) => {
      this.fileOpener.open(entry.toURL(), this.getMimeByExt(name))
        .then(() => console.log('File is opened'))
        .catch(e => console.log('Error opening file', e));
    }, (error) => {
      console.log(error);

    });
  }

  getMimeByExt(name: any) {
    var extention = name.split('.').pop();
    for (let i = 0; i < this.extToMimes.length; i++) {
      const element = this.extToMimes[i];
      if (element.ext == extention) {
        return element.MType;
      }
    }
  }
2 Likes

Thank you very much

cld

At the end I’ve used this code using DocumentViewer:

  openRemoteFileByUrl(fileUrl, content_type='application/pdf'){
    const transfer = this.transfer.create();
    let path = null;
    if (this.platform.is('ios')) {
      path = this.file.documentsDirectory;
    } else if (this.platform.is('android')) {
      path = this.file.dataDirectory;
    }
    transfer.download(fileUrl, path + 'myfile.pdf').then(entry => {
      let url = entry.toURL();
      this.document.viewDocument(url, content_type, {});
    });
  }

It works, however I’ve seen that the use of FileTransfer is deprecated.
For now I’ll use it the same, but I’ll look for a different way of downloading the file.

cld

am working on a similar issue can yu help me on this