使用kartik mpdf将表格列表导出到yii2中的pdf

时间:2017-02-19 07:04:15

标签: yii2 mpdf

我正在尝试为数据列表生成pdf文件。这里我使用了kartik mpdf扩展。我的报告视图中有3个组合框。点击我表格中的搜索按钮数据列表,当我点击打印按钮时我的所有按钮都出现在我的pdf文件和按钮中。我想跳过组合框和按钮。我也想设置生成的pdf文件。

我的观看代码:

    <?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\FiscalYear;
use app\models\CodeValue;
use yii\helpers\ArrayHelper;
use app\models\CompanyInfo;
?>

<?php $form = ActiveForm::begin(['id' => 'Company-report',
        'action' => ['company-report'],
        'method' => 'POST',

    ]); ?>

            <div class="col-sm-2">
              <?= $form->field($model, 'fy_id')->dropDownList(
                ArrayHelper::map(FiscalYear::getAllFiscalYr(),'fy_id','fiscal_yr'),
                ['prompt'=>Selct Fiscal Year.।'])?>
            </div>

            <div class="col-sm-3">
                     <?= $form->field($model, 'sfcl_id')->dropDownList(
                ArrayHelper::map(Company::getCompanyData(),'company_id','company_name'),
                ['prompt'=>'Select Company'] )?>

            </div>

        <table>
        <tr>

        <?= Html::submitButton('Search',[ 'name'=>'view', 'value' => 'view', 'class' => 'btn btn-default']) ?>
        <?= Html::submitButton('Print',[ 'name'=>'print', 'value' => 'print','target'=> '_blank', 'class' => 'btn btn-default']) ?>

        </tr>
        </table>

        <br/><br/>


    <?php ActiveForm::end(); ?>
    <div class="box">
            <div class="box-header with-border">
               <h3 class="box-title"><b>Report</b></h3>
            </div>
        </div>
    <div class="box-body table-responsive no-padding">
     <table class="table table-hover" id="myDemoTable" cellpadding="0" cellspacing="0">

        <tbody>
            <tr>
                <td style="font-weight: bold;">Sn.</td>
                <td style="font-weight: bold;">Fiscal Yr</td>
                <td style="font-weight: bold;">P1</td>
                <td style="font-weight: bold;">P2</td>
                <td style="font-weight: bold;">P3</td>
                <td style="font-weight: bold;">E1</td>
                <td style="font-weight: bold;">E2</td>
                <td style="font-weight: bold;">E3</td>
                <td style="font-weight: bold;">E4</td>
                <td style="font-weight: bold;">E5</td>

            </tr>
            <?php 

            $fyId=$model->fy_id;
            $companyId=$model->company_id;
            $pearlFinancial=array();

            $companyData=Yii::$app->db->createCommand("select * from company_report where fy_id='$fyId'  and company_id='$companyId' ")->queryAll();

            $sn=1;
            if (empty($companyData)) {
              return;
            }else{


            foreach($companyData as $pearl)
            {
            ?>
            <tr>
                <td><?php echo $sn; ?></td>
                <td><?php 
                $fisYear=FiscalYear::findFiscalYearById($model->fy_id);
                echo($fisYear->fiscal_yr);

                ?></td>
                <td><?php echo $pearl['P1']; ?></td>
                <td><?php echo $pearl['P2']; ?></td>
                <td><?php echo $pearl['P3']; ?></td>
                <td><?php echo $pearl['E1']; ?></td>
                <td><?php echo $pearl['E2']; ?></td>
                <td><?php echo $pearl['E3']; ?></td>
                <td><?php echo $pearl['E4']; ?></td>
                <td><?php echo $pearl['E5']; ?></td>

            </tr>
            <?php
            $sn++;
            }}
            ?> 

        </tbody>
    </table>
    </div>

我的控制器是:

public function actionPearlFinancialReport()
{       
    $model =  new CompanyReport;

    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        if (isset($_POST['print'])) {
        $pdf = new Pdf([
       'mode' => Pdf::MODE_UTF8,
       'format' => Pdf::FORMAT_A4,
       'orientation' => Pdf::ORIENT_PORTRAIT,
       'destination' => Pdf::DEST_BROWSER,
       'content' =>  $this->renderPartial('company_report', ['model' => $model,]), 
       'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
       'cssInline'=> '.kv-heading-1{font-size:40px}',
        'options' => ['title' => 'Pearl Financial Report'],
         'methods' => [
            'SetHeader'=>['Company Report'],
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);


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

    return $pdf->render(); 
        }

    }

    return $this->render('company_report',['model'=>$model]);
}

任何人都能解决我的问题吗?

2 个答案:

答案 0 :(得分:1)

假设您正在使用引导程序(如果您使用的是kartik小部件,则可能就是这样),您可以通过添加隐藏打印类来隐藏任何内容。应用于您的代码:

<?php $form = ActiveForm::begin(['id' => 'Company-report',
    'action' => ['company-report'],
    'method' => 'POST',
    'options' => [
        'class' => 'hidden-print',
    ],
]); ?>

答案 1 :(得分:0)

您的问题是您使用相同的视图来生成表单和pdf。您可能需要一个单独的pdf报告视图文件,您可以在其中省略按钮和组合框。

您的控制器中已有代码来设置报告样式。内联,使用cssInline或使用'cssFile'包含您自己的css文件