使用phpmailer和mpdf获取空白的pdf文件附件

时间:2014-11-14 11:32:44

标签: php phpmailer mpdf

当点击一个按钮并且我将它们保存在文件夹中时,我正在使用mPDF生成PDF。我正在寻找一种使用PHPmailer将PDF添加到附件的方法。这是我尝试过的:

$dir =  $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF']);

$pdfexist = $dir."/classes/pdf/feestructure_".$student['rollno'].".pdf";
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->From="xyz@abc.com";
        $mail->FromName="xyz";
        $mail->addAddress("xyz@abc.com");
        //echo $email."<br/>";
        $mail->addAddress("xyz@abc.com");
        $mail->addAddress("xyz@abc.com");
        $mail->Subject = 'XYZ';

       $pdfstring = $pdfexist;
       $mail->AddStringAttachment($pdfstring, "feestructure_".$roll.".pdf", $encoding = 'base64', $type = 'application/pdf');   

我生成的pdf的大小是13k,但它在邮件附件中显示1 k。帮助我们。

这是mpdf的输出:

$mpdf->WriteHTML(file_get_contents("$dir/feestructure_pdf.php?rollno=$rollno"));    
          $pdfname="feestructure_".$rollno.".pdf";   
          $mpdf->Output("classes/pdf/".$pdfname,"F");

1 个答案:

答案 0 :(得分:2)

您的$pdfexists / $pdfstring变量包含文件路径,而非二进制PDF数据,因此您应该使用AddAttachment(),而不是AddstringAttachment()。 AddAttachment附加文件(如PDF),AddStringAttachment附加字符串,就像您可能从Web调用或数据库中获取的内容。

$mail->AddAttachment($pdfstring, "feestructure_".$roll.".pdf", $encoding = 'base64', $type = 'application/pdf');
相关问题