Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Service in ionic 4 to provide method for downloading and opening a pdf file
import { File } from "@ionic-native/file/ngx";
import { FileTransfer } from "@ionic-native/file-transfer/ngx";
import { FileOpener } from "@ionic-native/file-opener/ngx";
import { Injectable } from "@angular/core";
providedIn: "root"
export class PdfViewerService {
fileTransfer: FileTransferObject;
private fileOpener: FileOpener,
private transfer: FileTransfer,
private file: File
) {}
download(url: string, title: string) {
this.fileTransfer = this.transfer.create();
.download(url, this.file.dataDirectory + title + ".pdf")
.then(entry => {
console.log("download complete: " + entry.toURL());
.open(entry.toURL(), "application/pdf")
.then(() => console.log("File is opened"))
.catch(e => console.log("Error opening file", e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment