Base64 to PDF Cordova Ionic v22019 Community Moderator Electionopen pdf from base64 in ionicHow can you encode a string to Base64 in JavaScript?Decode Base64 data in JavaIs embedding background image data into CSS as Base64 good or bad practice?Embedding Base64 ImagesBinary Data in JSON String. Something better than Base64How to convert image into base64 string using javascriptHow to do Base64 encoding in node.js?How to display Base64 images in HTML?How do I encode and decode a base64 string?Playing a video javascript blob within Cordova / Phonegap on IOS

Fair way to split coins

Error in master's thesis, I do not know what to do

Can "few" be used as a subject? If so, what is the rule?

Could any one tell what PN is this Chip? Thanks~

Why is "la Gestapo" feminine?

Do I need to convey a moral for each of my blog post?

Weird lines in Microsoft Word

PTIJ: Which Dr. Seuss books should one obtain?

Friend wants my recommendation but I don't want to

PTIJ: Why do we make a Lulav holder?

Do people actually use the word "kaputt" in conversation?

Does fire aspect on a sword, destroy mob drops?

Nested Dynamic SOQL Query

Is VPN a layer 3 concept?

Exit shell with shortcut (not typing exit) that closes session properly

Is xar preinstalled on macOS?

Why do I have a large white artefact on the rendered image?

Why are there no stars visible in cislunar space?

Why doesn't the fusion process of the sun speed up?

How do researchers send unsolicited emails asking for feedback on their works?

Emojional cryptic crossword

pipe commands inside find -exec?

DisplayForm problem with pi in FractionBox

Which partition to make active?



Base64 to PDF Cordova Ionic v2



2019 Community Moderator Electionopen pdf from base64 in ionicHow can you encode a string to Base64 in JavaScript?Decode Base64 data in JavaIs embedding background image data into CSS as Base64 good or bad practice?Embedding Base64 ImagesBinary Data in JSON String. Something better than Base64How to convert image into base64 string using javascriptHow to do Base64 encoding in node.js?How to display Base64 images in HTML?How do I encode and decode a base64 string?Playing a video javascript blob within Cordova / Phonegap on IOS










1















I want to download a PDF file in cordova app. I recive a base64 string that is that PDF.



I tried the next code using cordova-plugin-file and cordova-plugin-file-opener2:



openPDF(invoice) 
this.saveAndOpenPdf(invoice['base64'], 'invoice.pdf');


saveAndOpenPdf(pdf: string, filename: string)
const load = this.loadingCtrl.create(
content: this.translate.instant("app.loading"),
dismissOnPageChange: true
);
load.present();
var writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalRootDirectory;
console.log(writeDirectory);
var self = this;
this.checkPermissions()
.then(() =>
var res = this.b64toBlob(pdf, 'aplication/pdf', 512);
console.log(res);
( < any > window).resolveLocalFileSystemURL(writeDirectory, function(dir)
console.log("Access to the directory granted succesfully");
dir.getFile(filename,
create: true
, function(file)
console.log("File created succesfully.");
file.createWriter(function(fileWriter)
console.log("Writing content to file");
fileWriter.write(res);
self.opener.open(writeDirectory + filename, 'application/pdf').then(() =>
load.dismiss();
)
.catch((err) =>
console.error(err);
console.log('Error opening pdf file');
load.dismiss();
);

, function()
load.dismiss();
alert('Unable to save file in path ' + writeDirectory);
);
);
);
).catch((error) =>
load.dismiss();
console.log(error);
);


b64toBlob(b64Data, contentType, 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;



It seems that works well but when the opener try to open the PDF it fails and PDF size is 0B. (ONLY IN ANDROID)



What am I doing wrong?



PD: This is the example code: https://ourcodeworld.com/articles/read/230/how-to-save-a-pdf-from-a-base64-string-on-the-device-with-cordova



ADDED:



Also I tried the solution explained in this post: https://stackoverflow.com/a/49137874/8228843



In those cases, in android, only in android, the file.writeFile function did nothing. The promise did not work and the then () code was not executed, nor was it the catch () code. It is important to note that there is no error either in the console or in the logcat.



Code of solution in the post explained:



saveAndOpenPdf(pdf: string, filename: string) 
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), replace: true)
.then(() =>
this.loading.dismiss();
this.opener.open(writeDirectory + filename, 'application/pdf')
.catch(() =>
console.log('Error opening pdf file');
this.loading.dismiss();
);
)
.catch(() =>
console.error('Error writing pdf file');
this.loading.dismiss();
);


convertBaseb64ToBlob(b64Data, contentType): Blob









share|improve this question
























  • Did you try to increase your slice size?

    – Jay Ordway
    Mar 7 at 4:03











  • Yes but also the same problem. It seems a corrupt document.

    – nfont
    Mar 7 at 9:45















1















I want to download a PDF file in cordova app. I recive a base64 string that is that PDF.



I tried the next code using cordova-plugin-file and cordova-plugin-file-opener2:



openPDF(invoice) 
this.saveAndOpenPdf(invoice['base64'], 'invoice.pdf');


saveAndOpenPdf(pdf: string, filename: string)
const load = this.loadingCtrl.create(
content: this.translate.instant("app.loading"),
dismissOnPageChange: true
);
load.present();
var writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalRootDirectory;
console.log(writeDirectory);
var self = this;
this.checkPermissions()
.then(() =>
var res = this.b64toBlob(pdf, 'aplication/pdf', 512);
console.log(res);
( < any > window).resolveLocalFileSystemURL(writeDirectory, function(dir)
console.log("Access to the directory granted succesfully");
dir.getFile(filename,
create: true
, function(file)
console.log("File created succesfully.");
file.createWriter(function(fileWriter)
console.log("Writing content to file");
fileWriter.write(res);
self.opener.open(writeDirectory + filename, 'application/pdf').then(() =>
load.dismiss();
)
.catch((err) =>
console.error(err);
console.log('Error opening pdf file');
load.dismiss();
);

, function()
load.dismiss();
alert('Unable to save file in path ' + writeDirectory);
);
);
);
).catch((error) =>
load.dismiss();
console.log(error);
);


b64toBlob(b64Data, contentType, 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;



It seems that works well but when the opener try to open the PDF it fails and PDF size is 0B. (ONLY IN ANDROID)



What am I doing wrong?



PD: This is the example code: https://ourcodeworld.com/articles/read/230/how-to-save-a-pdf-from-a-base64-string-on-the-device-with-cordova



ADDED:



Also I tried the solution explained in this post: https://stackoverflow.com/a/49137874/8228843



In those cases, in android, only in android, the file.writeFile function did nothing. The promise did not work and the then () code was not executed, nor was it the catch () code. It is important to note that there is no error either in the console or in the logcat.



Code of solution in the post explained:



saveAndOpenPdf(pdf: string, filename: string) 
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), replace: true)
.then(() =>
this.loading.dismiss();
this.opener.open(writeDirectory + filename, 'application/pdf')
.catch(() =>
console.log('Error opening pdf file');
this.loading.dismiss();
);
)
.catch(() =>
console.error('Error writing pdf file');
this.loading.dismiss();
);


convertBaseb64ToBlob(b64Data, contentType): Blob









share|improve this question
























  • Did you try to increase your slice size?

    – Jay Ordway
    Mar 7 at 4:03











  • Yes but also the same problem. It seems a corrupt document.

    – nfont
    Mar 7 at 9:45













1












1








1








I want to download a PDF file in cordova app. I recive a base64 string that is that PDF.



I tried the next code using cordova-plugin-file and cordova-plugin-file-opener2:



openPDF(invoice) 
this.saveAndOpenPdf(invoice['base64'], 'invoice.pdf');


saveAndOpenPdf(pdf: string, filename: string)
const load = this.loadingCtrl.create(
content: this.translate.instant("app.loading"),
dismissOnPageChange: true
);
load.present();
var writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalRootDirectory;
console.log(writeDirectory);
var self = this;
this.checkPermissions()
.then(() =>
var res = this.b64toBlob(pdf, 'aplication/pdf', 512);
console.log(res);
( < any > window).resolveLocalFileSystemURL(writeDirectory, function(dir)
console.log("Access to the directory granted succesfully");
dir.getFile(filename,
create: true
, function(file)
console.log("File created succesfully.");
file.createWriter(function(fileWriter)
console.log("Writing content to file");
fileWriter.write(res);
self.opener.open(writeDirectory + filename, 'application/pdf').then(() =>
load.dismiss();
)
.catch((err) =>
console.error(err);
console.log('Error opening pdf file');
load.dismiss();
);

, function()
load.dismiss();
alert('Unable to save file in path ' + writeDirectory);
);
);
);
).catch((error) =>
load.dismiss();
console.log(error);
);


b64toBlob(b64Data, contentType, 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;



It seems that works well but when the opener try to open the PDF it fails and PDF size is 0B. (ONLY IN ANDROID)



What am I doing wrong?



PD: This is the example code: https://ourcodeworld.com/articles/read/230/how-to-save-a-pdf-from-a-base64-string-on-the-device-with-cordova



ADDED:



Also I tried the solution explained in this post: https://stackoverflow.com/a/49137874/8228843



In those cases, in android, only in android, the file.writeFile function did nothing. The promise did not work and the then () code was not executed, nor was it the catch () code. It is important to note that there is no error either in the console or in the logcat.



Code of solution in the post explained:



saveAndOpenPdf(pdf: string, filename: string) 
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), replace: true)
.then(() =>
this.loading.dismiss();
this.opener.open(writeDirectory + filename, 'application/pdf')
.catch(() =>
console.log('Error opening pdf file');
this.loading.dismiss();
);
)
.catch(() =>
console.error('Error writing pdf file');
this.loading.dismiss();
);


convertBaseb64ToBlob(b64Data, contentType): Blob









share|improve this question
















I want to download a PDF file in cordova app. I recive a base64 string that is that PDF.



I tried the next code using cordova-plugin-file and cordova-plugin-file-opener2:



openPDF(invoice) 
this.saveAndOpenPdf(invoice['base64'], 'invoice.pdf');


saveAndOpenPdf(pdf: string, filename: string)
const load = this.loadingCtrl.create(
content: this.translate.instant("app.loading"),
dismissOnPageChange: true
);
load.present();
var writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalRootDirectory;
console.log(writeDirectory);
var self = this;
this.checkPermissions()
.then(() =>
var res = this.b64toBlob(pdf, 'aplication/pdf', 512);
console.log(res);
( < any > window).resolveLocalFileSystemURL(writeDirectory, function(dir)
console.log("Access to the directory granted succesfully");
dir.getFile(filename,
create: true
, function(file)
console.log("File created succesfully.");
file.createWriter(function(fileWriter)
console.log("Writing content to file");
fileWriter.write(res);
self.opener.open(writeDirectory + filename, 'application/pdf').then(() =>
load.dismiss();
)
.catch((err) =>
console.error(err);
console.log('Error opening pdf file');
load.dismiss();
);

, function()
load.dismiss();
alert('Unable to save file in path ' + writeDirectory);
);
);
);
).catch((error) =>
load.dismiss();
console.log(error);
);


b64toBlob(b64Data, contentType, 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;



It seems that works well but when the opener try to open the PDF it fails and PDF size is 0B. (ONLY IN ANDROID)



What am I doing wrong?



PD: This is the example code: https://ourcodeworld.com/articles/read/230/how-to-save-a-pdf-from-a-base64-string-on-the-device-with-cordova



ADDED:



Also I tried the solution explained in this post: https://stackoverflow.com/a/49137874/8228843



In those cases, in android, only in android, the file.writeFile function did nothing. The promise did not work and the then () code was not executed, nor was it the catch () code. It is important to note that there is no error either in the console or in the logcat.



Code of solution in the post explained:



saveAndOpenPdf(pdf: string, filename: string) 
const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory;
this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), replace: true)
.then(() =>
this.loading.dismiss();
this.opener.open(writeDirectory + filename, 'application/pdf')
.catch(() =>
console.log('Error opening pdf file');
this.loading.dismiss();
);
)
.catch(() =>
console.error('Error writing pdf file');
this.loading.dismiss();
);


convertBaseb64ToBlob(b64Data, contentType): Blob






cordova ionic-framework ionic2 base64 cordova-plugin-file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 9:49







nfont

















asked Mar 6 at 23:31









nfontnfont

4016




4016












  • Did you try to increase your slice size?

    – Jay Ordway
    Mar 7 at 4:03











  • Yes but also the same problem. It seems a corrupt document.

    – nfont
    Mar 7 at 9:45

















  • Did you try to increase your slice size?

    – Jay Ordway
    Mar 7 at 4:03











  • Yes but also the same problem. It seems a corrupt document.

    – nfont
    Mar 7 at 9:45
















Did you try to increase your slice size?

– Jay Ordway
Mar 7 at 4:03





Did you try to increase your slice size?

– Jay Ordway
Mar 7 at 4:03













Yes but also the same problem. It seems a corrupt document.

– nfont
Mar 7 at 9:45





Yes but also the same problem. It seems a corrupt document.

– nfont
Mar 7 at 9:45












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033834%2fbase64-to-pdf-cordova-ionic-v2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033834%2fbase64-to-pdf-cordova-ionic-v2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко