ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-html2canvas和jsPDF:将生成的pdf作为电子邮件附件发送

2019-11-19 10:29:54  阅读:286  来源: 互联网

标签:jspdf html2canvas ajax php


我创建了一个函数来获取当前网页的屏幕截图,并使用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>

通过使用上面的代码,我可以下载文件并将其保存在本地.
但是我想使用php脚本通过电子邮件直接发送生成的pdf.

以下是在php脚本中发布图像的代码,该图像将作为电子邮件发送:

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

但是如何在数据参数中发布生成的pdf?

请为此提出任何解决方案.

解决方法:

请检查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 = 'to-email@email.com';
    $from = 'from-email@email.com';
    $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";
} 

我希望它对您有用:)

标签:jspdf,html2canvas,ajax,php
来源: https://codeday.me/bug/20191119/2035637.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有