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 ?
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.
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();
}
}
}
Related
I am trying to use a third party library for openfire in my Codeigniter Application.
So I have put the libarary in third party folder, and have created an index.php file where I configure my Library.
Then I created a class file in library folder called index.php and I call the third party library like this:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Open extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
require_once APPPATH.'third_party/openfire/index.php';
}
}
Finally created a controller called user.php and tried to load this libaray using :
$this->load->library('Open');
But on screen this shows me error:
Unable to locate the specified class: Session.php
Its an unexpected error! what can be the possible reasons for this ? If i stop load this library everything works fine.
And I have already loaded Session in autload.php
class Open extends MY_Controller
Try not extending the MY_Controller
I need to create custom libraries for my Codeigniter 3 application that have common functions used by all my custom library.
How to use function only in one place?
What you appear to need is a helper file, not a library.
Create a file in application/helpers directory
Eg file : mycommon_helper.php
Contents as :
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
function my_function($param1) {
printf('Hello %s', $param1);
}
Include this in the autoload file as below
$autoload['helper'] = array('url','html', .....'mycommon'); // Note _helper is omitted
After this you use the my_function($param1) anywhere in your code
$sayIt = my_function('user2473015');
You can create new custom libraries.
The library classes created by you should be placed in your application/libraries folder.
Classes should have basic prototype like this (here the name Myclass as an example):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Myclass {
public function my_function()
{
}
}
/* End of file Myclass.php */
From within any of your Controller functions you can initialize your class using the standard:
$this->load->library('myclass');
$this->myclass->my_function(); // Object instances will always be lower case
I am trying to add Smarty to CodeIgniter, here are the steps that I did:
Downloaded CI
Downloaded Smarty and put its content in 'application/third_party/smarty' folder
Created 'smarty.php' file in 'application/libraries'
Created 'templates' & 'templates_c' folders inside 'application/views' folder
Created simple 'test.tpl' file in 'application/views/templates' folder
Opened 'autoload.php' in 'application/config' folder and added:
$autoload['libraries'] = array('smarty');
Inside a controller wrote $this->smarty->display('test.tpl';
And I get this error - Message: Unable to load template file 'test.tpl'
After some debugging I have noticed that my Smarty template dir is set to default, and not the one that is specified in the bridging class ('application/libraries/smarty.php')
Here is the bridging class content:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'third_party/smarty/Smarty.class.php');
class CI_Smarty extends Smarty {
function __construct()
{
parent::__construct();
// NOT Changig this for some reason
$this->setTemplateDir(APPPATH.'views/templates/');
$this->setCompileDir(APPPATH.'views/templates_c/');
}
}
I think the CI_Smarty class isn't being executed for some reason, thats why my template directory is set to default
also note that if I go directly into Smarty.class.php and change the template directory manually - everything will work
Figured out the problem - bridging class should have been in 'system/libraries' and not in 'application/libraries' folder.
The problem is that you need to load ci_smarty library which extends smarty, not smarty directly.
$autoload['libraries'] = array('ci_smarty');
In your controller,
$this->ci_smarty->display('test.tpl');
Important
Please note that all native CodeIgniter libraries are prefixed with
CI_ so DO NOT use that as your prefix.
Try to change library name to custom_smarty instead.
Last but not least
Don't touch the system directory for any reason, Never ever. It is not the best practice. You can easily create new one or extend the existing libraries in your application/libraries directory.
Hope it will be useful for you.
Futher, if you like or need to use as
$this->smarty->display('test.tpl');
First, please CLONE the 'ci_smarty' object into 'smarty' on the Controller __Consructor() function
$this->smarty = clone $this->ci_smarty;
I have a HTML form data getting from database.
I use PHP framework codeigniter . we want generate a PDF file from this HTML form data. tell me how to do.
You may wanna look at html2Pdf. It is a php class based on FPDF and it allows you to create PDF file from HTML. So just format your text using html and then create its pdf. Its very flexible and give greate control.
SorceForge Link
Or You can also look at this: Convert HTML + CSS to PDF with PHP?
Look at DomPDF https://github.com/dompdf/dompdf
Examples: http://pxd.me/dompdf/www/examples.php
By the way, on my project I use Zend Framework and ZendParadoxPDF to ge generate pdf. But it requires Java, so ASAP I'll redevelop it using DomPDF.
You can also use mPDF Library to create pdf in codeignator. create the mpdf.php file. After that put it into the CodeIgniter library directory.
copy this code to mpdf.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once APPPATH.'/third_party/mpdf/mpdf.php';
class M_pdf {
public $param;
public $pdf;
public function __construct($param = "'c', 'A4-L'")
{
$this->param =$param;
$this->pdf = new mPDF($this->param);
}
}
Then, download mPDF Library from github or mpdf website.After downloading, Extract it and put the mpdf folder into CodeIgniter third_party directory.Then,
Open your controller(here,i am using Welcome controller) ,create a method on that controller as following
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function my_mPDF(){
$filename = time()."_order.pdf";
$html = $this->load->view('unpaid_voucher',$data,true);
// unpaid_voucher is unpaid_voucher.php file in view directory and $data variable has infor mation that you want to render on view.
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);
//download it D save F.
$this->m_pdf->pdf->Output("./uploads/".$filename, "F");
}
}
?>
Here, my_ mPDF function of Welcome controller will generate pdf file of common/template view file.
It’s done. I hope it will help you.
I am trying to use PHPWord and having a difficulties to use it.
This my code within application/libraries/words.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PhpWord/Autoloader.php";
\PhpOffice\PhpWord\Autoloader::register(); \\NOT SURE IF THIS IS CORRECT
class Word extends PhpWord {
public function __construct() {
parent::__construct();
}
}
Based on PHPWord github, i must use it like this:
Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.
require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); // WHERE SHOULD I PUT THIS?
When i try to load the library from my controller as such:
$this->load->library('word');
it gives me error that says:
Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7
i tried to extends the Autoloader class, but PHP still complains that it can't found Autoloader.php.
when i do
file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php
it returns 1.
Does anyone know how to fix this?
From Russia with love;)
Use like this in library for CodeIgniter:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/libraries/PHPWord/src/PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();
class Word extends Autoloader {
}
?>
In controller like code from samples:
$this->load->library('word');
/* remove ;)
#require_once 'PHPWord/src/PhpWord/Autoloader.php';
#PhpOffice\PhpWord\Autoloader::register();
*/
// Template processor instance creation
echo date('H:i:s') , ' Creating new TemplateProcessor instance...' ;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('text.docx');
$templateProcessor->setValue('sss','123');
echo date('H:i:s'), ' Saving the result document...';
$templateProcessor->saveAs('test1.docx');
Tested it works!