Reputation: 139
I'm using the following plugin to send an email from a iphone phongeap app (using ionic): https://github.com/katzer/cordova-plugin-email-composer
Everything works fine except when I try to attach a pdf file saved to the device: When the email interface opens up, I can see the attached pdf (called test.pdf), but when the email arrives to the recipient, there's no PDF. Instead there's a attached txt file but without any content.
If I go window.replace(path to pdf) the PDF opens up, so I'm sure the PDF gets saved and that the path is correct. After reading this, https://github.com/katzer/cordova-plugin-email-composer/issues/33, I have also tried to take away the path start (file://) and to replaace it with (relative://) but to no use. Would really appreciate any input on this: Here's the relevant code:
var doc = new jsPDF();
doc.text(20, 20, 'HELLO!');
doc.setFont("courier");
doc.setFontType("normal");
doc.text(20, 30, 'This is a PDF document generated using JSPDF.');
doc.text(20, 50, 'YES, Inside of PhoneGap!');
var pdfOutput = doc.output();
console.log( pdfOutput );
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile("test.pdf", {create: true}, function(entry) {
var fileEntry = entry;
console.log(entry);
$scope.filepath = fileEntry.toURL();
$scope.filepath = $scope.filepath.replace('file\:\/\/', 'relative://');
$scope.emailer($scope.filepath);
// window.location.href = $scope.filepath;
entry.createWriter(function(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
console.log("writing to file");
writer.write( pdfOutput );
}, function(error) {
console.log(error);
});
}, function(error){
console.log(error);
});
},
function(event){
console.log( evt.target.error.code );
});
$scope.emailer = function(file) {
alert(file);
window.plugin.email.open({
to: ['[email protected]'],
subject: 'Congratulations',
body: '<h1>Happy Birthday!!!</h1>',
attachments: [file]
});
Upvotes: 2
Views: 1078
Reputation: 199
We are using the exact same plugin and have it working, the URL we use looks like this
/var/mobile/Applications/998AEF02-5D26-4064-BC7D-A94A218609C5/Documents/yourpdffilenamehere.pdf
The URL is returned to our app from a pdf-plugin we use.
Upvotes: 1