wekas
March 8, 2019, 11:09pm
1
I can generate a pdf (using pdfmake), save and open it on an android emulator fine with Cordova. There are a lot of examples of how to do this.
What I can’t seem to do is do this using Capacitor.
Any examples or suggestions would be massively appreciated
See my attempt here:
I am generating a pdf using pdfMake and writing it to an android device so that I can open it.
I am using Capacitor and the file system command does not appear to take a blob (this worked in cordova) so I am sending it a base64.
It seems to write ok but when I open the pdf it says the format is invalid.
Note: The cordova file plugin does not work with Capacitor (Cordova plugins not working with Capacitor ) so it seems I am stuck with Capacitor file system commands.
Can I write to blob in capa…
1 Like
Hi @wekas
I am stuck on the same question from a couple of days… Have you solved this issue?
If yes, can I ask you how or (even better) to share a code snippet?
wekas
June 18, 2019, 8:25pm
3
Hi @filippo-ots ,
Yes, The code is in the link above. I am using it on a project at the moment so I will copy in here again.
Here is the link:
If you are using the below (and it is not just a placeholder) you are just sending text as data. Not a pdf.
If you don’t specify an encoding type it assumes it is a base64 string.
You are sending raw text. Not a base64 string. Are you using a pdf generator like pdfMake?
See my code below:
downloadPdf(pdfBase64: string) {
const { Filesystem } = Plugins;
if (this.plt.is('cordova')) {
// Save the PDF to the device
const fileName = 'defectreport.pdf';
try {
…
downloadPdf(pdfBase64: string) {
const { Filesystem } = Plugins;
if (this.plt.is('cordova')) {
// Save the PDF to the device
const fileName = 'timesheet.pdf';
try {
Filesystem.writeFile({
path: fileName,
data: pdfBase64,
directory: FilesystemDirectory.Documents
// encoding: FilesystemEncoding.UTF8
}).then((writeFileResult) => {
Filesystem.getUri({
directory: FilesystemDirectory.Documents,
path: fileName
}).then((getUriResult) => {
const path = getUriResult.uri;
this.fileOpener.open(path, 'application/pdf')
.then(() => console.log('File is opened'))
.catch(error => console.log('Error openening file', error));
}, (error) => {
console.log(error);
});
});
} catch (error) {
console.error('Unable to write file', error);
}
} else {
// On a browser simply use download
this.pdfObj.download();
}
}
4 Likes