steve-jansen / cordova-ios-emailcomposer

A Cordova plugin for creating e-mail messages on iOS devices
MIT License
14 stars 4 forks source link

Can we attach .pdf similar to .jpg file #6

Open YatinShevade opened 10 years ago

YatinShevade commented 10 years ago

I am using cordova 3.0 and using file plugin i have created pdf file and have a path as './persistent/readme.pdf' on iPad. I wanted to attach the file to email. Is it possible?

steve-jansen commented 10 years ago

Hello @YatinShevade,

Yes this is possible. From the example in the README, you can pass a file path to the attachment as follows:

var emailComposer = cordova.require('emailcomposer.EmailComposer');
emailComposer.show({ 
  to: 'to@example.com',
  cc: 'cc@example.com',
  bcc: 'bcc@example.com',
  subject: 'Example email message',
  body: '<h1>Hello, world!</h1>',
  isHtml: true,
  attachments: [
    // attach a file using a hypothetical file path
    { filePath:'./persistent/readme.pdf' }
  ],
  onSuccess: function (winParam) { 
    window.alert('EmailComposer onSuccess - return code ' + winParam.toString());
  },
  onError: function (error) {
    window.alert('EmailComposer onError - ' + error.toString());
  }
});

You may need to use the cordova file api's FileEntry#fullPath property to pass the full physical path to your attachment, as ./ implies the current working directory, which should be the app's sandbox home directory. But there could be a difference from what cordova's file API use for ./ and what the plugins for as ./.

Let me know how this goes so I can update the documentation as needed.

Cheers, Steve

YatinShevade commented 10 years ago

Hi Steve,

I tried and specified the file path using FileEntry from file plugin i receive file path as "cdvfile://localhost/persistent/readme.pdf" and specify the attachment code as below

attachments: [ // attach a file using a hypothetical file path { filePath:'./persistent/readme.pdf' } ],

But, i am not able to see file getting attached to email. I am using iPad with version OS 6. Is there any version issue of OS 6 and OS 7 file system.

My code for your reference: function GeneratePDF(){

                                    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
                               }
                               function gotFS(fileSystem) {
                               fileSystem.root.getFile("readme.pdf", {create: true, exclusive: false}, gotFileEntry, fail);

} function gotFileEntry(fileEntry) { pdfFilePath = fileEntry.toURL(); if (userOSver.charAt(0) >= 7){ pdfFilePath = pdfFilePath.replace('cdvfile://', './'); } else if (userOSver.charAt(0) == 6){ } //alert(pdfFilePath); fileEntry.createWriter(gotFileWriter, fail); } function gotFileWriter(writer) { writer.onwriteend = function(evt) { //alert("contents of file now 'some sample text'"); writer.truncate(11); writer.onwriteend = function(evt) { //alert("contents of file now 'some sample'"); writer.seek(4); writer.write("different text"); writer.onwriteend = function(evt){ //alert("contents of file now 'some different text'"); } ShowEmailComposer();}

my ShowEmailComposer(); calls the email composer with attchment.

please provide your valuable solution.

steve-jansen commented 10 years ago

Hi @YatinShevade

According to Cordova CB-3413, you need to simply use fileEntry.fullPath for the full physical path to the file on the device. You are using toUrl(), which serves a very different purpose from the fullPath property.

Let me know how this goes.

Cheers, Steve

YatinShevade commented 10 years ago

Hi Steeve,

Thanks for your quick reply. I used fileEntry.fullPath insteat of toURL(). I get the path as "./readme.pdf" My attchment code is as below attachments: [ { mimeType: 'image/png', encoding: 'Base64', data: base64Data, name: 'hseq.png' }, { filePath: imagePath }, { filePath: pdfFilePath } ],

is it correct? I am attchining image from camera roll and also from capture form camera. "imagePath " and hseq.png refere to image form camera roll and "pdfFilePath" i am specifying as fileEntry.fullPath.

I tried with above solution process but dint got the pdf attchment.

YatinShevade commented 10 years ago

Hi Steve,

Do you have any solutions. Where i am getting wrong?

MBing commented 10 years ago

Hi Steve,

Thanks for your great plugin!! I am having the same problem as @YatinShevade . With entry.fullPath I get '/somefile.pdf' and with entry.toURL() I get 'cdvfile://localhost/persistent/somefile.pdf'

There is no '.' in front of my fullPath string. There's also no attachment in my email. I've tried to hardcode, but it doesn't work either. Do you know what is a proper url so it gets the file as an attachment?

Following didn't work: 'cdvfile://localhost/persistent/somefile.pdf' => ( entry.toURL() ) './persistent/somefile.pdf' => ( hardcoded ) '/somefile.pdf' => ( entry.fullPath ) './somefile.pdf' => ( hardcoded )

Thanks!

steve-jansen commented 10 years ago

Hello,

Thanks for helping to report extra details. I suspect that something has changed in the Cordova file API. I am traveling right now and won't have access to my MacBook for a couple more days.

I will repro this soonest and respond with either a workaround or timing on a fix.

Cheers, Steve

YatinShevade commented 10 years ago

Hi Steve,

Good morning,

You are correct there is change in Cordova file plugin. Please refer the link below. https://github.com/apache/cordova-plugin-file/blob/dev/doc/index.md#ios-persistent-storage-location

I attach my file using this path /var/mobile/Applications/<application UUID>/Documents/path/to/file

Where i found the Application UUID from document.baseURL and extract the UUID using javascript split function. now i am able to attach pdf but still i am not able to write into it. Working on writing the pdf file.

On Tue, Mar 18, 2014 at 7:13 AM, Steve Jansen notifications@github.comwrote:

Hello,

Thanks for helping to report extra details. I suspect that something has changed in the Cordova file API. I am traveling right now and won't have access to my MacBook for a couple more days.

I will repro this soonest and respond with either a workaround or timing on a fix.

Cheers, Steve

Reply to this email directly or view it on GitHubhttps://github.com/steve-jansen/cordova-ios-emailcomposer/issues/6#issuecomment-37891849 .

Thanks & Regards,

Yatin Shevade *| _yatin_shevade@tudip.com yatinshevade@tudip.com* | Skype: yatinshevade www.tudip.blogspot.com http://www.tudip.blogspot.com/ * | _www.facebook.com/tudiptechnologies http://www.facebook.com/tudiptechnologies *+91 20 4674 0881 | +1 408 216 8162_

http://www.tudip.com/ | http://www.steersimple.com/

MBing commented 10 years ago

Good morning, Sounds like a lot got messed up with the change of the Cordova File API, noticed when doing a google search.. It would be nice to have a choice between a relative and absolute path of adding an attachment. @YatinShevade maybe this helps: http://stackoverflow.com/questions/16858954/how-to-properly-use-jspdf-library home site: http://parall.ax/products/jspdf Haven't used this before but I know a lot of people who have and it seems to be the more popular lib around to create pdf files.

Enjoy your travel @steve-jansen and thanks for the great plugin and response! Cheers

johannish commented 10 years ago

Here's why this broke:

Previous (pre-1.0.0) versions of the plugin stored the device-absolute-file-location in the fullPath property of Entry objects. ... With v1.0.0, the fullPath attribute is the path to the file, relative to the root of the HTML filesystem.

https://github.com/polyvi/cordova-plugin-file/blob/master/doc/index.md#upgrading-notes

I'm now investigating what the proper fix is.

johannish commented 10 years ago

Here's the situation as I understand it. Because the File plugin no longer exposes a way to get the "device-absolute-file-location" (e.g., /var/mobile/Applications/.../myfile), the two options for this plugin are:

  1. implement a way to get file paths relative to the bundle path, and let users guess at where the CDVFile object might have put them, or
  2. to call the File plugin directly, and ask it to read the data for you.

Option (1) is how the other popular cordova email plugin does it: https://github.com/katzer/cordova-plugin-email-composer/blob/f4fcee88c47c7ac642cceb27d3d8b31edd26a8f6/src/ios/APPEmailComposer.m#L370-L377.

Option (2) is how the apache file-transfer-plugin does it: https://github.com/apache/cordova-plugin-file-transfer/blob/05786ec2ecf24a9244d43a88dce4c645eb0059b4/src/ios/CDVFileTransfer.m#L284

In my mind, both solutions suck for different reasons. Option 1 requires you to guess whether the File plugin is putting things in ../Documents or ../Library, relative to the bundle path. Option 2 works, but requires tight-coupling to the File plugin.

In my opinion, the better solution would be for the File plugin to again expose a way to get device absolute paths, even though it's non-standard from a W3C perspective. That way the Javascript land could get the full paths, and other plugins, such as this one, wouldn't have to care. They'd just use the apple-provided APIs for reading files from the disk.

Both of those plugins still allow one to use the device absolute path, as well, but that is only helpful if there is a way to discover the device absolute path from Javascript.

johannish commented 10 years ago

I've now logged an Improvement ticket at https://issues.apache.org/jira/browse/CB-6439

Jiiieeef commented 10 years ago

Any news about it? I am facing the same problem :(

johannish commented 10 years ago

For the time being, I'm using katzer/cordova-plugin-email-composer plugin, and attaching using the relative path:

attachments: [
    'relative://../Documents/myfile.txt'
]

But this is brittle because if I ever change the File plugin config to <preference name="iosPersistentFileLocation" value="library" /> which they now recommend, I'll have to remember to go change the attachment path to 'relative://../Library/myfile.txt'.

Jiiieeef commented 10 years ago

It works !! Thanks for your help ;)

DaveInfinity commented 10 years ago

can we attach pdf file in wp8

YatinShevade commented 10 years ago

Hi Kunal,

No, as per my update we can't attach .pdf in wp8 as class "Email composer" provided by Microsoft does not have attribute "attachment" and also windows don't support HTML formatted text.

On Thu, Sep 4, 2014 at 12:14 PM, Kunal notifications@github.com wrote:

can we attach pdf file in wp8

— Reply to this email directly or view it on GitHub https://github.com/steve-jansen/cordova-ios-emailcomposer/issues/6#issuecomment-54416119 .

Thanks & Regards,

Yatin Shevade *| _yatin_shevade@tudip.com yatinshevade@tudip.com* | Skype: yatinshevade www.tudip.blogspot.com http://www.tudip.blogspot.com/ * | _www.facebook.com/tudiptechnologies http://www.facebook.com/tudiptechnologies *+91 20 4674 0881 | +1 408 216 8162_

http://www.tudip.com/ | http://www.steersimple.com/

DaveInfinity commented 10 years ago

Thanks for your comment Yatin.

DaveInfinity commented 10 years ago

Please go through the following code which i used to create text or pdf files in wp8 (i am using device nokia lumia 925), in log it showing file created but i am not able to locate the created file in device physically.

function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("Test1.pdf", { create: true, exclusive: false }, gotFileEntry, fail);
        fsName = fileSystem.name;
        console.log("fsName: " + fsName);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
        var fname = fileEntry.name;
        fpath = fileEntry.fullPath;
        NativePath = fileEntry.toNativeURL();
        console.log("fname: " + fname + "\nfpath: " + fpath);
        console.log("NativePath: " + NativePath);
    }

    function gotFileWriter(writer) {
        writer.write("some sample text goes here...");
    }