2011-11-30 73 views
6

我試圖用數據表創建一個PDF文件..但是當一個分頁符被滿足時,它會跳轉到一個新頁面,每當一個新的多單元被添加到頁面斷點水平..!?TCPDF/FPDF - 分頁問題

我試圖做同樣的用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(); 
+0

您能否給一個* short *代碼樣本?只需10分鐘即可簡化代碼,以便填充一個頁面,刪除所有對象屬性,並有更多人嘗試回答您。 – 2011-12-03 17:20:42

回答

19

的問題是,在Cell()方法(其在MultiCell()稱爲)FPDF永諾增加了一個新的頁面,如果當前Y位置+新小區的高度比allowe更大d頁面高度。

默認頁面高度似乎爲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 
} 

Btw。我最近還必須生成一個動態PDF,最後我使用wkhtmltopdf,它比所有PHP庫更容易使用和定製。我建議看看它。

+0

非常感謝..! :) – clarkk

+1

只是一個FYi,今天在閱讀這個答案時,我注意到'wkhtmltopdf'的鏈接被破壞了。 –

+1

@TimLewis謝謝,我修復了這個鏈接! – Jona