Codeigniter+tcpdf Plugin
This is how I integrated tcpdf and CI. First things first get your tcpfd class here. Extracted the following to the
system/plugins Folder
- tcpdf/cache Folder
- tcpdf/config Folder
- tcpdf/fonts Folder (kind of big almost 9MB)
- tcpdf/barcodes.php
- tcpdf/htmlcolors.php
- tcpdf/tcpdf.php
- tcpdf/unicode_data.php
Hoping that I know what I’m doing and moved on in creating the tcpdf_pi.php and saved it to system/plugins Folder.
<?Php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
function tcpdf(){
return new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);
}
?>
And on my controller I did this:
<?Php class Welcome extends Controller {
function Welcome(){
parent::Controller();
}
function index()
{
$this->load->plugin( 'tcpdf' );
$pdf = tcpdf();
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Nicola Asuni");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//initialize document
$pdf->AliasNbPages();
// add a page
$pdf->AddPage();
// ---------------------------------------------------------
// set font
$pdf->SetFont("dejavusans", "BI", 20);
// print a line using Cell()
$pdf->Cell(0,10,"Example 002",1,1,'C');
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output("example_002.pdf", "I");
}
}?>
A little bit of advice, this library has very little capability on converting HTML to PDF. DOMPDF still is my best bet.

about 1 year ago
Any idea to customize header and footer…
I want to place my own images in header and footer…
about 1 year ago
I am very sorry, I have no idea on how to do that, I only know a little about this tcpdf class. You can try dompdf. ^_^
about 1 year ago
You can either do it in tcpdf_config.php or in header() or footer() of class TCPDF.PHP
about 1 year ago
Hi, nice idea.
But if in Output you switch from I to F, it will not work, i try to understand why, but i was unable to do it.
about 1 year ago
Refer to the documentation:
http://www.tecnick.com/pagefiles/tcpdf/doc/com-tecnick-tcpdf/TCPDF.html#methodOutput
My guess, you’re gonna need to specify a correct path/filename for it and make sure that you can write to that directory.
about 1 year ago
How can I remove the line in the header part of the pdf… It seems to be useless for me and what if the line is not really required on the page… please someone help me.. tnx
about 1 year ago
refer to line 18 from the controller code of this post. $pdf->setPrintHeader(false); maybe this will help
about 1 year ago
@ikawka,
well thanks for the guide but i have already found the solution.. Just remove this code **$this->Cell(0, 0, ”, ‘T’, 0, ‘C’);** in the Header() function in tcpdf.php,,, that code would create a line after the header….
about 1 year ago
@ikawka, glad to have helped you buddy ^_^
about 1 year ago
@ikawka, no worries…
about 1 year ago
Hello Ikawaka, great job, thanks for this.
Paulo
about 11 months ago
Hi Ikawka,
Thanks for this useful info, but I am wondering how you can implement an example with
“class MYPDF extends TCPDF” (let’s say, example 11) into code igniter.
I tried but could not get it to work. Error: class TCPDF not found.
Thanks in advance.
about 11 months ago
I’ve never tried doing that, i think it doesn’t work because the class is not pre loaded, unlike the native CI classes that they are pre loaded. I’ll see what I can do bout this. ^_^
about 10 months ago
thanks for this.. it helped me out …
about 4 months ago
Hi,
This is very simple and straight forward.
It helped me a lot.
Thanks You
about 4 months ago
It took only two minutes to integrate TCPDF and start working.
Better place to tcpdf libraries is application/plugins instead of system/plugin.
Thanks,
Ravi