简体   繁体   中英

jsPDF, html2canvas, PHPMailer, again…! Attach PDF to mail

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.

For now, I have an HTML page with some input. When the customer fill all of them, he clicks on Send button.

  • html2canva take a screenshot
  • jsPDF cut it into as page as it needs
  • jQuery send the PDF to a PHP Script
  • PHPMailer takes it and send via mail.

Everything work, except the attachment of the PDF File.

So, I need your help ;)

This is what I have :

JS Script :

    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 >= 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');
    }
});

And my PHP Script :

<?php

if(!empty($_POST['data']))
{

    require 'lib/PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = '*';
    $mail->SMTPAuth = *; 
    $mail->Username = '*';
    $mail->Password = '*';          
    $mail->SMTPSecure = '*';                           
    $mail->Port = *;                                   

    $mail->setFrom('*', '*');
    $mail->addAddress('*', '*');   

    //=== Tested this
    $originalbase = $base;
    //=== or this
    $base = explode('data:application/pdf;base64,', $_POST['data']);
    $mail->addStringAttachment($originalbase, "name.pdf", "base64", "application/pdf");

    $mail->isHTML(true);                              

    $mail->Subject = '*';
    $mail->Body    = '*';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

} else echo "No Data Found"; 

?>

I had tested with base64 decode, without, with the mime type, without...

Edit 1: 13.10.2016 Seems to be good with

var PDF = doc.output('datauri');

$base = explode('data:application/pdf;base64,', $_POST['data']);
$base = base64_decode($base[1]);
$mail->addStringAttachment($base, 'pdfName.pdf');

Edit 2: 13.10.2016 Replace

var PDF = doc.output('datauri');

by

var PDF = doc.output('datauristring');

in order to avoid the redirection to the created PDF

尝试这一行没有其他参数,如果它不起作用,你可以共享一部分乞讨的base64张贴到PHP

$mail->addStringAttachment($pdfString, 'pdfName.pdf');
//=== Send it       
$.post("mail.php", 
{
  data: doc;
}, function () {}).done(function() {/*SOME CODE*/});   

BY 

//=== Send it       
$.post("mail.php", 
{
  data: doc //or doc.output('datauristring')
}, function () {}).done(function() {/*SOME CODE*/});   

Use html2pdf , when the user submit the form , send the whole HTML code to php then use this part of the code to create your pdf

 $htmlCode= $_POST["htmlCodeSentWithJquery"];

 require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
 $html2pdf = new HTML2PDF('P','A4','fr');
 $html2pdf->WriteHTML($htmlCode);
 $html2pdf->Output('pdfFileName.pdf');

More info about the constructor

/**
 * Constructeur
 *
 * @param   string      $sens - landscape or portrait orientation
 * @param   string      $format - format A4, A5, ...
 * @param   string      $langue - language: fr, en, it ... 
 * @param   boolean     $unicode - TRUE means clustering the input text IS unicode (default = true)
 * @param   String      $encoding - charset encoding; Default is UTF-8
 * @param   array       $marges - margins by default, in order (left, top, right, bottom)
 * @return  null
 */
public function __construct($sens = 'P', $format = 'A4', $langue='en', $unicode=true, $encoding='UTF-8', $marges = array(5, 5, 5, 8))

then send the file using your email function

OR

use the html2pdf email function

   $content_PDF = $html2pdf->Output('', true);  
   require_once(dirname(__FILE__).'/pjmail/pjmail.class.php'); 
   $mail = new PJmail(); 
   $mail->setAllFrom('webmaster@my_site.net', "My personal site"); 
   $mail->addrecipient('mail_user@my_site.net'); 
   $mail->addsubject("Example sending PDF"); 
   $mail->text = "This is an example of sending a PDF file"; 
   $mail->addbinattachement("my_document.pdf", $content_PDF); 
   $res = $mail->sendmail(); 

More about the email function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM