4

I am converting an HTML table into PDF using jsPDF. I want to get the PDF and attach it to email in mailto on click of a button.

Can someone please help me with this? Below is the code I am using to get the PDF using jsPDF. I used the output() function of jsPDF, which is base64 string of the PDF. According to the syntax of mailto, the attachment should be a path to the file being attached. So, can I some how store the PDF temporarily and use the path to email and then delete the file from that temporary location. I do not know how to do this. Can someone help me with this.

function funcEmail() {
  pdf = new jsPDF('p', 'pt', 'letter');
  source = $('#print_preview')[0];
  specialElementHandlers = {
    // element with id of "bypass" - jQuery style selector
    '#bypassme': function(element, renderer) {
      // true = "handled elsewhere, bypass text extraction"
      return true
    }
  };
  margins = {
    top: 80,
    bottom: 60,
    left: 40,
    width: 522
  };
  // all coords and widths are in jsPDF instance's declared units
  // 'inches' in this case
  pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
      'width': margins.width, // max width of content on PDF
      'elementHandlers': specialElementHandlers
    },
    function(dispose) {
      // dispose: object with X, Y of the last line add to the PDF 
      //          this allow the insertion of new lines after html
      //pdf.save('Test.pdf');
      emailPdf = pdf.output('datauristring');
    }, margins);

  //console.log(dataurl);
  //console.log(emailPdf);
  location.href = "mailto:mail@example.org?subject=Test&body=Test&attachments='+emailPdf+'";
}

This is what I tried so far. Here, print_preview is the id to the div containing the table to be converted to PDF.

user7637745
  • 965
  • 2
  • 14
  • 27
ksagar
  • 305
  • 3
  • 6
  • 20

0 Answers0