Monday 28 April 2014

Create Pdf file using Tcpdf plugin in php Yii framework

Create Pdf file using Tcpdf plugin in php Yii framework

public function actionCreatePdf() {
        $path = Yii::app()->basePath;
        require_once($path . '/extensions/tcpdf/tcpdf.php');
        $pdf = new TCPDF();
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Nicola Asuni');
        $pdf->SetTitle('TCPDF Example 001');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
        $pdf->SetHeaderData('', 0, PDF_HEADER_TITLE, '');
        $pdf->setHeaderFont(Array('helvetica', '', 8));
        $pdf->setFooterFont(Array('helvetica', '', 6));
        $pdf->SetMargins(15, 18, 15);
        $pdf->SetHeaderMargin(5);
        $pdf->SetFooterMargin(10);
        $pdf->SetAutoPageBreak(TRUE, 0);
        $pdf->SetFont('dejavusans', '', 7);
        $pdf->AddPage();
        $pdf->SetFillColor(255, 0, 0);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(128, 0, 0);
        $pdf->SetLineWidth(0.1);
        $pdf->SetFont('');
        // Header
        $tbl_header = '<table border="1">';
        $tbl_footer = '</table>';
        // Data        
        $tbl = '';

        $sql = "select * from student";
        $data = Yii::app()->db->createCommand($sql)->queryAll(); 

        for ($i = 0; $i < count($data); $i++) {
            $tbl .= '<tr>' .
                    '<td>' . $data[$i]['name'] . '</td>' .
                    '<td>' . $data[$i]['age'] . '</td>' .
                    '<td>' . $data[$i]['mark1'] . '</td>' .
                    '<td>' . $data[$i]['mark2'] . '</td>' .
                    '<td>' . $data[$i]['mark3'] . '</td>' .
                    '</tr>';
        }

        $pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
        $pdf->Output("Student_Details.pdf", "D");
        Yii::app()->end();
    }

Find more examples related to phpexcel from here

Related Links:

Create Excel File with PHPExcel Plugin Yii Framework

CGridView display data from database in Yii PHP Framework

3 comments:

  1. "Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
    "

    ReplyDelete
  2. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Hire Yii Developers in India

    ReplyDelete