Suraz Khanal
Suraz Khanal

Reputation: 222

Attach Pdf to email composer in ionic

i am trying to attach the blob pdf file to email composer . But it is not working for me.

function createPdf(reportData){
         return $q(function(resolve, reject) {
            var dd = createDocumentDefinition(reportData);
            var pdf = pdfMake.createPdf(dd)
            .getBuffer(function(buffer){
                var utf8 = new Uint8Array(buffer); // Convert to UTF-8... 
                binaryArray = utf8.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 code works and it alert pdf created.

pdfGenerator.createPdf(reportbody)
      .then(function(pdf){
        $ionicLoading.hide();
        var blob = new Blob([pdf], {type: 'application/pdf'});
        $scope.pdfUrl = URL.createObjectURL(blob);
            var email = {
           to: '[email protected]',
           cc: '[email protected]',
           bcc: ['[email protected]', '[email protected]'],
           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 console the cordova.file.dataDirectory it gives me cdvfile:// path not the native path. So how can i attach the file to mail.

Upvotes: 0

Views: 1431

Answers (1)

Suraz Khanal
Suraz Khanal

Reputation: 222

Ya I found the solution for my problem. I have mentioned code here , hope this will help someone.

function createPdf(reportData){
    return $q(function(resolve, reject) {
        var dd = createDocumentDefinition(reportData);
        var pdf = pdfMake.createPdf(dd)
        .getBuffer(function(buffer){
            var utf8 = new Uint8Array(buffer); // Convert to UTF-8... 
            binaryArray = utf8.buffer; // Convert to Binary...
            resolve(binaryArray);
        });
    });
}

And in the controller ,

pdfGenerator.createPdf(reportbody)
    .then(function(pdf){
        $ionicLoading.hide();
        $cordovaFile.writeFile(cordova.file.externalApplicationStorageDirectory,'mydoc.pdf',pdf,true).then(function(success){
            console.log("File created");
        })
        var email = {
           to: '[email protected]',
           cc: '[email protected]',
           bcc: ['[email protected]', '[email protected]'],
           attachments: [externalApplicationStorageDirectory+'mydoc.pdf'],
           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);
});

Upvotes: 1

Related Questions