I am facing an issue with attaching a blob PDF file to an email composer. Despite my efforts, it does not seem to work as expected.
function generatePdf(reportData){
return $q(function(resolve, reject) {
var docDefinition = createDocumentDefinition(reportData);
var pdfDoc = pdfMake.createPdf(docDefinition)
.getBuffer(function(buffer){
var utf8Array = new Uint8Array(buffer); // Convert to UTF-8...
binaryArray = utf8Array.buffer; // Convert to Binary...
$cordovaFile.writeFile(cordova.file.dataDirectory, "file.pdf", binaryArray, true)
.then(function (success) {
alert('PDF created');
console.log("PDF created");
}, function (error) {
console.log("Error");
});
});
});
}
This piece of code functions correctly and triggers an alert when the PDF is successfully created.
pdfGenerator.generatePdf(reportbody)
.then(function(pdf){
$ionicLoading.hide();
var fileBlob = new Blob([pdf], {type: 'application/pdf'});
$scope.pdfUrl = URL.createObjectURL(fileBlob);
var email = {
to: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="117c7069517c64626574637c707f7f3f7574">[email protected]</a>',
cc: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4e5942404a6b465e585f4e59464a4545054f4e">[email protected]</a>',
bcc: ['<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c060304022c080309420f0301">[email protected]</a>', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86ece7e8e3c6e2e9e3a8e5e9eb">[email protected]</a>'],
attachments: [$scope.pdfUrl],
subject: 'Cordova Icons',
body: 'How are you? Nice greetings from Leipzig',
isHtml: true
};
$cordovaEmailComposer.open(email).then(null, function () {
// user cancelled email
});
},function(error){
console.log(error);
});
When I check the cordova.file.dataDirectory in the console, it returns a cdvfile:// path instead of the native path. Therefore, I am unsure how to properly attach the file to an email.