【问题标题】:export table list to pdf in yii2 using kartik mpdf使用kartik mpdf在yii2中将表格列表导出为pdf
【发布时间】:2017-02-19 07:04:14
【问题描述】:

我正在尝试为数据列表生成 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]);
}

谁能帮我解决我的问题?

【问题讨论】:

    标签: yii2 mpdf


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你,我不知道mpdf 尊重这些css 类。比我的解决方案优雅得多,并将整个报告保存在一个文件中。
    • 是的,很好——我觉得它很有用
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多