【问题标题】:FPDF Header Footer Codeigniter 4 Not ShowingFPDF 页眉页脚 Codeigniter 4 不显示
【发布时间】:2023-01-28 15:48:11
【问题描述】:

我正在尝试使用 fpdf 将页眉和页脚添加到我的 pdf 文件 我的控制器代码是:

<?php
namespace App\Controllers;
use App\Libraries\FPDF;

class Test extends BaseController
{
    function Header()
    {
        $this->Image('logo.png',10,6,30);
        $this->SetFont('Arial','B',15);
        $this->Cell(80);
        $this->Cell(30,10,'Test',1,0,'C');
        $this->Ln(20);
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial','I',8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }

    public function index()
    {
        $this->pdf = new fpdf();
        $this->pdf->AliasNbPages();
        $this->pdf->AddPage();
        $this->pdf->SetFont('Times','',12);
        for($i=1;$i<=40;$i++)
            $this->pdf->Cell(0,10,'Printing line number '.$i,0,1);
        $this->response->setHeader("Content-Type", "application/pdf");
        $this->pdf->Output();
    }   
}

pdf 输出只是没有页眉页脚的行号。有什么线索吗?

【问题讨论】:

  • Header()Footer() 放入控制器中没有任何效果。您必须扩展 FPDF 类。

标签: php codeigniter fpdf


【解决方案1】:

现在它正在通过在 FPDF tuto2 之后稍微更改代码来工作。

<?php
namespace AppControllers;

use AppLibrariesFPDF;

class PDF extends FPDF
{
    function Header()
    {
        $this->Image('logo.png',10,6,30);
        $this->SetFont('Arial','B',15);
        $this->Cell(80);
        $this->Cell(30,10,'Title',1,0,'C');
        $this->Ln(20);
    }

    function Footer()
    {
        $this->SetY(-15);
        $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();

感谢 Olivier PLATHEY 为您提供了出色而简单的 FPDF 课程

【讨论】:

    【解决方案2】:

    尝试从我的 ajax 调用控制器

    $.ajax({
                    type: 'GET',
                    url:  s+"registerReport",
    

    我将输出更改为:

    $pdf->Output('F','../public/pdf/somefile.pdf');
    

    创建的文件但有错误:

    调用未定义的方法 AppControllersPDF::initController() 如何解决? ^_^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2015-03-04
      • 2013-04-08
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多