Jason
Jason

Reputation: 851

Cordova - Generate PDF from HTML and Email PDF as attachment?

As the title, I want to be able to generate PDF from current HTML page content and Email that PDF as attachment. Could anyone point me to some plugin and possible sample code on how to do that? thanks!

Upvotes: 1

Views: 3323

Answers (1)

Justin
Justin

Reputation: 889

I was able to do that with jsPDF and the cordova email composer plugin. This has worked for me for creating small PDFs but large ones have some problems (I could be doing something wrong).

Here's a sample of my code:

var doc = new jsPDF('p', 'pt', 'letter');
doc.addHTML(window.document.body, function () {       
  var filename = 'report.pdf';
  var dataUrl = doc.output('dataurlstring');

  // This part is only required because the dataurl formats from the 2 plugins don't match up (see Note at the bottom of my post)
  var base64parts = dataUrl.split(',');
  base64parts[0] = "base64:" + window.escape(filename) + "//";
  var compatibleAttachment = base64parts.join("");

  window.plugin.email.open({
      subject: 'Report',
      body: 'Report is attached',
      attachments: [compatibleAttachment]
  });
});

*Note: There is a bug with sending attachments in the newest version of the cordova email composer plugin (0.8.2), but I was able to get it to work using 0.8.1 with help from this link.

Upvotes: 1

Related Questions