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.






Any idea to customize header and footer…
I want to place my own images in header and footer…
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. ^_^
You can either do it in tcpdf_config.php or in header() or footer() of class TCPDF.PHP
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.
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.
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
refer to line 18 from the controller code of this post. $pdf->setPrintHeader(false); maybe this will help
@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….
I think it would be much better to create an overridden Header method in your class that extends the original TCPDF class.
@ikawka, glad to have helped you buddy ^_^
@ikawka, no worries…
Hello Ikawaka, great job, thanks for this.
Paulo
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.
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. ^_^
thanks for this.. it helped me out …
Hi,
This is very simple and straight forward.
It helped me a lot.
Thanks You
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
I had a really strange issue. When the test controller is run from within the application/controllers folder the resulting PDF has no filename? eg. There is no PDF title and when you go to save the filename is the URL of the controller, not the PDF filename. Outside of these folders eg Web Root the filename has no problem. Anybody have any thoughts on that?
Hi, can you post parts of your code here? so that i can help. tnx
The code is exactly the same in both cases and not different from your configuration. When it is called from a controller within the application. It has no filename, outside the application folder (exact same code) it works. Strange?
well if you really want to reuse the function, best practice on my point of view is convert it into a library with the filename as parameter, also make sure that the path you are saving the file to is writable. since you wont post how exactly did you use the function inside the controller; it is difficult for me to trouble shoot.
I wonder if [$pdf->Output("example_002.pdf", "F");] works?
Greetings.
Help to understand with TCPDF (the library of generation PDF of files), for me has arisen the following problem:
By an example 006 from a site of developer TCPDF has created page and a variable $view I assign a value in which it is stored html a code of construction of the table then I call перемеменную $view for creation pdf the document by means of function TCPDF writeHTML.
It is as a result generated empty стрнаица format PDF.
What to do? Where an error?
If a variable $view directly to assign all code HTML of the table as a string variable (i.e. $view = ‘
‘ that all is fulfilled for few seconds.
Hello World!!!
Greetings.
Help to understand with TCPDF (the library of generation PDF of files), for me has arisen the following problem:
By an example 006 from a site of developer TCPDF has created page and a variable $view I assign a value in which it is stored html a code of construction of the table then I call перемеменную $view for creation pdf the document by means of function TCPDF writeHTML.
It is as a result generated empty стрнаица format PDF.
What to do? Where an error?
If a variable $view directly to assign all code HTML of the table as a string variable (i.e. $view = ‘<table>
<tr> <td> Hello World!!! </td> </tr> </table>) ‘ that all is fulfilled for few seconds.