2015-11-30 93 views
3

我已經創建了一個功能,使用html2canvas和jsPDF將當前網頁的屏幕截圖保存爲PDF。以下是我的代碼:html2canvas和jsPDF:發送生成的pdf作爲電子郵件附件

<script> 
function downloadpdf(){ 
    html2canvas(document.body, 
     { 
      onrendered: function(canvas){ 
       var imgData = canvas.toDataURL("image/jpeg"); 
       var a = document.createElement('a'); 
       var doc = new jsPDF('p','mm'); 
       doc.addImage(imgData, 'JPEG', 15, 40, 180, 160); 
       doc.save($.now()+'.pdf'); 

     } 
    }); 
} 
</script> 

通過使用上面的代碼,我可以下載文件並將其保存到本地。 但我想直接發送生成的PDF通過電子郵件使用PHP腳本。

下面就是它將作爲電子郵件發送在PHP腳本張貼圖像的代碼:

var imgData = canvas.toDataURL("image/jpeg"); 
       $.post("sendimage.php", 
       { 
        data: imgData 
       }, function (response,status) { 
        console.log(response); 
       }); 

但如何發佈生成的PDF中data參數?

請您爲此推薦任何解決方案。

回答

3

請檢查PHP碼 -

function MailWithAttachment($to, $subject, $message, $senderMail, $senderName, $files){ 

    $from = $senderName." <".$senderMail.">"; 
    $headers = "From: $from"; 

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

    // preparing attachments 
    if(count($files) > 0){ 
     for($i=0;$i<count($files);$i++){ 
      if(is_file($files[$i])){ 
       $message .= "--{$mime_boundary}\n"; 
       $fp = @fopen($files[$i],"rb"); 
       $data = @fread($fp,filesize($files[$i])); 
       @fclose($fp); 
       $data = chunk_split(base64_encode($data)); 
       $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
       "Content-Description: ".basename($files[$i])."\n" . 
       "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
       "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      } 
     } 
    } 
    $message .= "--{$mime_boundary}--"; 
    $returnpath = "-f" . $senderMail; 

    //send email 
    $mail = @mail($to, $subject, $message, $headers, $returnpath); 

    //function return true, if email sent, otherwise return fasle 
    if($mail){ return TRUE; } else { return FALSE; } 
} 


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

    //email variables 
    $to = '[email protected]'; 
    $from = '[email protected]'; 
    $from_name = 'PDF FIle'; 

    //attachment files path array 
    $file = base64_decode($_POST['data']); 

    $subject = 'PHP Email with attachment'; 
    $html_content = '<h1>PHP Email with attachment</h1>'; 

    //call MailWithAttachment() function and pass the required arguments 
    $send_email = MailWithAttachment($to,$subject,$html_content,$from,$from_name,$file); 

    //print message after email sent 
    echo $send_email?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>"; 

} else { 
    echo "No Data Found"; 
} 

我希望它會爲你工作:)

+1

謝謝謝謝謝謝! :)它爲我工作perrrfect。但我需要修改我的功能: 'var doc = new jsPDF('p','mm'); doc.addImage(imgData,'JPEG',15,40,180,160); var pdf = btoa(doc.output()); (「響應」); };;。 –

相關問題