Search code examples
cordovapdfcordova-pluginsjspdfngcordova

ngCordova attaching pdf as base64 string to email using emailComposer plugin


I'm using jspdf to generate a pdf as a base64 string, all seems to work OK, the format returned is:

data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovTWVkaWFCb3ggWzAgMCA1OTUuMjggODQxLjg5XQovQ29udGVudHMgNCAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwvTGVuZ3RoIDEwPj4Kc3RyZWFtCjAuNTcgdwowIEcKZW5kc3RyZWFtCmVuZG9iagoxIDAgb2JqCjw8L1R5cGUgL1BhZ2VzCi9LaWRzIFszIDAgUiBdCi9Db3VudCAxCj4

The ngCorova docs for email composer state that attachments can be in the following format:

attachments: [
  'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
]

Where attachments is an object of the email with properties for 'subject', 'CC', etc.

When my app opens the email client on the device a message pops up saying 'Unable to attach file', everything else works OK (Subject, CC, BCC, etc).

I'm wondering if the format of the base64 pdf is wrong, OR i'm failing to tell the email client the attachment is inline? (not a file), can't find anywhere in the docs that states this though.


Solution

  • The format of the string has to be as shown in the example

    attachments: [
      'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
    ]
    

    for pdf something like 'base64:filename.pdf//' + base64 without the 'data:application/pdf;base64,'