php - in yii2 on using TCPDF library i am getting "Failed to load PDF document." error

I am trying to generate example pdf file as it is shown here: https://packagist.org/packages/hbalkhi/yii2-pdf.

output ---> this is the out put.

myController

public function actionTcpdf()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
    Yii::$app->response->headers->add('Content-Type','application/pdf');

        return $this->render('tcpdf');
    }

View

<?php

/**
 * @copyright Copyright &copy;2014 Giandomenico Olini
 * @company Gogodigital - Wide ICT Solutions 
 * @website http://www.gogodigital.it
 * @package yii2-tcpdf
 * @github https://github.com/cinghie/yii2-tcpdf
 * @license GNU GENERAL PUBLIC LICENSE VERSION 3
 * @tcpdf library 6.0.075
 * @tcpdf documentation http://www.tcpdf.org/docs.php
 * @tcpdf examples http://www.tcpdf.org/examples.php
 */
// echo "12345";
// exit();

// Load Component Yii2 TCPDF 
\Yii::$app->get('tcpdf');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Gogogital.it');
$pdf->SetTitle('Yii2 TCPDF Example');
$pdf->SetSubject('Yii2 TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Yii2 TCPDF Example', 'Gogodigital - Wide ICT Solutions | gogodigital.it', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//

this is the view code. I have been echoing at different places and it works fine but when i echo in view where m making a new variable for pdf in start the echo doesn't comes but the error.

Answer

Answer

Answer

Answer

Answer

------- // set default font subsetting mode $pdf->setFontSubsetting(true); // Set font // dejavusans is a UTF-8 Unicode font, if you only need to // print standard ASCII chars, you can use core fonts like // helvetica or times to reduce file size. $pdf->SetFont('dejavusans', '', 14, '', true); // Add a page // This method has several options, check the source code documentation for more information. $pdf->AddPage(); // set text shadow effect $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal')); // Set some content to print $html = "<h1>Yii2 TCPDF Works Fine!</h1>"; // Print text using writeHTMLCell() $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); //

Answer

Answer

Answer

Answer

Answer

------- // Close and output PDF document // This method has several options, check the source code documentation for more information. $pdf->Output('yii2_tcpdf_example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ // Close Yii2 \Yii::$app->end(); ?>

Source