如何在每个页面使用FPDF库显示表头? [英] How to display table header in every page using FPDF library?

查看:151
本文介绍了如何在每个页面使用FPDF库显示表头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要你的帮助我的代码。
我的问题是如何从上一页获取表头,并在剩余的页面访问它?如果我有一个大数据,我想在每一页也指出头表。



下面是构建表的函数:

 函数BuildTable($ header,$ data){

//颜色,线宽和粗体字体
$ this-> SetFillColor(255,255,255) ;
$ this-> SetTextColor(0);
$ this-> SetDrawColor(0,0,0);
$ this-> SetLineWidth(.3);
$ this-> SetFont('Arial','',7);

//标题
//为列宽设置数组

$ this-> SetFillColor(0,0,0);
$ this-> SetTextColor(255);
$ this-> SetDrawColor(0,0,0);
$ this-> SetLineWidth(.3);
$ this-> SetFont('','B');

$ w = array(15,120,30,30); // CHANGE THIS
//将标题发送到PDF文档

for($ i = 0; $ i< count($ header); $ i ++)

$ this-> Cell($ w [$ i],7,$ header [$ i],1,0,'C',1);
$ this-> Ln();

//颜色和字体恢复

$ this-> SetFillColor(255,255,230);
$ this-> SetTextColor(0);
$ this-> SetFont('');
$ this-> SetTextColor(0);
$ fill = false; //用于交替行颜色背景

//这是数据的一部分
foreach($ data as $ row){

$ this-> ; Cell($ w [0],4,$ row [0],'LR',0,'C',$ fill);
$ this-> SetFont('');

$ this-> Cell($ w [1],4,$ row [1],'LR',0,'L',$ fill);
$ this-> SetFont('');

$ this-> Cell($ w [2],4,$ row [2],'LR',0,'R',$ fill);
$ this-> SetFont('');

$ this-> Cell($ w [3],4,$ row [3],'LR',0,'R',$ fill);
$ this-> SetFont('');



$ this-> Ln();
$ fill =! $ fill;
}

$ this-> Cell(array_sum($ w),0,'','T');


$ this-> Ln(50);
$ this-> Ln(50);

}

下面是对PDF类

  foreach($ result-> result_array()as $ row){

$ data [] = array
number_format($ row ['total'],2),
, 2),

);

}


$ pdf = new PDF();
$ pdf-> AliasNbPages();

$ header = array('QTY','ITEM / DESCRIPTION','UNIT PRICE','TOTAL AMOUNT');

$ pdf-> SetFont('Arial','',8);
$ pdf-> SetFillColor(255,0,0);

$ pdf-> AddPage('l');

$ pdf-> Ln();
$ pdf = new PDF();

顺便说一句,我还有一个页眉和页脚功能。
在第一行,我需要有39行,不包括表头和第二行,其余的应该是36行。



我如何计算?

解决方案



div>

根据fpdf教程。

  class PDF extends FPDF 
{
//页header
function Header()
{
// Logo
$ this-> Image('logo.png',10,6,30);
// Arial bold 15
$ this-> SetFont('Arial','B',15);
//向右移动
$ this-> Cell(80);
//标题
$ this-> Cell(30,10,'Title',1,0,'C');
//换行符
$ this-> Ln(20);
}

//页脚
函数Footer()
{
//位于离底部1.5厘米处
$ this-> ; SetY(-15);
// Arial italic 8
$ this-> SetFont('Arial','I',8);
//页码
$ this-> Cell(0,10,'Page'。$ this-> PageNo()。'/ {nb}',0,0,'C' );
}
}

//继承类的实例
$ pdf = new PDF();
$ pdf-> AliasNbPages();
$ pdf-> AddPage();
$ pdf-> SetFont('Times','',12);
for($ i = 1; $ i <= 40; $ i ++)
$ pdf-> Cell(0,10,'Printing line number'。$ i,0,1);
$ pdf-> Output();

参考


just need your help with my code. My question is how can I get the table header from the previous page and access it on the remaining pages? If I have a large data I want to indicate also the header table in every page. But I don't have an idea how to do it.

Here's the function that build the table:

function BuildTable($header,$data) {

            //Colors, line width and bold font
            $this->SetFillColor(255,255,255);
            $this->SetTextColor(0);
            $this->SetDrawColor(0,0,0);
            $this->SetLineWidth(.3);
            $this->SetFont('Arial','',7);

            //Header    
            // make an array for the column widths

            $this->SetFillColor(0,0,0);
            $this->SetTextColor(255);
            $this->SetDrawColor(0,0,0);
            $this->SetLineWidth(.3);
            $this->SetFont('','B');

            $w = array(15,120,30,30);  //CHANGE THIS
            // send the headers to the PDF document

            for($i = 0; $i < count($header); $i++)

                $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
                $this->Ln();

                //Color and font restoration

                $this->SetFillColor(255,255,230);
                $this->SetTextColor(0);
                $this->SetFont('');
                $this->SetTextColor(0);
                $fill = false; // used to alternate row color backgrounds

            //HERE'S THE PART THAT LOOPS THE DATA
            foreach($data as $row){ 

                $this->Cell($w[0],4,$row[0],'LR',0,'C',$fill);
                $this->SetFont('');

                $this->Cell($w[1],4,$row[1],'LR',0,'L',$fill);
                $this->SetFont('');

                $this->Cell($w[2],4,$row[2],'LR',0,'R',$fill);
                $this->SetFont('');

                $this->Cell($w[3],4,$row[3],'LR',0,'R',$fill);
                $this->SetFont('');



                $this->Ln();
                $fill =! $fill;
            }

              $this->Cell(array_sum($w),0,'','T');


              $this->Ln(50);
              $this->Ln(50);

        }

Below is the call for PDF class

foreach($result->result_array() as $row){

        $data[] = array($row['item_qty'],
                        $row['itemname'],
                        number_format($row['item_price'],2),
                        number_format($row['total'],2),

                 );

    }


    $pdf = new PDF();
    $pdf->AliasNbPages();

    $header = array('QTY','ITEM / DESCRIPTION' , 'UNIT PRICE', 'TOTAL AMOUNT');

    $pdf->SetFont('Arial','',8);
    $pdf->SetFillColor(255,0,0);

    $pdf->AddPage('l');

    $pdf->Ln();
    $pdf = new PDF();

By the way I have also a header and footer function. In first row I need to have 39 rows excluding the table header and the second row and also the remaining should 36 rows.

How can I compute that? Where should I need a computation for that?

Please help me guys thanks.

解决方案

According to fpdf tutorial.

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Title',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();

reference

这篇关于如何在每个页面使用FPDF库显示表头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆