将Pdf附加到离子电子邮件编辑器中

时间:2016-12-04 08:31:49

标签: javascript angularjs cordova ionic-framework pdfmake

我正在尝试将blob pdf文件附加到电子邮件编辑器。但它不适合我。

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");
                });
            });
        });
    }

此代码有效且警告pdf已创建。

pdfGenerator.createPdf(reportbody)
      .then(function(pdf){
        $ionicLoading.hide();
        var blob = new Blob([pdf], {type: 'application/pdf'});
        $scope.pdfUrl = URL.createObjectURL(blob);
            var email = {
           to: 'max@mustermann.de',
           cc: 'erika@mustermann.de',
           bcc: ['john@doe.com', 'jane@doe.com'],
           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);
      });

当我控制cordova.file.dataDirectory时,它给我cdvfile://路径不是本机路径。那么如何将文件附加到邮件中。

1 个答案:

答案 0 :(得分:1)

Ya我找到了解决问题的方法。我在这里提到了代码,希望这会对某人有所帮助。

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);
        });
    });
}

在控制器中,

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: 'max@mustermann.de',
           cc: 'erika@mustermann.de',
           bcc: ['john@doe.com', 'jane@doe.com'],
           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);
});