title: jsPDF, html2canvas, PHPMailer, again...! Attach PDF to mail
body: <p>I had read a lot of answer about "how to attach PDF via PHPMailer from html2canva or jsPDF", but everything I tried didn't work.</p>
  
  <p>For now, I have an HTML page with some input. When the customer fill all of them, he clicks on Send button.</p>
  
  <ul>
  <li>html2canva take a screenshot</li>
  <li>jsPDF cut it into as page as it needs</li>
  <li>jQuery send the PDF to a PHP Script</li>
  <li>PHPMailer takes it and send via mail.</li>
  </ul>
  
  <p>Everything work, except the attachment of the PDF File.</p>
  
  <p>So, I need your help ;)</p>
  
  <p>This is what I have :</p>
  
  <p>JS Script :</p>
  
  <pre><code>    html2canvas($("#Div"), {
      onrendered: function(canvas) {
  
          //=== Take screenshot  
          var imgData = canvas.toDataURL('image/jpeg');   
  
          //=== PDFize it
          var doc = new jsPDF("p", "px");
          var options = {
               pagesplit: true
          };
  
          //=== Split it
          var imgWidth = 210; 
          var pageHeight = 295;  
          var imgHeight = canvas.height * imgWidth / canvas.width;
          var heightLeft = imgHeight;
  
          var doc = new jsPDF('p', 'mm');
          var position = 0;
  
          doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
          heightLeft -= pageHeight;
  
          while (heightLeft &gt;= 0) {
            position = heightLeft - imgHeight;
            doc.addPage();
            doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;
          }
  
          //=== Send it       
          $.post("mail.php", 
          {
              data: doc;
          }, function () {}).done(function() {/*SOME CODE*/});    
  
          //=== Just to test the PDF      
          doc.save( 'file.pdf');
      }
  });
  </code></pre>
  
  <p>And my PHP Script :</p>
  
  <pre><code>&lt;?php
  
  if(!empty($_POST['data']))
  {
  
      require 'lib/PHPMailer-master/PHPMailerAutoload.php';
      $mail = new PHPMailer;
  
      $mail-&gt;isSMTP();
      $mail-&gt;Host = '*';
      $mail-&gt;SMTPAuth = *; 
      $mail-&gt;Username = '*';
      $mail-&gt;Password = '*';          
      $mail-&gt;SMTPSecure = '*';                           
      $mail-&gt;Port = *;                                   
  
      $mail-&gt;setFrom('*', '*');
      $mail-&gt;addAddress('*', '*');   
  
      //=== Tested this
      $originalbase = $base;
      //=== or this
      $base = explode('data:application/pdf;base64,', $_POST['data']);
      $mail-&gt;addStringAttachment($originalbase, "name.pdf", "base64", "application/pdf");
  
      $mail-&gt;isHTML(true);                              
  
      $mail-&gt;Subject = '*';
      $mail-&gt;Body    = '*';
  
      if(!$mail-&gt;send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
      } else {
          echo 'Message has been sent';
      }
  
  } else echo "No Data Found"; 
  
  ?&gt;
  </code></pre>
  
  <p>I had tested with base64 decode, without, with the mime type, without...</p>
  
  <p>Edit 1: 13.10.2016
  Seems to be good with</p>
  
  <pre><code>var PDF = doc.output('datauri');
  
  $base = explode('data:application/pdf;base64,', $_POST['data']);
  $base = base64_decode($base[1]);
  $mail-&gt;addStringAttachment($base, 'pdfName.pdf');
  </code></pre>
  
  <p>Edit 2: 13.10.2016
  Replace </p>
  
  <pre><code>var PDF = doc.output('datauri');
  </code></pre>
  
  <p>by</p>
  
  <pre><code>var PDF = doc.output('datauristring');
  </code></pre>
  
  <p>in order to avoid the redirection to the created PDF</p>