我正在尝试使用数据表创建一个PDF文件.但是当遇到分页符时,每次在断点级别将新的多单元添加到页面时它会跳转到新页面.
我试图用TCPDF做同样的事情,但每次在页面断点点附近添加一个新单元时,页面中断仍然是同样的问题…
例:
http://www.online-økonomi.dk/_tst_fpdf.php
require_once '../class/download/fpdf/fpdf.PHP'; class File_PDF { private $pdf; private $col_product = 25; private $col_unit = 12; private $col_price = 20; private $col_count = 14; private $col_discount = 12; private $col_vat = 12; private $col_sum = 22; private $width = 200; private $line_height = 4.2; private $margin_top = 30; public function generate(){ $this->pdf = new FPDF(); $this->pdf->AddPage(); $this->pdf->SetDisplayMode('real'); $this->pdf->SetAutoPageBreak(true,150); if($this->products){ $i = 0; $this->color_light(); foreach($this->products as $product){ $this->add_product($product,$i % 2 ? true:false); $i++; } } $this->pdf->Output(); } private function add_product($product,$fill){ $this->txt(); $x = $this->width; $y = $this->pdf->GetY(); $this->cell_sum($this->col_sum,$x,$y,$product['sum'] / 100,'R',$fill); $this->cell_vat($this->col_vat,$product['vat_percent'],$fill); $this->cell_discount($this->col_discount,$product['discount_percent'] / 100,$fill); $this->cell_count($this->col_count,$product['count'] / 100,$fill); $this->cell_price($this->col_price,$product['price'] / 100,$fill); $this->cell_unit($this->col_unit,$product['unit_name'],'L',$fill); $this->cell_name(0,$product['name'],$fill); $this->cell_product($this->col_product,$product['product_id_'],$fill); } private function cell_sum($width,&$x,$str,$align,$fill=false){ $this->cnstr_cell($width,$fill); } private function cell_vat($width,$fill); } private function cell_discount($width,$fill); } private function cell_count($width,$fill); } private function cell_price($width,$fill); } private function cell_unit($width,$fill); } private function cell_name($width,$fill=false){ $this->pdf->SetXY($this->col_product + 10,$y); $this->pdf->MultiCell($x - $this->col_product - 10,$this->line_height,$fill); } private function cell_product($width,$fill=false){ $this->pdf->SetXY(10,$y); $this->pdf->MultiCell($this->col_product,$fill); } private function cnstr_cell($width,$align='L',$fill=false){ $x -= $width; $this->pdf->SetXY($x,$y); $this->pdf->MultiCell($width,$fill); } private function color_light(){ $this->pdf->SetFillColor(200,200,200); } private function txt(){ $this->pdf->SetFont('Arial','',8.5); } private function txt_marked(){ $this->pdf->SetFont('Arial','B',8.5); } private $products = array( array( 'product_id_' => 'ADS1550','name' => 'name','unit_name' => 'pcs','price' => 182450000,'count' => 310000,'discount_percent' => 19900,'vat_percent' => 0,'sum' => 1587057200 ),array( 'product_id_' => 'ADS1550','sum' => 1587057200 ) ); } $PDF = new File_PDF(); $PDF->generate();
问题是在Cell()方法(在MultiCell()中调用)中,如果当前Y位置新单元格的高度大于允许的页面高度,则FPDF总是添加新页面.
默认页面高度似乎是297,SetAutoPageBreak()从中减去150.所以当Y cell_height比147更重要时,你总是在调用cnstr_cell()时获得一个新页面.
要防止这种情况,您需要自己调用AddPage().在add_product()方法中添加此检查:
$x = $this->width; $y = $this->pdf->GetY(); if (($y + $this->line_height) >= 147) { $this->pdf->AddPage(); $y = 0; // should be your top margin }
顺便说一句.我最近还必须生成动态PDF,最后使用wkhtmltopdf,它比所有PHP库更容易使用和自定义.我建议看看它.