传递的pdf文件中的is_file失败

时间:2016-02-03 17:59:25

标签: javascript php

我正在编写一些代码来获取页面,将其转换为pdf,然后通过电子邮件发送pdf。我使用html2canvas和jsPDF来生成pdf。然后我将pdf发送到php脚本来处理它并邮寄它,但这是失败的。我知道pdf生成有效,因为我可以在本地保存pdf。到目前为止,这是我的代码:

的javascript:

$(document).ready(function() {
    $('#download').click(function() {  
    html2canvas($('#wrapper'), {
        onrendered: function(canvas) {
        var imgData = canvas.toDataURL('image/png');

        var doc = new jsPDF('p','mm');
        doc.addImage(imgData, 'PNG', 10,10);

        var pdfMail = btoa(doc.output());

         $.post("../mailPdf.php",
         {
            data:pdfMail
         },function (response,status) {
                console.log(response);
                 });

    }
});});});

mailPdf.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 = 'recipient@email.com';
$from = 'sender@email.com';
$from_name = 'From Name';

//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";
} 

脚本一直运行到if(is_file($files[$i])){循环。 is_file始终返回false,此循环无法运行。我无法弄清楚为什么它无法识别和读取文件。

1 个答案:

答案 0 :(得分:3)

php方面的问题是,在函数MailWithAttachment中,您期望files参数的数据类型有点不同。

$file = $_POST['data'];实际上不是文件,而是base64编码的字符串(btoa(doc.output()))。所以你真正想要做的就是这样(遵循MailWithAttachment函数的签名)。

改变这个:

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

这样的事情:

//attachment file encoded
$files = array();
$file = $_POST['data'];
$filePath = '/tmp/foo.pdf';
// actually here you can do whatever you want. you just need to save file on disk, at least for request context. I think creating file in memory also should work.
file_put_contents($filePath, base64_decode($file));
$files[] = $filePath;
...
// pay attention $file is changed to $files here
MailWithAttachment(...,$files);
...