Yii Framework 1.0 Tutorial

HTML TO PDF

This post will help you generate a pdf files in yii framework. I used tcpdf to create a pdf file. Pdf file based on html page. What you see in html page, You can download as pdf using this.

Download TCPDF

Download the tcpdf files using below link to generate html to PDF files. Download HTML2PDF Extract the download files and you will get following folders and files Folder
FOLDER:
_class     
_tcpdf_5.0.002
locale

FILES:
html2pdf.class

Create HTML2PDF Extension

After extracted folder, Create new folder inside extension of yiiframework as "tcpdf". Copy above files (html2pdf.class) and folders (_class, _tcpdf_5.0.002, locale) and paste it into "tcpdf" folder. "html2pdf.class" has contains "HTML2PDF" class. I rename "html2pdf.class" file name as "HTML2PDF" (For my use)

Controller: HtmltopdfController.php


<?php
class HtmltopdfController extends Controller
{    
	public function actionIndex()
	{
		$this->render('index');
	}
    public function actionPdf()
	{
		$this->renderPartial('html2pdf');
	}
}
?>

View: html2pdf.php


<?php
    ob_start();
    echo $this->renderPartial('htmlpage'); 
    $content = ob_get_clean();

    Yii::import('application.extensions.tcpdf.HTML2PDF');
    try
    {
        $html2pdf = new HTML2PDF('P', 'A4', 'en');
//      $html2pdf->setModeDebug();
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content,false);
        $html2pdf->Output("pdfdemo.pdf");
        
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }
?>

View: htmlpage.php



<style type="text/css">
h1{
    color:red;
    text-align: center;
}
table{
    margin: auto;
    border: 1px solid gray;
}
table td,table th{
    border: 1px solid gray;
    border-collapse: collapse;
    padding: 5px;
}
</style>
<page>
<div>
<img src="http://static.yiiframework.com/files/logo/yii.png" title="yii logo" />
<h1>
YII HTML TO PDF DEMO
</h1>
</div>
<br /><br /><br />
<table>
    <tr><th>TITLE</th><th>LINK</th></tr>
    <tr><td>BSOURCECODE</td><td><a href="http://www.bsourcecode.com/">http://www.bsourcecode.com</a></td></tr>
    <tr><td>DEMO BSOURCECODE</td><td><a href="http://demo.bsourcecode.com/">http://demo.bsourcecode.com</a></td></tr>
    <tr><td>YII FRAMEWORK</td><td><a href="http://www.yiiframework.com">http://www.yiiframework.com</a></td></tr>
</table>
</page>    
  • ijaz

    Hai

    I know about tcpdf working style and tcpdf is multiple language supported and dynamic data storage. But i want to create custom radio buttons, text fields and check boxes and i have experience tcpdf integrating with YII.

    I have a example pdf how i want so if any one clear about tcpdf completely please give their e-mail ids then i will send my example pdf. and can improve later discussions.

    Thanks & Regards,
    Akram.

  • Yessie Setiawan

    why i keep get “No data received” when trying to display pdf page?

    • m bala

      Please post your code.

    • jagdish sonic

      require(‘html2pdf_sample/html2fpdf.php’);

      $pdf=new HTML2FPDF();

      $pdf->AddPage();

      $pdf->WriteHTML($text_content);

      $pdf->Output(“upload_pdf/”.$file_name);

      echo “PDF file is generated successfully!”;

  • jagdish sonic

    i ask to need pdf generate file save and view help me