FPDF - 使用 Codeigniter 控制器调用页眉和页脚的其他方法

Qin*_* Yi 5 php codeigniter fpdf

我是 FPDF 新手,当我使用时,我正在尝试更改页眉和页脚

    function Header()
    {
       $this->SetFont('Arial','B',15);
       $this->Cell(80);
       $this->Cell(30,10,'Title',1,0,'C');
       $this->Ln(20);
    }
Run Code Online (Sandbox Code Playgroud)

它有错误说

'无法重新声明 header()',

所以我想问的是,还有其他方法来调用页眉和页脚吗?喜欢

$pdf->header->SetFont('Arial','B',15)

或者其他的东西?

这是我的代码,我从FPDF的教程2中复制了它,我不想要它的视图,我想要它在控制器中,这样它就可以一直调用

public function tutorial2()
{
    $this->load->library('myfpdf'); 
    // function Header()
    // {
    //     $this->SetFont('Arial','B',15);
    //     $this->Cell(80);
    //     $this->Cell(30,10,'Title',1,0,'C');
    //     $this->Ln(20);
    // } -- the Header function that is normally used

    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Times','',12);
    //$pdf->footer->SetY(-15);
    //$pdf->footer->SetFont('Arial','I',8);
    //$pdf->footer->Cell(0,10,'Page '.$pdf->footer->PageNo().'/{nb}',0,0,'C');      -- not actually a working, or is there another way?
    for($i=1;$i<=40;$i++)
        $pdf->Cell(0,10,'Printing line number '.$i,0,1);
    $pdf->Output();     
}
Run Code Online (Sandbox Code Playgroud)

Tej*_*aru 2

从网站下载 FPDF 。下载文件后解压,您会发现一个名为“fpdf181”的文件夹(其中181是FPDF的版本)。

  1. 现在将此文件夹移动到 Codeigniter 的“application/third_party”文件夹中。
  2. 现在在 Codeigniter 的“application/libraries”文件夹中创建一个名为“CustomFPDF.php”的新库。将以下代码复制到“CustomFPDF.php”中

代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH . 'third_party/fpdf181/fpdf.php');

class CustomFPDF extends FPDF {

    public function header($family){
        $this->SetFont($family,'B',15);
        $this->Cell(80);
        $this->Cell(30,10,'Title',1,0,'C');
        $this->Ln(20);
    }

    public function getInstance(){
        return new CustomFPDF();
    }
}
?>
Run Code Online (Sandbox Code Playgroud)
  1. 现在在“应用程序/控制器”中创建一个控制器,并在使用 FPDF 的函数中使用以下内容:

        $this->load->library('CustomFPDF');
        $pdf = $this->customfpdf->getInstance();
    
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->header('Arial');
        $pdf->SetFont('Times','',12);
        for($i=1;$i<=40;$i++)
            $pdf->Cell(0,10,'Printing line number '.$i,0,1);
        $pdf->Output(); 
    
    Run Code Online (Sandbox Code Playgroud)