title: Send html2pdf pdf or html2canvas image to email
body: <p>I have two PHP pages. First one is a form where user inputs data, when submitted second page is created with a div that makes a &quot;invoice&quot;. This page has options to save that invoice as PDF or IMG. This page it also sends and email automatically with confirmation.</p>
  <p>Id like to send in this email also PDF or image as attachment.</p>
  <p>html2canvas.js .png:</p>
  <pre><code>function picDiv() { 
  html2canvas($('#printableArea')[0], {
    width: 1200
  }).then(function(canvas) {
    var a = document.createElement('a');
    a.href = canvas.toDataURL(&quot;image/png&quot;);
    a.download = 'nalog.png';
    a.click();
  });
  };
  </code></pre>
  <p>And html2pdf.js to produce pdf:</p>
  <pre><code>function pdfDiv() { 
  var element = document.getElementById('printableArea');
  var opt = { 
      filename:     'nalog.pdf'
  };
  html2pdf().set(opt).from(element).save();
  };
  </code></pre>
  <p>I have read among other things <a href="https://stackoverflow.com/questions/25204621/send-image-to-server-using-file-input-type">Send Image to server using File input type</a>, <a href="https://stackoverflow.com/questions/33994282/html2canvas-and-jspdf-send-generated-pdf-as-email-attachment">html2canvas and jsPDF : send generated pdf as email attachment</a>. Also outside articles like this: <a href="https://www.askingbox.com/tutorial/send-html5-canvas-as-image-to-server" rel="nofollow noreferrer">Send HTML5 Canvas as Image to Server</a> and <a href="https://github.com/eKoopmans/html2pdf.js/issues/181#issuecomment-453028321" rel="nofollow noreferrer">html2pdf github post</a>.
  This last html2pdf github post states following code can be send to email or server but I don't know how to email it. It converts pdf successfully to string.</p>
  <pre><code>    html2pdf().from(element).toPdf().output('datauristring').then(function (pdfAsString) {
  //send via email.
          });
  </code></pre>
  <p>My php email sending is:</p>
  <pre><code>$user = &quot;some email&quot;;
  $usersubject = &quot;Initium - Vaš online radni nalog&quot;;
  $userheaders = &quot;From: some email&quot;;
  $usermessage = &quot;
  
  //some content with php variables and text.
  
  &quot;;    
  mail($user,$usersubject,$usermessage,$userheaders);
  </code></pre>
  <p>I would be happy if someone would explain how to send PDF OR IMAGE via this code I already have.
  Keep in mind this is all on same page and this needs to be done on this same page load.</p>
  <p>I tried numerous things and ways to do it and got lost as this is new territory for me. Any help is appreciated.</p>
  <p><strong>EDIT:</strong></p>
  <p>Full working solution is here: <a href="https://stackoverflow.com/a/61418366/7158959">https://stackoverflow.com/a/61418366/7158959</a></p>