Skip to content

Instantly share code, notes, and snippets.

Embed
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";
@Injectable({
providedIn: "root"
})
export class PdfViewerService {
fileTransfer: FileTransferObject;
constructor(
private fileOpener: FileOpener,
private transfer: FileTransfer,
private file: File
) {}
download(url: string, title: string) {
this.fileTransfer = this.transfer.create();
this.fileTransfer
.download(url, this.file.dataDirectory + title + ".pdf")
.then(entry => {
console.log("download complete: " + entry.toURL());
this.fileOpener
.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