如何将yii视图页面转换为PDF格式并下载?

时间:2016-05-10 12:19:08

标签: php yii

这是我想以PDF格式下载的视图页面。我的观看链接:http://localhost/myproject/Applicant/1

enter image description here

我想在该页面的右上角放置一个下载PDF按钮,当我点击它时,应该以PDF格式下载用户配置文件。我是yii的新手所以不知道该怎么做,但是通过搜索谷歌,我发现了一些扩展。

  • yii-pdf
  • mpdf1

按照上述扩展名,我将这些扩展名放在protected/extension文件夹中。那么这是我的主/配置文件..

'components'=>array(
    'user'=>array(
        // enable cookie-based authentication
        'class' => 'WebUser',
        'allowAutoLogin'=>true,
        'returnUrl'=>'/site/index#login',
        'loginUrl'=>array('/Applicant/login'),
    ),
    'ePdf' => array(
        'class'         => 'ext.yii-pdf.EYiiPdf',
        'params'        => array(
            'mpdf'     => array(
                'librarySourcePath' => 'application.extensions.mpdf.*',
                'constants'         => array(
                    '_MPDF_TEMP_PATH' => Yii::getPathOfAlias('application.runtime'),
                ),
                'class'=>'mpdf', // the literal class filename to be loaded from the vendors folder
                /*'defaultParams'     => array( // More info: http://mpdf1.com/manual/index.php?tid=184
                    'mode'              => '', //  This parameter specifies the mode of the new document.
                    'format'            => 'A4', // format A4, A5, ...
                    'default_font_size' => 0, // Sets the default document font size in points (pt)
                    'default_font'      => '', // Sets the default font-family for the new document.
                    'mgl'               => 15, // margin_left. Sets the page margins for the new document.
                    'mgr'               => 15, // margin_right
                    'mgt'               => 16, // margin_top
                    'mgb'               => 16, // margin_bottom
                    'mgh'               => 9, // margin_header
                    'mgf'               => 9, // margin_footer
                    'orientation'       => 'P', // landscape or portrait orientation
                )*/
            ),
        ),
    ),

我在ApplicantController中创建了这个动作

public function actionPDF()
{
    $mPDF1 = Yii::app()->ePdf->mpdf();
    $mPDF1->WriteHTML($this->render('UserProfileView',true));
    $mPDF1->Output();
}

接下来我该怎么办?如何获取该视图文件上的按钮,该文件链接到控制器操作以下载文件。

1 个答案:

答案 0 :(得分:0)

您需要将表单提交给某个操作。

例如,在您的视图中:

<?php echo CHtml::button('Button Text', array('submit' => array('controller/action'))); ?>

在此处按钮将调用控制器中的操作并执行该操作中需要执行的操作。

  

<强> EDITED

如果我是对的,你正在渲染protected / views / applicant / view.php,这样你就可以在侧边栏中添加指向你动作的链接:

<?php
/* @var $this ApplicantController */
/* @var $model Applicant*/

$this->breadcrumbs=array(
  'Applicant'=>array('index'),
   $model->id,
);

$this->menu=array(
    array('label'=>'List Applicant', 'url'=>array('index')),
    array('label'=>'Create Applicant', 'url'=>array('create')),
    array('label'=>'Create PDF', 'url'=>array('PDF')),

不要忘记在ApplicantController.php中添加规则:

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','view','PDF'),
            'users'=>array('*'),