Welcome Guest, Not a member yet? Register   Sign In
Class not found trying to integrate TCPDF
#1

(This post was last modified: 09-01-2021, 03:43 PM by SoccerGuy3.)

Following the instructions here: https://forum.codeigniter.com/thread-754...#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!
Reply
#2

Read this one also FIXED.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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.
Reply
#4

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;  
Reply
#5

(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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB