TCPDF 在末尾添加了一个额外的空白页
TCPDF is adding an extra blank page at the end
我正在使用以下代码生成 pdf。在某些情况下,它会在末尾添加一个空白页(在最后一个 tr 中,如果我在 td 中提供更多行,它会移动到第二页,但如果我只提供几行,那么它会在末尾添加一个空白页)
这是我的代码:
// 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');
我尝试了 this 来自 Whosebug 的解决方案,但它对我不起作用。有人可以帮我解决这个多余的空白页问题吗?
我通过使用 Cell
和 MultiCell
更改我的代码解决了这个问题。 TCPDF 建议尽量避免使用 WriteHTML
,因为它很慢。我认为这可能是个问题,我将代码更改为使用 Cell
和 MultiCell
,问题就解决了。而且它真的更快。
我正在使用以下代码生成 pdf。在某些情况下,它会在末尾添加一个空白页(在最后一个 tr 中,如果我在 td 中提供更多行,它会移动到第二页,但如果我只提供几行,那么它会在末尾添加一个空白页)
这是我的代码:
// 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');
我尝试了 this 来自 Whosebug 的解决方案,但它对我不起作用。有人可以帮我解决这个多余的空白页问题吗?
我通过使用 Cell
和 MultiCell
更改我的代码解决了这个问题。 TCPDF 建议尽量避免使用 WriteHTML
,因为它很慢。我认为这可能是个问题,我将代码更改为使用 Cell
和 MultiCell
,问题就解决了。而且它真的更快。