2

我正在使用 TCPDF 生成 PDF,然后通过电子邮件发送 PDF。它适用于 windows、gmail 等,但无法在 ipad 上打开。ipad 说它不是有效的 pdf 格式。

我也有一些用户报告说它不会在他们的机器(Windows 机器)上打开。我看不出代码有什么问题,绝望中我尝试调整格式以删除 UTF 编码,但没有任何区别。

我不确定这是否只是 ipad 问题、字体问题或其他问题。它已经让我把头发扯了一段时间了。我通过 pdfHarmony 运行它,它没有报告任何错误。

我的代码是:

                $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
                //  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

                // set document information
                $pdf->SetCreator(PDF_CREATOR);
                $pdf->SetAuthor('XX XX XX');
                $pdf->SetTitle('TCPDF Example 001');
                $pdf->SetSubject('TCPDF Tutorial');
                $pdf->SetKeywords('TCPDF, PDF, example, test, guide');


                $pdf->SetHeaderData('pdfheader.jpg', '190', '', '',array(0,0,0), array(255,255,255) );



                $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
                $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));


                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);


                $pdf->SetMargins('10', '50', PDF_MARGIN_RIGHT);
                $pdf->SetHeaderMargin('0');



                $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);


                $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);


                $pdf->setFontSubsetting(true);

                $pdf->SetFont('helvetica', '', 12, '', true);



                $pdf->AddPage();
                $dateNow = date('d-m-Y');

                $html = '<br/><br/><br/><br/><br/><br/>
                <h1>Order Form: Conference Ticket</h1>
                <p>Date: '.$dateNow.'</p>
                <p>Your email address:'.$payer_email.'</p>
                <p>Thank you for your order.</p>
                <p>Please print this page and bring it with you, as it will be required for registration.</p>
                <p>&nbsp;</p>';

                for($xx=0;$xx<$quantity;$xx++){


                $html="";

                $html.='
                <div style="page-break-inside:avoid;">
                <p></p>
                <table>
                <tr>
                <td style="width:20%">&nbsp;</td>
                <td style="border:1px solid #000000;text-align:center;width:60%">
                <h2>Steve Brady brings God’s Word</h2>

                <h2>Men’s Conference</h2>
                <p style="text-align:center;line-height:10%">Saturday XX November 2013</p>
                <p style="text-align:center;line-height:10%">‘Living for Christ – Faith, Work and Family’&lt;/p>

                <p style="text-align:center;line-height:10%">10.00 am – 13.00 pm</p>
                <p style="text-align:center;line-height:10%">(Tea/coffee & registration from 9.30 am)</p>

                <p style="text-align:center;line-height:10%">XX Evangelical Church</p>
                <p style="text-align:center;line-height:10%">X X Road</p>
                <p style="text-align:center;line-height:10%">XX – XX XX Xx</p>

                <p style="text-align:center;line-height:100%">Ticket Priced £5.00</p>
                <p style="text-align:center;line-height:100%">Ticket number: ';
                $ticketNumber = sprintf('%04d', $ticketIdIs);
                $html .= $ticketNumber;
                $html .='</p>
                    </td>
                    <td style="width:20%">&nbsp;</td>
                    </tr>
                    <tr><td>
                    <td>';
                $textIs = $name."-".$ticketNumber;


                $html.='</td>
                <td></td>
                </table>
                ';
                $pdf->writeHTML($html, true, false, true, false, '');

                $style = array(
                'position' => '',
                'align' => 'C',
                'stretch' => false,
                'fitwidth' => true,
                'cellfitalign' => '',
                'border' => true,
                'hpadding' => 'auto',
                'vpadding' => 'auto',
                'fgcolor' => array(0,0,0),
                'bgcolor' => false, //array(255,255,255),
                'text' => true,
                'font' => 'helvetica',
                'fontsize' => 8,
                'stretchtext' => 4
                );


                //$pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
                //  $pdf->write1DBarcode($textIs, 'C39', '', '', '', 18, 0.4, $style, 'N');

                $pdf->AddPage();    

                }
                //  $db->close();
                $html2='
                <div style="page-break-inside:avoid;">
                <p>We look forward to you joining with us for a great day listening to God&#39;s Word.</p>
                <h2>My name</h2><p></p><p></p>
                <p style="text-align:center;color:#548dd4;line-height:10%">XX</p>
                <p style="text-align:center;color:#548dd4;line-height:10%">XX is a company limited by guarantee, registered in XX.</p>
                <p style="text-align:center;color:#548dd4;line-height:10%">Company No. XX - XX Charity No. XX.</p>
                <p style="text-align:center;color:#548dd4;line-height:10%">Registered Office: XX.</p>
                <p></p>
                </div></div>
                ';
                $pdf->writeHTML($html2, true, false, true, false, '');
                // Print text using writeHTMLCell()
                /*$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='50', $html, $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=false);*/

                //*************************************************************************
                //end
                //*************************************************************************

                //*************************************************************************
                //This function allows sending of the email with the ticket details
                //
                //*************************************************************************
                $to = "myemail@gmail.com";
                $subject = 'Order Form';
                $repEmail = 'myemail@gmail.com';

                $fileName = 'orderForm.pdf';
                $fileatt = $pdf->Output($fileName, 'E');
                $attachment = chunk_split($fileatt);

                $eol = PHP_EOL;
                $separator = md5(time());

                $headers = 'From: Me <'.$repEmail.'>'.$eol;
                $headers .= 'MIME-Version: 1.0' .$eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

                $message = "--".$separator.$eol;
                $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
                $message .= "Thank you for ordering tickets to our conference.";
                if($quantity>1){
                $message .="\n\rPlease open the attached document which contains your ".$quantity." tickets.  If there is a problem please get in touch with us.";
                $message .= "\n\rPlease print out your tickets and bring them with you on the XX November 2013, we'll need them for registration purposes.";
                }else{
                $message .="\n\rPlease open the attached document which contains your ticket.  If there is a problem please get in touch with us.";
                $message .= "\n\rPlease print out the ticket and bring it with you on the 23 November 2013, we'll need it for registration purposes.";
                }   

                $message .= "\n\rThanks for your support and we look forward to meeting with you at the conference.".$eol;
                $message .= "\n\rThe CMT Team.".$eol;


                $message .= "--".$separator.$eol;
                $message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
                $message .= "Content-Transfer-Encoding: base64".$eol;
                $message .= "Content-Disposition: attachment".$eol.$eol;
                $message .= $attachment.$eol;
                $message .= "--".$separator."--";

                if (mail($to, $subject, $message, $headers)){
                // echo "Email sent";
                }

                else {
                echo "Email failed";
                }
                  ?>

抱歉,代码有点长,但它需要包含 tcpdf 创建部分和电子邮件部分。

谢谢

艾伦

4

2 回答 2

2

好的,经过大量搜索等后,我发现了问题。对于那些在未来看到这个的人。我首先通过将其放到屏幕上来测试我的 pdf 输出:

$fileatt = $pdf->Output($fileName, 'I');

一旦我确认有效,我就确定这是一个邮寄问题。我很快就遇到了编码问题,所以我改变了:

$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);

至:

$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));

这会将 pdf 编码为字符串,然后进行 base 64 编码,这似乎可以解决问题。我已经在 Android、Ipad 和 Windows 中对此进行了测试,并且适用于所有这些。所以问题解决了。

于 2013-09-18T10:59:51.060 回答
2

我只是有一个类似的问题,我无法成功下载由 tcpdf 生成的任何 pdf,试图在 ios 或 android 设备上下载它们(即时生成)它总是显示类似“下载不成功”并创建 0kb pdf 文件

exit;通过在生成pdf的php代码末尾手动终止php脚本来修复它

于 2014-01-29T19:02:52.313 回答