如何使用 Yii2 和 mpdf 将当前搜索条件 print/display 转换为 PDF?

How to print/display current search criteria into PDF using Yii2 and mpdf?

我一直在尝试通过 Yii2 高级模板的搜索功能实现 MPDF,但似乎没有给出我想要的结果。

我想要的是当我将值放入 textField(sales_id) 时,我应该能够使用 MPDF 扩展操作打印按钮,条件是 mpdf 中显示的数据应该只包含类似 sales_id 字段中的值,如下图所示:

我在我的控制器中制作了这样的方法:

public function actionPrintPerSales()
    {
        // trying to get the sales_id post value
        $request = Yii::$app->request;
        $sales_id = $request->post('sales_id');
        $model = new PiutangCustomer();



    // Your SQL query filter here
    $dataProvider = new ActiveDataProvider([
        'query' => PiutangCustomer::find()
            ->where(['status_piutang' => '0'])
            ->andWhere(['sales_id'] => $sales_id)
            ->orderBy(['customer_id' => SORT_ASC])
    ]);

    $content = $this->renderPartial('_print-per-sales', ['model', 'dataProvider' => $dataProvider]);

    // setup kartik\mpdf\Pdf component
    $pdf = new Pdf([
    // set to use core fonts only
    'mode' => Pdf::MODE_CORE,
    // A4 paper format
    'format' => Pdf::FORMAT_A4,
    // portrait orientation
    'orientation' => Pdf::ORIENT_PORTRAIT,
    // stream to browser inline
    'destination' => Pdf::DEST_BROWSER,
    // your html content input
    'content' => $content,
    // format content from your own css file if needed or use the
    // enhanced bootstrap css built by Krajee for mPDF formatting
    'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
    // any css to be embedded if required
    'cssInline' => '.kv-heading-1{font-size:18px}',
    // set mPDF properties on the fly
    'options' => ['title' => 'Cetak Laporan Piutang Menurut Customer'],
    // call mPDF methods on the fly
    'methods' => [
    'SetHeader'=>['Laporan Piutang - NAMA TOKO / PERUSAHAAN / CV'],
    'SetFooter'=>['Powered by PFSOFT | {PAGENO}'],
    ]
    ]);

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

    // return the pdf output as per the destination setting
    return $pdf->render();
    }

我做了这样的视图:

<?php echo $this->render('_search-per-sales', ['model' => $searchModel]); ?>

和子表单 _search-per-sales 视图:

<?php $form = ActiveForm::begin([
        'action' => ['print-per-sales'],
        'method' => 'post',
    ]); ?>

<?= $form->field($model, 'sales_id')->textInput() ?>

<div class="form-group">
    <?= Html::submitButton(Yii::t('app', 'PRINT'), ['class' => 'fa fa-print btn btn-warning']) ?>
</div>

<?php ActiveForm::end(); ?>

以及从 actionPrintPerSales (_print-per-sales) 呈现的视图:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            // 'customer_id',
            [
                'label' => 'Nama Customer',
                'value' => 'customer.first_name',
            ],
            [
                'attribute' => 'nilai_piutang',
                'contentOptions' => ['style' => 'text-align:right'],
                'format'=>['decimal',0],
            ],
            [
                'attribute' => 'total_bayar',
                'contentOptions' => ['style' => 'text-align:right'],
                'format'=>['decimal',0]
            ],
            [
                'attribute' => 'sisa_piutang',
                'contentOptions' => ['style' => 'text-align:right'],
                'format'=>['decimal',0]
            ],
            'faktur',
            [
                'attribute' => 'tanggal_jatuh_tempo',
                'contentOptions' => ['style' => 'text-align:center'],
                'format' => ['date', 'php:d-M-Y']
            ],
            'sales_id',
            [
                'label' => 'Nama Sales',
                'value' => function ($data){ return $data->kodeSales->nama; },
            ],  
        ],
    ]); ?>

我的 SQL 查询过滤器在控制器内部有问题。我可以告诉我的 mpdf 正在工作,因为当我注释掉 where 子句时:

// ->andWhere('sales_id' = $sales_id)

pdf 有效,只是它显示了所有未过滤的数据 sales_id。

由于使用了 ActiveForm,变量被作为 modelName[salesid] 而不是 sales_id 传递到您的 post。您可以使用 load:

从表单填充模型属性
$model->load(Yii::$app->request->post());

然后您可以通过以下方式过滤值:

$dataProvider = new ActiveDataProvider([
    'query' => PiutangCustomer::find()
        ->where(['status_piutang' => '0'])
        ->andWhere(['sales_id'] => $model->sales_id)
        ->orderBy(['customer_id' => SORT_ASC])
]);

您需要将 $model 传递给 renderPartial

$content = $this->renderPartial('_print-per-sales', ['model' => $model, 'dataProvider' => $dataProvider]);

然后将 $model 传递到您的表单中:

<?php echo $this->render('_search-per-sales', ['model' => $model]); ?>

尝试使用

  ->andFilterWhere(['sales_id' => $model->sales_id ])

可能是您 $sales_id = $request->post('sales_id'); 没有按预期加载值..