CodeIgniter Forums
Class not found trying to integrate TCPDF - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Class not found trying to integrate TCPDF (/showthread.php?tid=80009)



Class not found trying to integrate TCPDF - SoccerGuy3 - 09-01-2021

Following the instructions here: https://forum.codeigniter.com/thread-75463-post-377974.html#pid377974

I setup TCPDF, but upon running I am getting an error. As best I can tell I followed the instructions exactly (except for a slight name change). What am I missing?

Unzipped the file downloaded from GitHub into App/ThirdParty/TCPDF

Added a file (SBPdf.php) into App/Libraries:
PHP Code:
<?php

 
namespace App\Libraries;

 use 
App\ThirdParty\TCPDF\Tcpdf;

 class 
SBPdf extends TCPDF {
 } 

Then in my controller I call it:
PHP Code:
public function testpdf()
 {
 
$pdf = new SBPdf(PDF_PAGE_ORIENTATIONPDF_UNITPDF_PAGE_FORMATtrue'UTF-8'false);

etcetc.... 

At that point I was getting an error: 'Class "App\Controllers\Admin\SBPdf" not found'

I then added this to my controller (app/Controllers/Admin/Services.php), which got me closer, I think:
PHP Code:
use App\Libraries\SBPdf

But this produces an error of: 'Class "App\ThirdParty\TCPDF\Tcpdf" not found' error.

Feel like I am missing something obvious as others have said it just works, so... Help!


RE: Class not found trying to integrate TCPDF - InsiteFX - 09-02-2021

Read this one also FIXED.


RE: Class not found trying to integrate TCPDF - SoccerGuy3 - 09-02-2021

(09-02-2021, 04:34 AM)InsiteFX Wrote: Read this one also FIXED.

Not sure I understand. I don't see anything in that post about the Class not being found.


RE: Class not found trying to integrate TCPDF - iRedds - 09-02-2021

You are copying code you don't understand and you get an error. And these are the basics of PHP.
PHP Code:
use App\ThirdParty\TCPDF\Tcpdf
This line is trying to import the class and

1. The class file must be reachable through the path App/ThirdParty/TCPDF/Tcpdf.php
2. The class must be in the specified namespace (App\ThirdParty\TCPDF).

If you haven't guessed yet, then the shared library will not have the App\ThirdParty\TCPDF namespace.
That's why you get your error.

There are 2 simple solutions.

CodeIgniter way

1. Unpack the library from github to ThirdParty/TCPDF
2. Change config App/Config/Autoload. 
Add the line to the $classmap property
PHP Code:
public $classmap = [
 
    'TCPDF' => APPPATH '/ThirdParty/TCPDF/tcpdf.php',
    ]; 

3. Import the library into your controller.
PHP Code:
<?php 
namespace Your\Controller\Namespace
use TCPDF;  
Now you can create an instance of the class.

The easy way

1.Install the library with composer
Code:
composer require tecnickcom/tcpdf

2. Import the library into your controller.
PHP Code:
<?php 
namespace Your\Controller\Namespace
use TCPDF;  



RE: Class not found trying to integrate TCPDF - SoccerGuy3 - 09-02-2021

(09-02-2021, 11:06 AM)iRedds Wrote: You are copying code you don't understand and you get an error. And these are the basics of PHP.
PHP Code:
use App\ThirdParty\TCPDF\Tcpdf
This line is trying to import the class and

1. The class file must be reachable through the path App/ThirdParty/TCPDF/Tcpdf.php
2. The class must be in the specified namespace (App\ThirdParty\TCPDF).

If you haven't guessed yet, then the shared library will not have the App\ThirdParty\TCPDF namespace.
That's why you get your error.

There are 2 simple solutions.

CodeIgniter way

1. Unpack the library from github to ThirdParty/TCPDF
2. Change config App/Config/Autoload. 
Add the line to the $classmap property
PHP Code:
public $classmap = [
 
    'TCPDF' => APPPATH '/ThirdParty/TCPDF/tcpdf.php',
    ]; 

3. Import the library into your controller.
PHP Code:
<?php 
namespace Your\Controller\Namespace
use TCPDF;  
Now you can create an instance of the class.

The easy way

1.Install the library with composer
Code:
composer require tecnickcom/tcpdf

2. Import the library into your controller.
PHP Code:
<?php 
namespace Your\Controller\Namespace
use TCPDF;  

Totally admit I don't fully understand CodeIgniter's methodology, but trying to learn. The autoload part seems to be what would have been needed - but was missing from the previous posts instructions. With that said, About 30 minutes ago I dug into composer and how it works and installed TCPDF via composer. Lo and behold, it just worked! Smile Guess in the future I shouldn't be scared of composer (never used it before because I hadn't taken the time to figure what it does and how it impacts what I do).

So your second solution is what I did and it worked. Thanks for replying! Us newbies appreciate it!


RE: Class not found trying to integrate TCPDF - getsamplecode - 11-25-2024

Kindly check this TCPDF Integration in Codeigniter 4

https://getsamplecode.com/blog/generate-pdf-files-in-codeigniter-4-using-tcpdf