Php 使用tcpdf AcceptPageBreak()更改第二页的上边距
我正在使用TCPDF生成PDF。PDF通过fpdi类使用PDF模板。一些生成的PDF是一页式的。但有时我有第二页。我使用$pdf->MultiCell输出我的内容。分页符可以通过$pdf->SetAutoPageBreak(true)正常工作 现在我的问题是:我需要在第二页上有一个不同的上边距。到目前为止,我尝试的是使用AcceptPageBreak()函数——不幸的是没有成功 以下代码被剪掉后,我设法更改了第二页的页边空白。但它在PDF的末尾添加了一个空白页面Php 使用tcpdf AcceptPageBreak()更改第二页的上边距,php,fpdf,tcpdf,Php,Fpdf,Tcpdf,我正在使用TCPDF生成PDF。PDF通过fpdi类使用PDF模板。一些生成的PDF是一页式的。但有时我有第二页。我使用$pdf->MultiCell输出我的内容。分页符可以通过$pdf->SetAutoPageBreak(true)正常工作 现在我的问题是:我需要在第二页上有一个不同的上边距。到目前为止,我尝试的是使用AcceptPageBreak()函数——不幸的是没有成功 以下代码被剪掉后,我设法更改了第二页的页边空白。但它在PDF的末尾添加了一个空白页面 public function
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结尾的空白页?我终于找到了解决我自己问题的方法。 也许对其他有同样问题的人来说很有趣 我使用了上面发布的函数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');
$pdf=new MYPDF();
$pdf->SetMargins(24,54);
$pdf->AddPage();
...
$pdf->MultiCell('0','','text','L');
$pdf->lastPage();
$lastPage=$pdf->PageNo()+1;
$pdf->Output($filePath,'F');
//删除最后一页
$finalPdf=新的FPDI();
$finalPdf->setSourceFile($filePath);
对于($i=1;$i<$lastPage;$i++){
$finalPdf->AddPage();
$tplIdx=$finalPdf->importPage($i);
$finalPdf->useTemplate($tplIdx);
}
$finalPdf->Output($filePath,'F');
希望能有帮助 使用您的一些代码和原始函数,我找到了一种方法,它不会在文件末尾添加不必要的空白页
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;
}
公共函数AcceptPageBreak(){
如果(1=$this->PageNo()){
$this->SetMargins($left\u margin,$top\u margin,$right\u margin,true);
}
如果($this->num\u columns>1){
//多列模式
如果($this->current_columns<($this->num_columns-1)){
//转到下一列
$this->selectColumn($this->current_column+1);
}elseif($this->AutoPageBreak){
//添加新页面
$this->AddPage();
//设置第一列
$this->selectColumn(0);
}
//避免使用checkPageBreak()进行分页符
返回false;
}
返回$this->AutoPageBreak;
}
TCPDF自动分页符会导致内容呈现不一致。元素可能会无意中超出页面边界,这可能会导致生成其他页面。使用以下方法添加内容时,最好仅使用自动分页中断:
$pdf->SetAutoPageBreak(true, $margin_bottom);
然后在不需要时禁用它
$pdf->SetAutoPageBreak(false);