如何使用fpdf在分页符上重复标题

时间:2017-03-12 08:35:47

标签: php html css fpdf

我正在尝试使用FPDF创建报告。下面的代码很好地生成一个报告,但现在我需要在每个页面上重复表头部分。另外如何在页眉和页脚上添加边距。目前只有徽标部分重复。如何使用公司名称徽标和表头重复整个部分。

以下是我的表格标题部分:

    $pdf->SetFillColor(170, 170, 170); //gray
    $pdf->setFont("Arial","B","9");
    $pdf->setXY(10, 40); 
    $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
    $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
    $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
    $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
    $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

以下是我的完整代码:

    <?php
$localhost = 'localhost'; //name of server. Usually localhost
$database = 'payorder'; //database name.
$username = 'root'; //database username.
$password = ''; //database password.

// connect to db  
$conn = mysql_connect($localhost, $username, $password) or die('Error connecting to mysql');   
$db = mysql_select_db($database,$conn) or die('Unable to select database!'); 

        require('lib/include/fpdf/fpdf.php');
            class PDF extends FPDF {
                //Page header
                function Header(){
                    //Logo
                    $this->Image('images/logo.jpg',15,8,20);

                }
                //Page footer
                function Footer(){
                    //Position at 1.5 cm from bottom
                    $this->SetY(-15);
                    //$this->SetX(-35);
                    //Arial italic 8
                    $this->SetFont('Arial','I',8);

                    //Page number
                    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
                }
            }

            $pdf = new PDF();
            //$pdf->open();
            $pdf->AddPage("P","A4"); // P =portrait L = Landscape
            $pdf->AliasNbPages();   // necessary for x of y page numbers to appear in document
            $pdf->SetAutoPageBreak(true, 20);

            // document properties
            $pdf->SetAuthor('Sonali Bank Limited');
            $pdf->SetTitle('Daily Transuction Report');

            $pdf->SetFont('Arial','B',14);
            $pdf->Cell(30,10,' Company name Here');

            // Add date report ran
            $pdf->SetFont('Arial','I',10);
            $date =  date("d/m/Y");
            $pdf->Cell(20,20,"Company Subtitle here Dated: ".$date);
            $pdf->SetDrawColor(0, 0, 0); //black

            //table header
            $pdf->SetFillColor(170, 170, 170); //gray
            $pdf->setFont("Arial","B","9");
            $pdf->setXY(10, 40); 
            $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
            $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
            $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
            $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
            $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

            $y = 50;
            $x = 10;  
            $pdf->setXY($x, $y);
            $pdf->setFont("Arial","","9");

            //payorder data query
            $sql = "SELECT * FROM payorder_data WHERE po_date ='$report_day'"; 
            $res = mysql_query($sql) or die(mysql_error());
            $row = mysql_num_rows($res);
            while($row = mysql_fetch_array($res)){
                    $pdf->Cell(25, 8, $row['po_date'], 1);  
                    $pdf->Cell(35, 8, $row['po_number'], 1);
                    $pdf->Cell(50, 8, $row['po_ben_name'], 1);
                    $pdf->Cell(30, 8, $row['contra_date'], 1);
                    $pdf->Cell(30, 8, $row['po_amount'], 1);
                    $y += 8;

                    if ($y > 260)    // When you need a page break
                    {
                        $pdf->AddPage();
                        $y = 40;
                    }
                    $pdf->setXY($x, $y);
                }
            $pdf->Output();
            }

1 个答案:

答案 0 :(得分:1)

您应该修改本节中的代码

if ($y > 260)    // When you need a page break
   {
     $pdf->AddPage();
     $y = 40;
   }

制作这样的东西

if ($y > 260)    // When you need a page break
   {
      $pdf->AddPage();
      $pdf->SetFillColor(170, 170, 170); //gray
      $pdf->setFont("Arial","B","9");
      $pdf->setXY(10, 40); 
      $pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);  
      $pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
      $pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
      $pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1); 
      $pdf->Cell(30, 10, "Amount", 1, 0, "L", 1); 

      $y = 50;
      $x = 10;  
      $pdf->setXY($x, $y);
      $pdf->setFont("Arial","","9");
   }

已编辑:

要设置保证金,您可以查看http://www.fpdf.org/en/doc/setmargins.htm

上的文档

在此文档中,它不包含底部边距,因此对于底部边距,请访问此帖子:How to set a bottom margin in FPDF

您也可以像这样设置保证金

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);