php – TCPDF/FPDF – 分页符问题

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – TCPDF/FPDF – 分页符问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用数据表创建一个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库更容易使用和自定义.我建议看看它.

脚本宝典总结

以上是脚本宝典为你收集整理的php – TCPDF/FPDF – 分页符问题全部内容,希望文章能够帮你解决php – TCPDF/FPDF – 分页符问题所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。