首页 > 解决方案 > 在 codeigniter 中使用 fpdf 标签脚本时如何修复“Pdf_label::__construct() 缺少参数 1”

问题描述

我收到错误:

消息:Pdf_label::__construct() 缺少参数 1,在第 1285 行的 C:\xampp\htdocs\project1b\system\core\Loader.php 中调用并定义

文件名:库/PDF_Label.php

我想要做的是,我尝试在 codeigniter 中使用 fpdf 标签脚本(此处的文档),我已经尝试使用以下代码在 codeigniter 中生成一个简单的 pdf:

public function cetakLabelfpdf()
    {
        $this->load->library('fpdf');

        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(40, 10, 'Hello World!');
        $pdf->Output();
    }

它起作用了,但是当我尝试添加脚本(在本例中为标签脚本)时,我将标签脚本放在与 fpdf 文件(应用程序/库)相同的目录中,并使用此示例代码生成 pdf

public function cetakLabelfpdf()
{
    $this->load->library('PDF_Label');

    // Standard format
    $pdf = new PDF_Label('L7163');

    $pdf->AddPage();

    // Print labels
    for ($i = 1; $i <= 20; $i++) {
        $text = sprintf("%s\n%s\n%s\n%s %s, %s", "Laurent $i", 'Immeuble Toto', 'av. Fragonard', '06000', 'NICE', 'FRANCE');
        $pdf->Add_Label($text);
    }

    $pdf->Output();
}

我很喜欢第一条消息,如果有人能证明我在这个案子上做错了什么,我真的很感激。

标签: phpcodeigniterfpdf

解决方案


To add to my comment, essentially $this->load is meant to work with CodeIgniter compatible libraries/models/helpers .etc. When you have something completely unrelated to CodeIgniter (not built around its ecosystem) you can either create a library to "adapt" the class to be compatible with CodeIgniter or you can just use it like a regular class with either composer autoloading or requiring the necessary files at the top of the controller/model class that needs it (won't work for namespaced classes - you'd then need composer or something that can autoload).

In your specific case, when you called $this->load->library() on the label class, CI created a new label class (behind the scene) and didn't pass anything to its __construct where there is a required param. Hence the error. You can pass variables to a libraries constructor via $this->load->library('some_lib', ['arg1'=>'foo', 'arg2'=>'bar'] however that is only if the library is built for CI (receives all constructor arguments in an array rather than a comma separated parameter list).

See more here: https://www.codeigniter.com/user_guide/general/creating_libraries.html#passing-parameters-when-initializing-your-class


推荐阅读