【问题标题】:TCPDF / FPDF - Page break issueTCPDF / FPDF - 分页问题
【发布时间】:2012-01-10 04:40:29
【问题描述】:

我正在尝试使用数据表创建 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, $x, $y, $product['vat_percent'], 'R', $fill);
        $this->cell_discount($this->col_discount, $x, $y, $product['discount_percent'] / 100, 'R', $fill);
        $this->cell_count($this->col_count, $x, $y, $product['count'] / 100, 'R', $fill);
        $this->cell_price($this->col_price, $x, $y, $product['price'] / 100, 'R', $fill);
        $this->cell_unit($this->col_unit, $x, $y, $product['unit_name'], 'L', $fill);
        $this->cell_name(0, $x, $y, $product['name'], 'L', $fill);
        $this->cell_product($this->col_product, $x, $y, $product['product_id_'], 'L', $fill);
    }

    private function cell_sum($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_vat($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_discount($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_count($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_price($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_unit($width, &$x, $y, $str, $align, $fill=false){
        $this->cnstr_cell($width, $x, $y, $str, $align, $fill);
    }

    private function cell_name($width, &$x, $y, $str, $align, $fill=false){
        $this->pdf->SetXY($this->col_product + 10, $y);
        $this->pdf->MultiCell($x - $this->col_product - 10, $this->line_height, $str, 0, $align, $fill);
    }

    private function cell_product($width, &$x, $y, $str, $align, $fill=false){
        $this->pdf->SetXY(10, $y);
        $this->pdf->MultiCell($this->col_product, $this->line_height, $str, 0, $align, $fill);
    }

    private function cnstr_cell($width, &$x, $y, $str, $align='L', $fill=false){
        $x -= $width;
        $this->pdf->SetXY($x, $y);
        $this->pdf->MultiCell($width, $this->line_height, $str, 0, $align, $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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        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',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            ),
        array(
            'product_id_' => 'ADS1550',
            'name' => 'name',
            'unit_name' => 'pcs',
            'price' => 182450000,
            'count' => 310000,
            'discount_percent' => 19900,
            'vat_percent' => 0,
            'sum' => 1587057200
            )
        );
}

$PDF = new File_PDF();
$PDF->generate();

【问题讨论】:

  • 能否提供一个简短代码示例?只需花 10 分钟时间来简化您的代码,使其填满一页,删除所有对象属性,这样可能会有更多人尝试回答您。

标签: php pdf tcpdf fpdf


【解决方案1】:

问题在于,在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 库更易于使用和自定义。建议去看看。

【讨论】:

  • 仅供参考,今天阅读此答案时,我注意到指向wkhtmltopdf 的链接已损坏。
猜你喜欢
  • 2012-09-03
  • 2011-01-26
  • 2012-03-14
  • 2011-11-06
  • 1970-01-01
  • 2016-06-18
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多