动作加载不止一次的TCpdf在Yii框架


action load more than once by TCpdf in Yii framework

我正在Yii框架中使用Tcpdf,(只是)当我在我的视图中创建新的Tcpdf以导出文件时,这导致我的动作加载两次或三次(我在我的动作中通过计数器检查了它),最后它工作并给我pdf文件。

我不希望这个动作加载几次,因为在动作中我通过一些规则改变了一些属性,这些规则会在控制器第一次加载后改变。

这个问题是在主机和在我的本地(xammp)这工作正常(我的服务器是Linux)

my action:

public function actionPrint_diploma($id)
{
    // check number of load this action :
    if(isset(Yii::app()->session['counter'])&&Yii::app()->session['counter']>=0)
        Yii::app()->session['counter']=Yii::app()->session['counter']+1;
    else    
        Yii::app()->session['counter']=0;
    // ...
    $this->render('coach_certificate');
}

my view (coach_certificate.php):

// Include the main TCPDF library (search for installation path).
$dir=Yii::getPathOfAlias('webroot').'/my_library/tcpdf/examples/tcpdf_include.php';
require_once($dir);
// 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->SetTitle('printing certificate');
$pdf->SetSubject('printing certificate');
$pdf->SetKeywords('PDF');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING);
// 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 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(FALSE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/far.php')) {
    require_once(dirname(__FILE__).'/lang/far.php');
    $pdf->setLanguageArray($l);
}
//$pdf->addTTFfont('/fonts/BZar.ttf', '', '', 32);
// ---------------------------------------------------------
// set font                                                          
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------

$pdf->AddPage();                                                                                                                 
$pdf->SetFont('freeserif', '', 10);   
$pdf->SetAbsXY(305,50);
$html = Yii::app()->session['counter'];
$pdf->writeHTML($html, true, false, true, true,'C');    
$pdf->lastPage();                                                                        
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output("test.pdf", 'D');                                         // 
Yii::app()->end();

你必须用两个动作来解决这个问题。

第一步:

在第一个操作中设置您的更改等…然后不要使用from render,而是使用redirect第二个动作

步骤2:

在第二个动作只是render您的视图文件。最后,如果它多次加载动作,它将加载第二个动作,并且不会多次调用更改函数。我希望它能正常工作

好好享受;-)