Friends,
I used a working code from a project in my project with same code for creating a custom PDF file using code
createPdf(epayTransID,status,ReceiptDate,TaxPeriod,TotAmount,BankRefNo) {
var docDefinition = {
content: [
{ text: 'Payment Receipt', style: 'header' },
{ text: new Date().toTimeString(), alignment: 'right' },
{ text: 'Transaction ID', style: 'subheader' },
{ text: epayTransID },
{ text: 'Status', style: 'subheader' },
{ text: status },
{ text: 'Receipt Date', style: 'subheader'},
{ text: ReceiptDate },
{ text: 'Tax Period', style: 'subheader'},
{ text: TaxPeriod },
{ text: 'Total Amount', style: 'subheader'},
{ text: TotAmount},
{ text: 'Bank Reference no', style: 'subheader'},
{ text: BankRefNo},
],
styles: {
header: {
fontSize: 18,
bold: true,
},
subheader: {
fontSize: 14,
bold: true,
margin: [0, 15, 0, 0]
},
story: {
italic: true,
alignment: 'center',
width: '50%',
}
}
}
this.pdfObj = pdfMake.createPdf(docDefinition).getBlob(buffer => {
console.log("step 1 : " + this.file.externalRootDirectory);
this.file.resolveDirectoryUrl(this.file.externalRootDirectory)
.then(dirEntry => {
console.log("step 2 : " + JSON.stringify(dirEntry));
this.file.getFile(dirEntry, 'paymentreceipt.pdf', { create: true })
.then(fileEntry => {
console.log("step 3 : " + JSON.stringify(fileEntry));
console.log("step 3.1 : " + JSON.stringify(fileEntry.toURL()));
fileEntry.createWriter(writer => {
console.log("dsd"+JSON.stringify(writer));
console.log("step 3.2 : Enter within create writer");
writer.onwritestart = function() { console.log('onwritestart'); };
writer.onabort = function() { console.log('onabort'); };
writer.write(buffer);
console.log("buffer is after writing: "+JSON.stringify(buffer));
writer.onwrite = () => {
console.log("step 4 : " + fileEntry.toURL());
this.fileOpener.open(fileEntry.toURL(), 'application/pdf')
.then(res => { console.log("step 5 : completed : " + res); })
.catch(err => {
console.log("step 6 : error : " + err);
});
}
writer.write(buffer);
console.log("step 7 : writing : " + JSON.stringify(buffer));
console.log("step 7.1 : "+fileEntry.toURL());
}, function(error){
console.log("Error occurred while writing to file. Error code is: " + error.code+ ":error: "+error);
})
})
.catch(err => {
console.log("step1 catch: "+err);
});
})
.catch(err => {
console.log("step2 catch: "+err);
});
});
}
Here got empty value for object buffer …
Also no PDF file created .
it’s console output of above function is
step 1 : file:///storage/emulated/0/
step 2 : {"isFile":false,"isDirectory":true,"name":"","fullPath":"/","filesystem":"<FileSystem: sdcard>","nativeURL":"file:///storage/emulated/0/"}
step 3 : {"isFile":true,"isDirectory":false,"name":"paymentreceipt.pdf","fullPath":"/paymentreceipt.pdf","filesystem":"<FileSystem: sdcard>","nativeURL":"file:///storage/emulated/0/paymentreceipt.pdf"}
step 3.1 : "file:///storage/emulated/0/paymentreceipt.pdf"
dsd{"fileName":"","length":0,"localURL":"cdvfile://localhost/sdcard/paymentreceipt.pdf","position":0,"readyState":0,"result":null,"error":null,"onwritestart":null,"onprogress":null,"onwrite":null,"onwriteend":null,"onabort":null,"onerror":null}
step 3.2 : Enter within create writer
buffer is after writing: {}
step 7 : writing : {}
step 7.1 : file:///storage/emulated/0/paymentreceipt.pdf
The function not going to Step 4 and Step 5 .
Please advise how this file write operation can done successfully…
Thanks
Anes