JoenMarz JoenMarz - 19 days ago 14
PHP Question

Yii2 & mpdf - how to print/display current search criteria into pdf[SOLVED]

I've been trying to implement MPDF over the search function from Yii2 advanced template.

So basically, when I put the value inside the textField(sales_id), the Print Button will display pdf report using MPDF extension where the condition is that the data showed in mpdf should only contains similar sales_id value in it's fields, like this image below:

view example

I made the method in my controller like so:

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]) // solved,before was ->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();
}


For the View I made something like this:

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


And the sub-form _search-per-sales View:

<?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(); ?>


And as for the rendered view from the 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; },
],
],
]); ?>


all done!

Answer

Try use

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

could be that you $sales_id = $request->post('sales_id'); don't load the value as you expected ..