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

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 :
<?php
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
Eric
Reply
#2

Use modern IDE for development. Have autoimport namespaces

PHP Code:
<?php

namespace App\Libraries;

use 
FPDF;

class 
MyFpdfClass extends FPDF
{


Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

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 :

Code:
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
Code:
PHPMialer\PHPMailer

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
Code:
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 :

Code:
$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

Code:
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"
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#4

This helps ! Thank you so much !!
Reply
#5

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.
ESD Digital
No SEO spam - see forum guidelines
Reply
#6

i got this one working on one of my old sites : https://packagist.org/packages/tecnickcom/tcpdf
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#7

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();
$pdf->AddPage();
thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB