gpt4 book ai didi

php - Yii 框架中的 TCpdf 多次加载 Action

转载 作者:可可西里 更新时间:2023-10-31 23:40:21 24 4
gpt4 key购买 nike

我在 Yii 框架中使用 Tcpdf,(只是)当我在我的 View 中创建新的 Tcpdf 以导出文件时,这导致我的操作加载了两到三次(我在操作中通过计数器检查了它)终于成功了,给我我的 pdf 文件。

我不想多次加载此操作,因为在操作中我正在根据某些规则更改某些属性,并且此规则将在 Controller 第一次加载后更改。

这个问题出在主机和我的本地(xammp)中,它工作正常(我的服务器是 linux)

我的行动:

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');

}

我的观点(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();

最佳答案

你必须使用两个 Action 来解决这个问题!

第 1 步:

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

第 2 步:

在第二个 Action 中只是render你的 View 文件。最后,如果它多次加载操作,它会加载第二个操作并且不会多次调用您的更改功能。我希望它能正常工作

尽情享受 ;-)

关于php - Yii 框架中的 TCpdf 多次加载 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24906015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com