Leo Leo - 3 months ago 51x
PHP Question

include(MYPDF.php): failed to open stream: No such file or directory yii tcpdf

I am trying to integrate pdf by following the below link


But got an error like this:

include(MYPDF.php): failed to open stream: No such file or directory

any clue?


Just do it step by step:

1) Download TCPDF from here.

2) Put files from folder TCPDF-master from ZIP archive to protected/extensions/tcpdf.

3) Create MYPDF class in protected/extensions:

 * @abstract This Component Class is created to access TCPDF plugin for generating reports.
 * @example You can refer http://www.tcpdf.org/examples/example_011.phps for more details for this example.
 * @todo you can extend tcpdf class method according to your need here. You can refer http://www.tcpdf.org/examples.php section for 
 *       More working examples.
 * @version 1.0.0
class MYPDF extends TCPDF {

    // Load table data from file
    public function LoadData($file) {
        // Read file lines
        $lines = file($file);
        $data = array();
        foreach($lines as $line) {
            $data[] = explode(';', chop($line));
        return $data;

    // Colored table
    public function ColoredTable($header,$data) {
        // Colors, line width and bold font
        $this->SetFillColor(255, 0, 0);
        $this->SetDrawColor(128, 0, 0);
        $this->SetFont('', 'B');
        // Header
        $w = array(40, 35, 40, 45);
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        // Data
        $fill = 0;
        foreach($data as $row) {
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
        $this->Cell(array_sum($w), 0, '', 'T');

4) Add import of MYPDF class at top of your controller (after <?php). Create action in your controller to generate PDF (based on example in tutorial from link that you posted):


    Yii::import('ext.MYPDF'); //here is import
    class ReportController extends Controller
        $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information

        $pdf->SetTitle("Selling Report -2013");
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "Selling Report -2013", "selling report for Jun- 2013");
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->SetFont('helvetica', '', 8);

        //Write the html
        $html = "<div style='margin-bottom:15px;'>This is testing HTML.</div>";
        //Convert the Html to a pdf document
        $pdf->writeHTML($html, true, false, true, false, '');

        $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)'); //TODO:you can change this Header information according to your need.Also create a Dynamic Header.

        // data loading
        $data = $pdf->LoadData(Yii::getPathOfAlias('ext.tcpdf').DIRECTORY_SEPARATOR.'examples'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'table_data_demo.txt'); //This is the example to load a data from text file. You can change here code to generate a Data Set from your model active Records. Any how we need a Data set Array here.
        // print colored table
        $pdf->ColoredTable($header, $data);
        // reset pointer to the last page

        //Close and output PDF document
        $pdf->Output('filename.pdf', 'I');