Welcome Guest, Not a member yet? Register   Sign In
Use of FPDF library in CI4

Hello guys,
As I need fpdf in my project, I have installed it thru seatsign/fpdf found in packagist.org, using :

composer require setasign/fpdf:^1.8

It is now present in the vendor\setasign\fpdf directory of my app

To get it in my project, I have put a file MyFpdfClass.php in \APP\Libraries containing :
namespace App\Libraries;
use \setasign\fpdf\fpdf;
class MyFpdfClass extends Fpdf

but the call in controller $t = new MyFpdfClass() send an error :

Class "setasign\fpdf\fpdf" not found
APPPATH/Libraries/MyFpdfClass.php at line 13

How can I set the "use" so that the library is found ?
Is it a problem with autoloader not correctly set ?

Thanks for your help

Use modern IDE for development. Have autoimport namespaces

PHP Code:

namespace App\Libraries;


MyFpdfClass extends FPDF


if you integrated seatsign/fpdf using composer, then to use the classes of that package in a controller it would be in the form of :

use  fullNamespaceName\classname   //Youwant to use

To get the namespace open up one of the classes with a text editor.
For instance i used composer to integrate PHPMailer into CI4

opening up one of the classes i saw they used the namespace

I noticed there were few classes , of which one class of PHPMailer might need to use another, so in my controller, where I will instantiate i wrote
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

then in the same controller to instantiate it was just :

$mail = new PHPMailer(true);

Now if you used composer to install , CI should know exactly where classes are , via autoload. So you should have to shift things about or mess with autoload class. Previously i manually put in PHPMailer at root level ; in that case I did have to make use of autoload.php and the section

public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
        'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src',

The point as i understand it is that with composer , if the classes integrated in vendor , DO HAVE a namespace in the classes, then you shouldn't have to do anything excpet make use of "use"
CMS CI4     I use Arch Linux by the way 


This helps ! Thank you so much !!

I have found that mPdf is much easier to configure and to use. Install via composer.

Especially where you already have the output in html/css and just need to output as a pdf.

Give it a look. The docs can be found here https://mpdf.github.io/
Dirk B.
Abatrans Software
No SEO spam - see forum guidelines

i got this one working on one of my old sites : https://packagist.org/packages/tecnickcom/tcpdf
CMS CI4     I use Arch Linux by the way 


I want use fpdf in my project CI4.. how i can do this step by step? How include it as thirdparty?!

<?php namespace App\Controllers\Purchase;

namespace App\ThirdParty\fpdf;

use CodeIgniter\Controller;

use FPDF;
and how build the controller to call fpdf methods?!

$pdf = new PDF();

Theme © iAndrew 2016 - Forum software by © MyBB