GstiAld
GstiAld

Reputation: 17

How to fix 'Missing argument 1 for Pdf_label::__construct()' when using fpdf label script in codeigniter

Im getting error :

Message: Missing argument 1 for Pdf_label::__construct(), called in C:\xampp\htdocs\project1b\system\core\Loader.php on line 1285 and defined

Filename: libraries/PDF_Label.php

what I'm trying to do is, I tried to using fpdf label script (Documentation Here) in codeigniter, I already tried a simple pdf generate in codeigniter using this code :

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();
    }

and it worked, but when I tried to add script (in this case label script) I put the label script in the same directory like fpdf file does (application/libraries) and generate pdf using this example code

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();
}

I'm getting like on the first message, I really appreciate it if someone can show what I did wrong on this case.

Upvotes: 0

Views: 491

Answers (1)

Alex
Alex

Reputation: 9265

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

Upvotes: 1

Related Questions