Ajantha Bandara
Ajantha Bandara

Reputation: 1521

How to open pdf files in assets folder ionic 3

I am working on a ionic 3 project and I want to open pdf files inside the "assets/pdf" folder. I used ionic-native file opener for this. But it throws an error saying file not found

import { File } from '@ionic-native/file';
import { FileOpener } from '@ionic-native/file-opener';

constructor(private file: File, private fileOpener: FileOpener) { }

public openFile() {
    const url = "assets/pdf/unit1.pdf";
    this.fileOpener.open(this.file.applicationDirectory + url, 'application/pdf')
        .then(() => console.log('File is opened'))
        .catch(e => {
          console.log('Error openening file', e);
        });
}

It seems like an error in the path. But I don't know how to use it.Anyone knows how to use this function?

Upvotes: 3

Views: 5823

Answers (2)

code_vengeance
code_vengeance

Reputation: 31

What I did to get past this issue was copy the file to externalCacheDirectory prior to opening it.

this.file.copyFile(this.file.applicationDirectory + 'www/assets/pdf/', this.filename, this.file.externalCacheDirectory, this.filename)
    .then( _ => {
        this.fileOpener.open(this.file.externalCacheDirectory + this.filename, 'application/pdf')
        .then( _ => {
            console.log('File opened');
        })
        .catch(e => console.log("Error opening file: " + JSON.stringify(e)));
     })
     .catch( e => {
        console.log("Error copying file: " + JSON.stringify(e));
     });

Upvotes: 2

Rocky2014
Rocky2014

Reputation: 21

I imagine you confirmed if the file is really in that path. You can try to use Ionic Document Viewer, please take a look at this: Ionic Document Viewer , should solve your problem, give it a try..

Upvotes: 0

Related Questions