TCPDF | Generate PDF from HTML

|
| By Webner

Problem: FPDF which is a php class to generate pdf does not render many HTML tags like <style>,<img>. So it generates an unformatted pdf from HTML content.

Solution: TCPDF solves the problem of not rendering of HTML tags. It is a free and open source software for generating pdf. It is based on FPDF with some additional functionality and support. It overcomes limitations of FPDF as it is the only PHP based library that completely supports UTF_8 Unicode.

To use TCPDF, simply download it and include its tcpdf.php file in your project.

You can download it from https://github.com/tecnickcom/TCPDF

It can be included in a CakePHP project as:

App::import ( 'vendor', 'TCPDF-master', array 
(
'file' => 'TCPDF-master' . DS . 'tcpdf.php'
) );

For any PHP project:

require('tcpdf.php');

After including tcpdf file, it is just required to create the object of TCPDF class and call the methods of TCPDF class.

Object creation:

$pdf=new TCPDF();

Method Calling:

$pdf->WriteHTML();

Output:

It also provides Output method to output the generated pdf as required.

  1. $pdf->Output() : it simply displays the generated pdf.
  2. $pdf->Output(filename,’S’) : it allows the generated pdf to be sent as attachments in email.
  3. $pdf->Output(filename,’F’): it allows the generated pdf to be saved as local file.
  4. $pdf->Output(filename,’I’) : to send the pdf to browser.

 

Example of generating pdf with Html content by TCPDF:

$html=' <div style="width:800px; height:600px; padding:20px; text-align:center; border: 10px solid #787878">

<div style="width:750px; height:550px; padding:20px; text-align:center; border: 5px solid #787878">

<span style="font-size:50px; font-weight:bold">Certificate of Completion</span>

<br><br>

<span style="font-size:25px"><i>This is to certify that</i></span>

<br><br>

<span style="font-size:30px"><b>Student Name here</b></span><br/><br/>

<span style="font-size:25px"><i>has completed the course</i></span> <br/><br/>

<span style="font-size:30px">Course Name here</span> <br/><br/>

<span style="font-size:20px">with score of <b>Grade here</b></span> <br/><br/>

</div>

</div>';

$rostersCount = count ( $ html );

$rostersPerPage = 14;

$totalPages = ceil ( $rostersCount / $rostersPerPage );

$maxTitleLength = 18;

$filename = 'html' . time () . '.pdf';

$this->response->header ( array (

'Content-Type' => 'application/pdf',

'Content-Disposition' => 'attachment; filename=' . $filename

) );

$this->autoRender = false;

$this->layout = 'pdf';

$pdf = new TCPDF ( 'P', 'mm', 'A4' );

$count = 0;

$pageNumber = 1;

while ( $pageNumber <= $totalPages )

{

$pdf->AddPage ( 'L' );

$bMargin = $pdf->getBreakMargin();

$auto_page_break = $pdf->getAutoPageBreak();

$pdf->SetAutoPageBreak(false, 0);

$pdf->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);

$pdf->SetAutoPageBreak($auto_page_break, $bMargin);

$pdf->setPageMark();

$storeNameTitle = Configure::read ( 'storeNameTitle' );

$pdf->WriteHTML($html);

$pageNumber ++;

}

$pdf->Output ();

The output will be as follows: 

1

 

Leave a Reply

Your email address will not be published. Required fields are marked *