Reputation: 11198
I am trying to open a PDF using FileOpener
. I followed this tutorial https://ionicacademy.com/create-pdf-files-ionic-pdfmake/ which is for Ionic 3 so I had to adjust a few things. I have it working in the browser and on the iOS simulator, but when I test on my device through the Ionic Viewer app, I get an error saying plugin_not_installed
when I click the download button and downloadPdf()
gets called.
I have installed the cordova plugins via:
ionic cordova plugin add cordova-plugin-file-opener2
and
ionic cordova plugin add cordova-plugin-file
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { File } from '@ionic-native/file/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule
],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
File,
FileOpener
],
bootstrap: [AppComponent]
})
export class AppModule {}
home.page.ts
import { Component } from '@angular/core';
import { NavController, Platform, AlertController } from '@ionic/angular';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import { File } from '@ionic-native/file/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
letterObj = {
to: 'Ronnie',
from: 'Shana',
text: 'my message'
}
pdfObj = null;
constructor(public navCtrl: NavController, private plt: Platform, private file: File, private fileOpener: FileOpener, private alertController: AlertController) { }
createPdf() {
var docDefinition = {
content: [
{ text: 'REMINDER', style: 'header' },
{ text: new Date().toTimeString(), alignment: 'right' },
{ text: 'From', style: 'subheader' },
{ text: this.letterObj.from },
{ text: 'To', style: 'subheader' },
this.letterObj.to,
{ text: this.letterObj.text, style: 'story', margin: [0, 20, 0, 20] },
{
ul: [
'Bacon',
'Rips',
'BBQ',
]
}
],
styles: {
header: {
fontSize: 18,
bold: true,
},
subheader: {
fontSize: 14,
bold: true,
margin: [0, 15, 0, 0]
},
story: {
italic: true,
alignment: 'center',
width: '50%',
}
}
}
this.pdfObj = pdfMake.createPdf(docDefinition);
}
downloadPdf() {
if (this.plt.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'myletter.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'myletter.pdf', 'application/pdf')
.then(() => this.presentAlert('opened'))
.catch(error => this.presentAlert(error)); // Error thrown here.
})
.catch(error => {
// an error
this.presentAlert(error);
})
});
} else {
// On a browser simply use download!
this.pdfObj.download();
}
}
async presentAlert(message) {
const alert = await this.alertController.create({
header: 'Alert',
subHeader: 'Subtitle',
message: message,
buttons: ['OK']
});
await alert.present();
}
}
Upvotes: 2
Views: 2610
Reputation: 789
The import path as shown in the doc here is apparently wrong. I used to get "Invalid provider for ngModule", so searching in the plugin source files I found a different import path, which apparently works.
I could import the plugin using
import { FileOpener } from "@ionic-native/file-opener/ngx";
both in my app.module.ts and in my page.
Upvotes: 2