1
votes

I am trying to implement tcpdf library in codeigniter but getting error

Fatal error: Cannot redeclare class Pdf in C:\xampp\htdocs\project\system\libraries\Pdf.php on line 6

here code example

path : "project\system\libraries\Pdf.php"

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';

class Pdf extends TCPDF
{
 function __construct()
 {
 parent::__construct();
 }
}

/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */

also Download latest TCPDF

also want to know which is best TCPDF or DOMPDF to generate quick PDF ?

2
I like tcpdf :) - Barry

2 Answers

0
votes

When using/making custom libraries, smart move would be (as CI makers advise too) to put it in APPPATH.'libraries/' because of upgrading version could overwrite/delete added files from system directory. That is why application folder is used for - to make any kind of custom files related to CI framework not touching default files/directories.

Other than that, error you've got there says there is more than one or better say there is two classes with same name. PHP doesn't allow that and every used file and/or class must have unique name. Possible solution should be renaming your file to

BASEPATH.'Pdf_lib.php'

and class name accordingly.

But again, if you are not limited with something else, move your class to

APPPATH.'libraries/Pdf_lib.php'

with following directories and subdirectories and files of tcpdf and use it from there.

0
votes

I fix this error using bellow method. its happen coz same class is call from other TCPDF included file here's the solution

path : "project\system\libraries\Pdf.php"

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once  '/tcpdf/tcpdf.php';

if (!class_exists('Pdf')) {
class Pdf extends TCPDF
{
    function __construct()
    {
        parent::__construct();
    }
}
}