How to open a remote PDF in URL?


#1

Friends,
I need to open a PDF , the link is a remote URL . My template code is

<ion-list *ngSwitchCase="'GovOrders'" ngSelected="selected">

    <ion-list  *ngIf="spinner=='true'">
 <button ion-item *ngFor="let go of orders" (click)="downloadViewImage(go.link)">
        <ion-avatar item-right >
            <img src={{icon}}/>
        </ion-avatar>
        <h2>{{go.gono}} {{go.orderdate}}</h2>
        <p>{{go.description}}</p>
        </button>  

    </ion-list>

I need to implement the open the PDF in fuction

downloadViewImage(go.link)

here link can passed .

Please advise

Thanks

Anes


#2

Hi,
I do it following way

downloadViewImage(url){
    window.open(encodeURI(url),"_system","location=yes");
}

It will open the default phone browser with the link in the browser.
Works both Android and IOS


#3

Dear @pavel_savchuk,

My file is PDF not image … is that enough ?

Anes


#4

It does not matter. I use the same window.open() to open images, docs, pdf and other file formats.
It works for all of them.


#5

Dear @pavel_savchuk and friends,

A simple code rearrangement in current code base do the Trick !!

for your reference I add that function on ts file

downloadViewImage(fileurl) {
                      if (this.platform.is('android')) {
                        
                                        FileOpener.appIsInstalled('com.adobe.reader').then(function(res) {
                                    if (res.status === 0) {
                                
                                        this.global1.showToast("Adobe Reader Not Installed");
                                    } else 
                                    {
                                       // blah blah
                                    }

                                });
                            }
                        this.spinner="false";
                        this.platform.ready().then(() => {
                            GoogleAnalytics.trackEvent("God Order", "PDF Download", "Download", 1);
                        });                      
                        //---------------------get Filename-----------------------
                        var result1 =   fileurl.split('/');
                        var result=result1[result1.length-1];
                        var result2 =   result.split('&');
                        var result3=result2[0];
                        var result4 =   result3.split('=');
                    var image=result4[1];
                    //this.global1.showToast(image);                        
                    File.checkFile(this.storageDirectory, image)
                            .then(() => {

                            const alertSuccess = this.alertCtrl.create({
                                title: 'File retrieval Succeeded!',
                                subTitle: '${image} was successfully retrieved from: ${this.storageDirectory}',
                                buttons: ['Ok']
                            });
                    FileOpener.open(this.storageDirectory+image,    // Any system location, you CAN'T use your appliaction assets folder
                            'application/pdf'
                        ).then(function() {
                            console.log('Success');
                        }, function(err) {
                            console.log('An error occurred: ' + JSON.stringify(err));
                                const alertFailure = this.alertCtrl.create({
                                    title: "Can't Open File!",
                                subTitle: "PDF View Failed",
                                    buttons: ['Ok']
                                });

                                    alertFailure.present();

                        });
                       
                            })
                            .catch((err) => {
                            this.platform.ready().then(() => {
                            const fileTransfer = new Transfer();
                            const pdfLocation = 'http://go.baby.mov.in/files/'+image;                        
                                fileTransfer.download(pdfLocation, this.storageDirectory + image).then((entry) => {
                                    FileOpener.open(this.storageDirectory+image,    // Any system location, you CAN'T use your appliaction assets folder
                            'application/pdf'
                        ).then(function() {
                            console.log('Success');
                        }, function(err) {
                            console.log('An error occurred: ' + JSON.stringify(err));
                                const alertFailure = this.alertCtrl.create({
                                    title: "Can't Open File!",
                                subTitle: "PDF View Failed",
                                    buttons: ['Ok']
                                });

                                    alertFailure.present();

                        });




                                    }, (error) => {

                                    const alertFailure = this.alertCtrl.create({
                                    title: 'Download Failed!',
                                subTitle: 'PDF was Failed to  downloaded.',
                                    buttons: ['Ok']
                                });

                                    alertFailure.present();

                                });
                        
                            
                        });

                            });
                        this.spinner="true";
    
  }

Thanks

Anes