Reputation: 569
I am using TCPDF to generate and then email a PDF. It works fine for windows, gmail etc but won't open on an ipad. The ipad says that it is not a valid pdf format.
I've also had some users reporting that it won't open on their machines (windows machines). I can't see anything wrong with the code, in desperation I tried adjusting the format to remove UTF encoding but it made no difference.
I'm not sure if this is just an ipad issue, a font issue or something else. It has had me tearing my hair out for a while now. I ran it through pdfHarmony which reported no errors.
My code is:
$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> </p>';
for($xx=0;$xx<$quantity;$xx++){
$html="";
$html.='
<div style="page-break-inside:avoid;">
<p></p>
<table>
<tr>
<td style="width:20%"> </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%"> </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'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 = "[email protected]";
$subject = 'Order Form';
$repEmail = '[email protected]';
$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";
}
?>
Sorry the code is a bit long but it needs to include both the tcpdf creation part and the email part.
thanks
Alan
Upvotes: 2
Views: 2845
Reputation: 569
Okay so after much searching etc I found the problem. For those that look at this in the future. I first tested my pdf output by putting it to the screen with:
$fileatt = $pdf->Output($fileName, 'I');
Once I'd confirmed that worked I was then sure it was a mailing issue. I quite quickly got to an encoding problem so I changed:
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
To:
$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));
This encodes the pdf as a string and then base 64 encodes which seems to resolve the problem. I've tested this in Android, Ipad, and Windows and it works for all of them. So problem solved.
Upvotes: 2
Reputation: 772
I just had a similar issue where I wasn't able to successfully download any pdf generated by tcpdf trying to download them (generated on the fly) on ios or android devices it always showed something like "unsuccessful download" and created 0kb pdf files
fixed it by manually terminating the php script using exit;
at the end of my php code which generates the pdf
Upvotes: 2