Beruflich Dokumente
Kultur Dokumente
JOIN THE
IONIC
How to Open PDF Files with Ionic 4 on Android and iOS
Posted on January 4th, 2019 ACADEMY
IN-DEPTH COU
TUTORIALS
SEARCH EXCLUSIVE HA
PROJECTS & RE
ACTIVE, SUPPO
COMMUNITY
SEARCH
CLICK HERE TO
STARTED
PDF that we supply with our app and one that we Layout with
previous
We use cookiesversion
to ensure for
that Ionic
we give3you
wethewill
bestnow How
haveonaour website. If you continue
experience tothis site we will
to use
assume that you are happy with it. Read and
OK READ MORE
Create QR
switch between iOS and Android to handle things Codes with
di erently. Ionic [v4]
Ionic Native
Contact
Management
(Call, SMS)
[v4]
JOIN THE
Building an
Ionic
IONIC
Theme
ACADEMY
Switcher &
Dynamic
CSS IN-DEPTH COU
TUTORIALS
Variables
[v4]
EXCLUSIVE HA
How To
PROJECTS & RE
Scroll Ionic
4 List ACTIVE, SUPPO
COMMUNITY
Manually
From Code
CLICK HERE TO
[v4] STARTED
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
OK READ MORE
With the previous Document Viewer plugin we had
to use a speci c app on Android, so this time we are
rolling with a more general plugin that will
automatically use your preferred reader.
OK READ MORE
Next we need to add all of them to our
app/app.module.ts just like we are used to, so go
ahead and insert:
JOIN THE
6 import { SplashScreen } from '@ionic-native/splash-screen
7 import { StatusBar } from '@ionic-native/status-bar/ngx';
8
9
10
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module'; IONIC
11
12
13
import { File } from '@ionic-native/File/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx
ACADEMY
14 import { FileTransfer } from '@ionic-native/file-transfer
15 import { DocumentViewer } from '@ionic-native/document-vi
16
17 @NgModule({ IN-DEPTH COU
18 declarations: [AppComponent], TUTORIALS
19 entryComponents: [],
20 imports: [BrowserModule, IonicModule.forRoot(), AppRout
21 providers: [ EXCLUSIVE HA
22 StatusBar,
23 SplashScreen, PROJECTS & RE
24 { provide: RouteReuseStrategy, useClass: IonicRouteSt
25 File,
26 FileOpener, ACTIVE, SUPPO
27 FileTransfer, COMMUNITY
28 DocumentViewer
29 ],
30 bootstrap: [AppComponent]
31 })
32 export class AppModule {} CLICK HERE TO
STARTED
OK READ MORE
Now go ahead and change your
app/home/home.page.ts to:
JOIN THE
8 @Component({
9 selector: 'app-home',
10 templateUrl: 'home.page.html',
11
12
styleUrls: ['home.page.scss'],
}) IONIC
13
14
15
export class HomePage {
constructor(private platform: Platform, private file: F
ACADEMY
16 private fileOpener: FileOpener, private document: Doc
17
18 }
19 IN-DEPTH COU
20 openLocalPdf() { TUTORIALS
21 let filePath = this.file.applicationDirectory + 'www/
22
23 if (this.platform.is('android')) { EXCLUSIVE HA
24 let fakeName = Date.now();
25 this.file.copyFile(filePath, '5-tools.pdf', this.fi PROJECTS & RE
26 this.fileOpener.open(result.nativeURL, 'applicati
27 .then(() => console.log('File is opened'))
28 .catch(e => console.log('Error opening file', e ACTIVE, SUPPO
29 }) COMMUNITY
30 } else {
31 // Use Document viewer for iOS for a better UI
32 const options: DocumentViewerOptions = {
33 title: 'My PDF'
34 } CLICK HERE TO
35 this.document.viewDocument(`${filePath}/5-tools.pdf STARTED
36 }
37 }
38 }
Android Quirks
Not completely related to the content but the JOIN THE
Android build failed for me when using these plugins IONIC
so I wanted to add a little note.
ACADEMY
First, I had to downgrade my platform because
Cordova 7 didn’t work out well so I ran: IN-DEPTH COU
TUTORIALS
But that’s not enough, due to some libraries being ACTIVE, SUPPO
COMMUNITY
included by our File plugin we need to make a little
hack as well. I found this hack inside this Github
issue and the change you need to apply is adding CLICK HERE TO
STARTED
the below snippet at the end of your
platforms/android/build.gradle:
Conclusion
If you understand the general behaviour behind the
le system and how the URL works you can easily
open or copy all kind of local information and pass JOIN THE
the information to other plugins. IONIC
In our case we used a PDF le but other les would
ACADEMY
work as well, just make sure you get the path,
ending and letype right and you shouldn’t run into IN-DEPTH COU
TUTORIALS
any trouble.
EXCLUSIVE HA
You can also nd a video version of this Quick Win PROJECTS & RE
below.
ACTIVE, SUPPO
COMMUNITY
How to Open PDF Files with Ionic 4 on Andr…
Andr…
CLICK HERE TO
STARTED
Comments Community
1 Login
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Recommend t Tweet f Share Sort
by
Best
assume that you are happy with it.
OK READ MORE
Join the discussion…
Join the discussion…
LOG
IN
WITH OR
SIGN
UP
WITH
DISQUS ?
Name
JOIN THE
3△ ▽ • Reply • Share ›
Sampath
Lokuge • a month ago
Hi, This is working perfectly fine on iOS. But I have a
problem with Android 9. It doesn't open the pdf. But it
works nicely with Android 7 and 8 devices. Any clue?
△ ▽ • Reply • Share ›
Thomas
Pinna • 4 months ago
This didn't work for me, since some apis used seem
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
deprecated. assume that you are happy with it.
OK for the
However, what did work for me was READ MORE
"Download
and Open PDF" usecase was simply window open(url);
and Open PDF usecase was simply window.open(url);
Luís
Cunha • 8 months ago
Is there any good solution on Android that doesn't
require the user to have an external PDF opener
application installed?
△ ▽ • Reply • Share ›
Mudasir
Bhutto > Luís Cunha • 7 months ago
JOIN THE
Yes, use docs.google.com and it's best solution IONIC
for not only pdf, but also .doc, .docx, .ppt, .pptx,
.xls, .xlsx etc ACADEMY
△ ▽ • Reply • Share ›
IN-DEPTH COU
ALSO
ON
IONIC
ACADEMY TUTORIALS
Building
an
Ionic
Theme How
to
Use
Font
Awesome
Switcher
&
Dynamic
CSS With
Ionic
4 EXCLUSIVE HA
1 comment • 2 months ago 2 comments • a year ago PROJECTS & RE
ACTIVE, SUPPO
COMMUNITY
CLICK HERE TO
© Ionic Academy - 2019 DISCLAIMER STARTED
PRIVACY
SUPPORT
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
OK READ MORE