【问题标题】:TCPDF generated PDF won't open in ipadTCPDF 生成的 PDF 无法在 ipad 中打开
【发布时间】:2013-09-18 09:16:51
【问题描述】:

我正在使用 TCPDF 生成 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’</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 创建部分和电子邮件部分。

谢谢

艾伦

【问题讨论】:

标签: ipad pdf tcpdf


【解决方案1】:

我刚刚遇到了一个类似的问题,我无法成功下载 tcpdf 生成的任何 pdf,试图在 ios 或 android 设备上下载它们(动态生成) 它总是显示类似“下载失败”并创建了 0kb pdf 文件

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

【讨论】:

    【解决方案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 中对此进行了测试,并且适用于所有这些。所以问题解决了。

    【讨论】:

    • 感谢您花时间发布您的修复。顺便说一句,您不使用 SwiftMailer 或 PHPMailer 之类的库是否有原因?我已经编写了一个通过电子邮件发送 PDF 的系统超过六年了,我可以从经验中说,成功发送带有一个或多个附件的电子邮件并让它们在所有平台上正确呈现是一项非常棘手的工作,并且通过使用非常简化图书馆。此外,像 PHPMailer 这样的库可以进行 DKIM 签名,这可以显着降低目标邮件服务器将电子邮件标记为垃圾邮件的机会。
    • 没有理由不使用 PHP Mailier - 这是我通常会做的,但我喜欢先让事情在“原始”级别工作,这样我才能更全面地了解正在发生的事情 :)跨度>
    猜你喜欢
    • 2013-11-29
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2015-02-26
    • 2017-04-22
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多