【问题标题】:Laravel display "FPDF error: No page has been added yet"Laravel 显示“FPDF 错误:尚未添加页面”
【发布时间】:2020-08-25 04:06:14
【问题描述】:

我正在为我的 laravel 项目使用 fpdf 库。我为页眉和页脚函数创建了一个类。然后在我的 pdf 控制器上调用 thess 函数。我遇到此错误“FPDF 错误:尚未添加页面”,我不知道此错误来自何处。你能教我如何修复这个错误/错误。提前致谢。

来自我的控制器的代码

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Codedge\Fpdf\Fpdf\Fpdf;
use App\Personnel;
use App\Classes\PDFClass;

class PFTReportController extends Controller
{
    public function postPFTReport(Request $request)
    {
        $pdf = new FPDF();
        $pdf->AddPage('P', 'A4');
    $pdf->Ln(4);
    $pdf->SetFont('Arial', '', 12);

    // Call the header for this report
    $pdfClass = new PDFClass();
    $header = $pdfClass->Header();

    $pdf->Cell(0, 4, 'Sample Report', 0, 1, 'C');
    $pdf->Ln(2);
        
        $pdf->Output();
        exit;
    }
}

类的代码

namespace App\Classes;

use Codedge\Fpdf\Fpdf\Fpdf;

class PDFClass extends Fpdf
{
    protected $B = 0;
    protected $I = 0;
    protected $U = 0;
    protected $HREF = '';

    // Page header
    function Header()
    {
        $this->SetFont('Arial', '', 11);
        $this->Cell(0, 2, 'Line 1', 0, 1, 'C');
        $this->Cell(0, 8, 'Line 2', 0, 1, 'C');
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(0, 1, 'Line 3', 0, 1, 'C');
        $this->Cell(0, 8, 'Line 4', 0, 1, 'C');
        $this->SetFont('Arial', '', 12);
        $this->Cell(0, 1, 'Line 5', 0, 1, 'C');
        $this->Ln(8);
    }
}

【问题讨论】:

    标签: laravel fpdf


    【解决方案1】:

    您创建了 2 个类实例。第一个是FPDF,在其中添加一个页面:

    $pdf = new FPDF();
    $pdf->AddPage('P', 'A4');
    $pdf->Ln(4);
    $pdf->SetFont('Arial', '', 12);
    

    ...然后您创建一个新的并简单地手动调用您的 Header() 方法:

    $pdfClass = new PDFClass();
    $header = $pdfClass->Header();
    

    这没有意义,此时会引发错误,因为您在 Header() 中调用了几个方法,这些方法应该将内容输出到页面,但您之前没有添加过。

    你应该只使用PDFClass,你也不应该手动调用Header()方法,因为它是called internally automatically。

    public function postPFTReport(Request $request)
    {
        $pdf = new PDFClass();
        $pdf->AddPage('P', 'A4'); // NOW THE HEADER() METHOD IS INVOKED AUTOMATICALLY IN THIS CALL
        $pdf->Ln(4);
        $pdf->SetFont('Arial', '', 12);
    
        $pdf->Cell(0, 4, 'Sample Report', 0, 1, 'C');
        $pdf->Ln(2);
        
        $pdf->Output();
        exit;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多