2016-11-10 44 views
1

我一直試圖通過Yii2高級模板的搜索功能實現MPDF,但似乎沒有給出我想要的結果。如何使用Yii2和mpdf將當前搜索條件打印/顯示爲PDF?

我想要的是,當我把值放入textField(sales_id)時,我應該能夠使用MPDF擴展來操作打印按鈕,其條件是mpdf中顯示的數據應該只在其字段中包含類似的sales_id值,像這樣圖像下方:

view example

我在我的控制器進行的方法,像這樣:

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每銷售查看:

<?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每銷售)渲染視圖:

<?= 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的所有數據。

回答

1

嘗試使用

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

可能是您$sales_id = $request->post('sales_id');不加載值作爲你的預期..

+0

感謝您的幫助,但我通過修復where子句 - >和Where(['sales_id'] => $ model-> sales_id)到 - >和Where(['sales_id'=> $ model- > sales_id]) – JoenMarz

+0

@JoenMarz。是我的答案是正確的然後標記爲接受..否則,如果是有用的標記作爲有用.. – scaisEdge

+0

數組支架應該是 - > andFilterWhere(['sales_id'=> $ model-> sales_id]) – JoenMarz

0

由於使用了ActiveForm,因此該變量作爲modelName[salesid]傳遞到您的帖子中,而不是sales_id。您可以填充模型中使用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]) 
]); 

你需要傳遞$modelrenderPartial

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

而且然後通過$model到您的窗體中:

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

,你可以在我的控制器看,我已經做到了。我的mpdf正常顯示。我上面的問題是,如何顯示mpdf,其中sales_id值等於我的textField值。 – JoenMarz

+0

您正在傳遞字符串「model」而不是對象'$ model',即'['model',...]'而不是'['model'=> $ model']。我已經用其他問題更新了答案。 – topher

+0

感謝您的幫助,但我通過修復where子句 - >和Where(['sales_id'] => $ model-> sales_id)到 - >和Where(['sales_id'=> $ model-> sales_id ]) – JoenMarz