Search code examples
phphtmlpdfpdf-generationtcpdf

TCPDF is adding an extra blank page at the end


I'm using the following code to generate pdf. It is adding a blank page at the end in some cases(In the last tr,If I give more more lines in the td it is moving to second page but if I give few lines then it is adding a blank page at the end)

Here is my code:

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// set font
$pdf->SetFont('helvetica', '', 8);

// add a page
$pdf->AddPage('P');

$html = '<style type="text/css">
    th, td{
        border:1px solid #ccc;
    }
    </style>
    <table id="maintable" border="1" cellspacing="0" cellpadding="3" style="border:1px solid #cccccc;">
         <tr bgcolor="#dddddd" nobr="true">
         <th align="center" width="40px">'.LBL_PDF_EQUIPER_ID.'</th>
         <th align="left" width="100px">'.LBL_PDF_EQUIPER_NOM_PRENOM.'</th>
         <th align="left" width="175px">'.LBL_PDF_EQUIPER_EMAIL.'</th>
         <th align="center" width="100px">'.LBL_PDF_EQUIPER_TELEPHONE.'</th>
         <th width="120px">'.LBL_PDF_EQUIPER_ADRESSE.'</th>
         </tr>';
foreach($records as $record){       
    $comm_related_set = $record->getRelatedSet('con_sec__con__COMM');
    $email = "";
    $telephone = "";
    $html .= '<tr padding="0" nobr="true">
    <td align="center" width="40px">'.trim($record->getField('con_sec__USR::__kp__UsrId__lsan')).'</td>
    <td align="left" width="100px">'.trim($record->getField('con_sec__CON::NOM_Prenom_Societe')).'</td>
    <td align="left" width="175px">'.trim(implode('<br/>', $email)).'</td>
    <td align="left" width="100px">'.trim(implode('<br/>', $telephone)).'</td>
    <td width="120px">'.preg_replace('/[\n\r]/','<br/>',trim($record->getField('con_sec__con__ADDR::Address_comp_display_PHP'))).'</td>
</tr>';
}

$html .= '</table>';

// output the HTML content
//echo $html; exit;
$pdf->writeHTML($html, false, false, true, false, '');
$pdf->lastPage();

$fileName = 'Liste';
//Close and output PDF document
$pdf->Output($fileName . '.pdf', 'D');

I tried this solution from stackoverflow but it is not working for me. Can someone help me to fix this extra blank page issue?


Solution

  • I solved this problem by changing my code using Cell and MultiCell. TCPDF suggested to avoid using WriteHTML as much as since it is slow. I thought this might be an issue and I changed my code to use Cell and MultiCell and the problem is solved. Also it is really faster.