离子4 FileOpener plugin_not_installed

时间:2018-12-10 20:31:22

标签: angular cordova ionic-framework ionic4

我正在尝试使用FileOpener打开PDF。我遵循了适用于Ionic 3的本教程https://ionicacademy.com/create-pdf-files-ionic-pdfmake/,因此我不得不进行一些调整。我可以在浏览器和iOS模拟器上使用它,但是当我通过Ionic Viewer应用程序在设备上进行测试时,单击下载按钮时出现错误提示plugin_not_installed,并且调用downloadPdf()

我通过以下方式安装了cordova插件: ionic cordova plugin add cordova-plugin-file-opener2ionic 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();
  }

}

1 个答案:

答案 0 :(得分:0)

文档here中所示的导入路径显然是错误的。 我曾经得到“ ngModule的无效提供程序”,因此在插件源文件中搜索时,我发现了一条不同的导入路径,显然很有效。

我可以使用

导入插件
import { FileOpener } from "@ionic-native/file-opener/ngx";

在我的app.module.ts和页面中。