使用mpdf生成带有utf8字符的pdf文件

时间:2016-06-19 18:14:20

标签: utf-8 phpmailer mpdf

我正在尝试使用mpdf创建一个pdf文件并通过电子邮件发送。 该文件用英文和希伯来文写成。 当我在浏览器上打开文件时,我已经正确创建了文件($mpdf->output()): this is how it lloks like when it works  但是当我尝试用pdf查看器打开它或者当我尝试使用带有addStringAttachment($ stringAttachment,filename.pdf)的php邮件程序发送它时,所有的希伯来语都只是一堆矩形: this is how it looks when it doesnt work 这是我的代码:

    <?php
//$htmlResult is set to be the pdf page - written in html.

$mpdf = new mPDF('utf-8' , 'A4-P');

$mpdf->WriteHTML($htmlResult);
$fileName = "report".$formatedFromDate.'-'.$formatedToDate;
$mpdf->Output("1234.pdf" , "D");

当我尝试用phpMailer发送它时:

$stringPdf = $mpdf->Output("1234.pdf" , "S");
$body = " ";
$subject = $fileName;

FunctionPool::sendEmail($emailAddress, $body, $subject , $stringPdf ,$fileName);

和sendMail函数:

public static function sendEmail ($emailAddress , $body , $subject , $stringAttachments = NULL , $fileName = NULL) {


        //SMTP needs accurate times, and the PHP time zone MUST be set
        //This should be done in your php.ini, but this is how to do it if you don't have access to that
        date_default_timezone_set('Asia/Jerusalem');
        require_once __DIR__.'/variables.php';
        require_once __DIR__.'/PHPMailerMaster/PHPMailerAutoload.php';
        require_once __DIR__.'/PHPMailerMaster/class.phpmailer.php';
        require_once __DIR__.'/PHPMailerMaster/class.phpmaileroauthgoogle.php';
        require_once __DIR__.'/settings.php';
        //Create a new PHPMailer instance
        $mail = new PHPMailer;
        //Tell PHPMailer to use SMTP
        $mail->CharSet = "utf-8";
        $mail->isSMTP();

        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = 2;
        if (1 != $underDevelopment) {
            $mail->SMTPDebug = 0;
        }

        //Ask for HTML-friendly debug output
        $mail->Debugoutput = 'html';

        //Set the hostname of the mail server
        //$mail->Host = 'smtp.gmail.com';

        // use
        $mail->Host = gethostbyname('smtp.gmail.com');
        // if your network does not support SMTP over IPv6
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 587;
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'tls';
        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;
        //$mail->SMTPAutoTLS = false;        
        $mail->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
            )
        );


        //Username to use for SMTP authentication - use full email address for gmail
        $serversEmailAddress = **************@gmail.com";
        $serversEmailPassword = "**************";
        $systemsName = "**************";

        $mail->Username = $serversEmailAddress;
        //Password to use for SMTP authentication
        $mail->Password = $serversEmailPassword;
        //Set who the message is to be sent from
        $mail->setFrom($serversEmailAddress , $systemsName);
        //Set an alternative reply-to address
        $mail->addReplyTo($serversEmailAddress, $systemsName);
        //Set who the message is to be sent to
        $mail->addAddress($emailAddress);
        //Set the subject line
        $mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $mail->msgHTML($body/*file_get_contents('contents.html'), dirname(__FILE__)*/);
        //Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';



        if ($stringAttachments != NULL) {
            if ($fileName == NULL) {
                $fileName = "report";
            }
            $mail->addStringAttachment($stringAttachments , $fileName.".pdf");
        }

        //send the message, check for errors
        if (!$mail->send()) {
            echo "error: " . $mail->ErrorInfo;
        } else {
            return "sent";
        }
    }

当我发送附有pdf的电子邮件(也用希伯来语)时,它的工作正常。 我以为我会保存一个临时文件,然后发送它,但后来发现我只能从浏览器中查看该文件。

0 个答案:

没有答案