jspdf使用php创建pdf并发送邮件

时间:2017-03-21 10:45:13

标签: javascript php

我是php的新手。我正在使用jsPDF jspdf库并使用php发送pdf附件到邮件

我尝试使用此代码但不能正常工作

javascript文件

 <script type="text/javascript" src="js/jspdf.js"></script>
        <script type="text/javascript">
            var pdf = new jsPDF();
            pdf.text(30, 30, 'Hello world!');    
            var pdfBase64 = pdf.output('datauristring');
</script>

php文件

<?
        session_start();
        $email = "abc@mail.com";
        $to = "abc@mail.com";
        $from = "abc@mail.com";
        $subject = "Thank you";
        $message = "Hi, here's the file.";
        $filestr = $_POST['pdfBase64'];
        $fileatt = $filestr;
        $fileatttype = "application/pdf";
        $fileattname = $filestr;
        $headers = "From: $from";
        // This attaches the file
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
        $headers .= "\nMIME-Version: 1.0\n" .
                "Content-Type: multipart/mixed;\n" .
                " boundary=\"{$mime_boundary}\"";

        $message .= "This is a multi-part message in MIME format.\n\n" .
                "-{$mime_boundary}\n" .
                "Content-Type: text/plain; charset=\"iso-8859-1\n" .
                "Content-Transfer-Encoding: 7bit\n\n" .
                $message . "\n\n";

        $message .= "--{$mime_boundary}\n";
        $file = fopen($fileatt, 'rb');
        $data = fread($file, filesize($fileatt));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {$fileatttype};\n" .
                " name=\"{$fileatt}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$fileatt}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n" . "-{$mime_boundary}-\n";
        mail($to, $subject, $message, $headers);

如果有人有解决方案,请告诉我

0 个答案:

没有答案