Hi,
This is my implementation and now works fine, I hope this need you.
My app gets pdf from base 64 in JSon format.
public getPDF(dataBlob) {
let blob: Blob = this.b64toBlob(dataBlob, 'application/pdf');
let pathFile: string;
if (this.platform.is("ios")) {
console.log('ios detectado');
pathFile = cordova.file.documentsDirectory; //cordova.file.dataDirectory; externalDataDirectory
} else {
console.log('other platform');
pathFile = cordova.file.externalDataDirectory; //cordova.file.dataDirectory; externalDataDirectory
}
console.log(pathFile);
let fileName : string = "snMaterialEscolar.pdf";
console.log("Iniciando escrita do arquivo pdf");
this.file.writeFile(pathFile,fileName, blob, {replace:true} ).then((entry)=>{
console.log(pathFile);
// if (this.platform.is("ios")) {
// window.open(pathFile + fileName, '_system', 'location=yes,closebuttoncaption=Fechar,enableViewportScale=yes');
// } else {
// window.open(pathFile + fileName, '_system', 'location=yes,closebuttoncaption=Fechar,enableViewportScale=yes');
// }
// let optionsArray:DocumentViewerOptions[] = [];
// optionsArray.push(this.options);
cordova.plugins.SitewaertsDocumentViewer.viewDocument(pathFile + fileName, 'application/pdf', this.options, this.onShow, this.onClose, this.onMissingApp, this.onError);
}).catch((error)=>{
console.log('Erro criação arquivo PDF:');
console.log(error);
});
}
private onShow(){
window.console.log(âExibir documento.â);
//e.g. track document usage
}
private onClose(){
window.console.log(âFechar documentoâ);
//e.g. remove temp files
}
private onMissingApp(appId, installer) {
if (confirm(âVocĂȘ quer instalar o app grĂĄtis PDF Viewer " + appId + " para Android?â)) { installer(); }
}
private onError(error) {
window.console.log(error);
alert(âErro ao brir pdf.â);
}
/**
public b64toBlob(b64Data, contentType) : Blob {
contentType = contentType || ââ;
let sliceSize = 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}