1

I'm trying to open a pdf by pressing a button on Ionic Capacitor using the FileOpener plugin, but it doesn't work, would anyone know how to help me?

The pdf is stored into assets/documents

In the console I get this error ".open, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator cordovaWarn @ common.js:284 ch".

This is my 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 { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

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

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    File,
    FileOpener
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

And this is my page backend: open-pdf.page.ts:

import { Component, OnInit } from '@angular/core';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { File, IWriteOptions } from '@ionic-native/file/ngx';

@Component({
  selector: 'app-open-pdf',
  templateUrl: './open-pdf.page.html',
  styleUrls: ['./open-pdf.page.scss'],
})
export class OpenPdfPage implements OnInit {

  constructor(private fileOpener: FileOpener) { }

  ngOnInit() {
  }

  openPdf(){
    this.fileOpener.open('/assets/documents/mizzica.pdf', 'application/pdf')
    .then(() => console.log('File is exported'))
    .catch((e) => console.log(e));
  }

}

And this the HTML:

<ion-header [translucent]="true">
  <ion-toolbar>
    <ion-title>openPdf</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">
  <ion-header collapse="condense">
    <ion-toolbar>
      <ion-title size="large">openPdf</ion-title>
    </ion-toolbar>
  </ion-header>
  <ion-button (click)="openPdf()">Apri PDF</ion-button>
</ion-content>

Thanks you

I want to show a pdf on the screen

FalconZ
  • 11
  • 1

1 Answers1

0

I recommend using a Capacitor File Opener plugin, e.g. Capawesome File Opener or Capacitor Community File Opener.

RGe
  • 1,181
  • 1
  • 10
  • 19
  • openPdf() { FileOpener.openFile({path: 'assets/documents/documento.pdf'}) } I wrote this, but it doesn't worked, this is my error: e): Error: Not implemented on web. Error: Not implemented on web. I can't open my file on the smartphone too – FalconZ Jun 05 '23 at 09:27
  • now it work on my app, but the debugger said thant the file doesn't exist, but the path it's correct – FalconZ Jun 05 '23 at 14:26