2011-10-14 47 views
0

我正在使用TCPDF生成PDF。 PDF通过fpdi-class使用PDF模板。一些生成的PDF是一页的。但有时我有第二页。我使用$ pdf-> MultiCell输出我的内容。分页符可以通过$ pdf-> SetAutoPageBreak(true)正常工作。使用tcpdf更改第二页的顶部边距AcceptPageBreak()

现在我的问题:我需要在第二页上有一个不同的顶边距。我到目前为止所尝试的是使用AcceptPageBreak()函数 - 不幸的是没有成功。

通过以下代码剪切,我设法更改第二页上的页边距。但它在PDF的末尾添加了一个空白页面。

public function AcceptPageBreak() { 

    $this->SetMargins(24, 65, 24, true); 
    $this->AddPage();   
    return false; 

} 

我试图用$ pdf-> deletePage删除最后一页,但它不起作用。 我试图插入一些条件进入功能:

public function AcceptPageBreak() { 
    if (1 == $this->PageNo()) { 
     $this->SetMargins(24, 65, 24, true); 
     $this->AddPage();   
     return false; 
    } else { 
     return false; 
    } 

} 

也能正常工作与文本2页PDF文件。但是现在我总是可以阅读两份分页的PDF文档 - 即使我只有一小段文字。看起来每次生成PDF时都会调用“AcceptPageBreak()”函数。

如何防止在我的PDF文件末尾出现空白页?

回答

1

我终于找到了解决我自己的问题。 也许这对有同样问题的其他人很有趣。

我把功能AcceptPageBreak()像上面张贴(版本1)。保存PDF后,我将PDF导入到没有最后一页的新PDF中,并保存新的PDF。

下面的代码:

$pdf = new MYPDF(); 

$pdf->SetMargins(24, 54);   

$pdf->AddPage(); 

... 

$pdf->MultiCell('0', '', $text, '', 'L'); 

$pdf->lastPage(); 

$lastPage = $pdf->PageNo() + 1; 

$pdf->Output($filePath, 'F'); 

// remove last page 

$finalPdf = new FPDI(); 
$finalPdf->setSourceFile($filePath); 

for ($i=1; $i < $lastPage; $i++) { 
    $finalPdf->AddPage(); 
    $tplIdx = $finalPdf->importPage($i); 
    $finalPdf->useTemplate($tplIdx);    
} 
$finalPdf->Output($filePath, 'F'); 

希望它能帮助。

6

使用你的一些代码和原始函数,我找到了一种方法,它不会在文件末尾添加一个不需要的空白页。

public function AcceptPageBreak() { 
     if (1 == $this->PageNo()) { 
       $this->SetMargins($left_margin, $top_margin, $right_margin, true); 
     } 
     if ($this->num_columns > 1) { 
      // multi column mode 
      if ($this->current_column < ($this->num_columns - 1)) { 
       // go to next column 
       $this->selectColumn($this->current_column + 1); 
      } elseif ($this->AutoPageBreak) { 
       // add a new page 
       $this->AddPage(); 
       // set first column 
       $this->selectColumn(0); 
      } 
      // avoid page breaking from checkPageBreak() 
      return false; 
     } 
     return $this->AutoPageBreak; 
    } 
0

TCPDF自动换页导致内容呈现出现一些不一致。可能无意中延伸出页面边界的元素可能会导致生成其他页面。最好只在使用以下内容时自动截断:

$pdf->SetAutoPageBreak(true, $margin_bottom); 

然后在不需要时禁用它。

$pdf->SetAutoPageBreak(false);