1

I got some SVG-Charts and now I want to be able to download them as a PDF-File from my website. To do so I want to use TCPDF. I have required the file of the library and give out the used path, also I used "class_exists('TCPDF')". Both return the right value. Still, I get the following error:

Fatal error: Uncaught Error: Class 'Kanboard\Controller\TCPDF' not found in C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php:172 Stack trace: #0 C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php(117): Kanboard\Controller\ExportController->downoadPDF(Array) #1 C:\xampp\XAMPP\htdocs\fftboard\app\Core\Controller\Runner.php(77): Kanboard\Controller\ExportController->summaryPDF() #2 C:\xampp\XAMPP\htdocs\fftboard\app\Core\Controller\Runner.php(31): Kanboard\Core\Controller\Runner->executeController() #3 C:\xampp\XAMPP\htdocs\fftboard\index.php(13): Kanboard\Core\Controller\Runner->execute() #4 {main} thrown in C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php on line 172

This is strange, since the class is not 'Kanboard\Controller\TCPDF' but 'Kanboard\Analytic\TCPDF'. Has this something to do with the namespace? I do not know, why it can not find the class, if the file is required correctly. Some code of the file:

namespace Kanboard\Controller;
class ExportController extends BaseController
{
  public function downoadPDF($project)
  {
    $tcpdfPath = realpath( dirname( __FILE__ ).'/../../libs/tcPDF/TCPDF-master/TCPDF-master/tcpdf.php');
    require_once $tcpdfPath;
    $log .=  class_exists('TCPDF');
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
  }
}

In "PHP class not found but it's included" the Problem is not solved. The answer marked as solutin just tells you to check if the File exists and it does. The other solutions suggests to use <?php instead of <?, which I already did bevore and to check if the Class exists, which it did, as I said above.

Can somebody explain to me why this error appears?

SarahK
  • 59
  • 8
  • Possible duplicate of [PHP class not found but it's included](https://stackoverflow.com/questions/8490667/php-class-not-found-but-its-included) – LF00 Sep 16 '19 at 12:09
  • @Kris Roofe the Problem seems quite similar, but the answers do not help me, since I tried them without success. – SarahK Sep 16 '19 at 13:27
  • 1
    Can you not use composer and this? https://github.com/tecnickcom/TCPDF Then you can autoload the TCPDF library – Chris Hemmens Sep 16 '19 at 13:30
  • @Chris Hemmens I can not download it, because it is blockt from a Internet-security progrmm of my employer. – SarahK Sep 16 '19 at 13:39

1 Answers1

1

When you declare:

namespace Kanboard\Controller;

all partially-qualified class names will be taken relative to that name. Your code here:

new TCPDF(...)

uses a partially-qualified class name -- since it doesn't start with a \ -- so, it's resolved to Kanboard\Controller\TCPDF.

You've stated it should be Kanboard\Analysis\TCPDF, but I'm not so sure based on how the code is written. Try updating your code to either of these:

new \TCPDF(...)
new \Kanboard\Analysis\TCPDF(...)

Replace the ... with the actual arguments. If the first works, then TCPDF is in the root namespace (which I suspect). If the second works, then it's in the namespace you stipulate.

bishop
  • 34,858
  • 10
  • 96
  • 130