2011-11-30 16 views
6

Sto provando a creare un file PDF con una tabella di dati .. Ma quando viene soddisfatta un'interruzione di pagina, salta a una nuova pagina ogni volta che un nuovo multicell viene aggiunto alla pagina al punto di rottura ..!?TCPDF/FPDF - Interruzione pagina

ho cercato di fare esattamente la stessa cosa con TCPDF, ma ancora lo stesso problema con un'interruzione di pagina ogni volta che una nuova cella ho aggiunto intorno al livello di punto di interruzione di pagina ...

esempio:

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

Potrebbe fornire un esempio di codice * breve *? Impiega solo 10 minuti per semplificare il codice in modo che riempia una pagina, rimuovi tutte le proprietà dell'oggetto e più persone possano provare a rispondere. –

risposta

19

il problema è che nel metodo Cell() (che è chiamato in MultiCell()) FPDF aggiunge allways una nuova pagina se la posizione attuale Y + l'altezza della nuova cella è maggiore del allowe d altezza della pagina.

L'altezza di pagina predefinita sembra essere 297, con SetAutoPageBreak() si sottrae 150 da esso. Quindi, quando Y + cell_height è più grave di 147, ricevi sempre una nuova pagina quando chiami il tuo cnstr_cell().

Per evitare questo è necessario chiamare da soli AddPage(). Aggiungere questo controllo nel metodo 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. Ho anche dovuto generare un PDF dinamico di recente, ho finito per usare wkhtmltopdf, era molto più facile da usare e personalizzare rispetto a tutte le librerie PHP. Suggerisco di dare un'occhiata a questo.

+0

grazie mille ..! :) – clarkk

+1

Solo una FYi, leggendo questa risposta oggi, ho notato che il collegamento a "wkhtmltopdf" è rotto. –

+1

@TimLewis grazie, ho corretto il collegamento! – Jona