Currently i am working in hybrid application. i received base 64 string which i want to get downloaded as pdf and open it when user clicks on download button. I managed to convert to pdf refering to the below link It is getting stored in local storage of the device. In android file getting stored in internal storage.But for ios pdf is not accessibe inside local storage and ibooks is not identifying the file. how to make pdf available in download folder in android and ibooks for ios for better user experience and make pdf open when download is done?.
function b64toBlob(b64Data, contentType, sliceSize) {
var input = b64Data.replace(/\s/g, ''),
byteCharacters = atob(input),
byteArrays = [],
offset, slice, byteNumbers, i, byteArray, blob;
contentType = contentType || '';
sliceSize = sliceSize || 512;
for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
slice = byteCharacters.slice(offset, offset + sliceSize);
byteNumbers = new Array(slice.length);
for (i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
//Convert to blob.
try {
blob = new Blob(byteArrays, { type: contentType });
}
catch (e) {
// TypeError old chrome, FF and Android browser
window.BlobBuilder = window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
if (e.name == 'TypeError' && window.BlobBuilder) {
var bb = new BlobBuilder();
for (offset = 0; offset < byteArrays.length; offset += 1) {
bb.append(byteArrays[offset].buffer);
}
blob = bb.getBlob(contentType);
}
else if (e.name == "InvalidStateError") {
blob = new Blob(byteArrays, {
type: contentType
});
}
else {
return null;
}
}
return blob;
};
And then the downloading itself we need the cordova-file plugin:
function pdfConversion(){
alert("The paragraph was clicked.");
var fileName="test.pdf";
var fileToSave= b64toBlob(encodedString, 'application/pdf');
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, onFileSystemFail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile(fileName, {create: true}, onFileEntryRetrieved, onFileSystemFail);
}
function onFileEntryRetrieved(fileEntry) {
console.log("file entry retrieved");
fileEntry.createWriter(gotFileWriter, onFileSystemFail);
}
function gotFileWriter (writer) {
console.log("inside local file system"+JSON.stringify(writer));
alert("JSON.stringify(writer)"+JSON.stringify(writer));
console.log("contents of file now 'some sample text'");
//writer.truncate(11);
writer.onwriteend = function(evt) {
writer.write(fileToSave);
writer.seek(4);
}
writer.onerror = function (e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log('Write failed: ' + e.toString());
};
writer.write(fileToSave);
window.open(fileName, '_blank');
};
function onFileSystemFail(error) {
console.log(error.code);
}
};
In Android, we have access to write and read PDF. But in IOS, Sandbox access has certain restriction to read.
I managed to open PDF using encoded string as follows
window.open("data:application/pdf;base64,"+encodedString, "_blank","location=no,hidden=no,closebuttoncaption=Close");