Php 如何使用Yii2和mpdf将当前搜索条件打印/显示为PDF?
我一直试图通过Yii2高级模板的搜索功能实现MPDF,但似乎没有给出我想要的结果 我想要的是,当我将值放入Php 如何使用Yii2和mpdf将当前搜索条件打印/显示为PDF?,php,yii2,yii2-advanced-app,mpdf,Php,Yii2,Yii2 Advanced App,Mpdf,我一直试图通过Yii2高级模板的搜索功能实现MPDF,但似乎没有给出我想要的结果 我想要的是,当我将值放入textField(sales\u id)中时,我应该能够使用MPDF扩展来操作打印按钮,条件是MPDF中显示的数据应该只在其字段中包含类似的sales\u id值,如下图所示: 我在我的控制器中创建了如下方法: public function actionPrintPerSales() { // trying to get the sales_id post va
textField(sales\u id)
中时,我应该能够使用MPDF扩展来操作打印按钮,条件是MPDF中显示的数据应该只在其字段中包含类似的sales\u 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]); ?>
pdf正在工作,只是它显示了所有数据,而没有过滤销售id。由于使用了
ActiveForm
该变量作为modelName[salesid]
而不是sales\u id
传递到您的帖子中。可以使用以下命令从表单填充模型属性:
然后,您可以通过以下方式过滤这些值:
$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\u id=$request->post('sales\u id')代码>不要按预期加载值 正如你在我的控制器中看到的,我已经做到了。我的mpdf显示正常。我上面的问题是,如何显示销售id值等于我的文本字段值的mpdf。您传递的是字符串'model'
,而不是对象$model
,即['model',…]
而不是“['model'=>$model']。我已经用其他问题更新了答案。谢谢你的帮助,但我通过将where子句->andWhere(['sales\u id'=>$model->sales\u id)修复到->和where(['sales\u id'=>$model->sales\u id])中来实现这一切。谢谢你的帮助,但我通过将where子句->和where(['sales\u id'=>$model->sales\u id)修复到->andWhere中来实现这一切(['sales\u id'=>$model->sales\u id])@JoenMarz.我的答案正确吗?然后标记为已接受..否则如果有用,则标记为有用..数组括号应该是->和过滤器(['sales\u id'=>$model->sales\u id])
$dataProvider = new ActiveDataProvider([
'query' => PiutangCustomer::find()
->where(['status_piutang' => '0'])
->andWhere(['sales_id'] => $model->sales_id)
->orderBy(['customer_id' => SORT_ASC])
]);
$content = $this->renderPartial('_print-per-sales', ['model' => $model, 'dataProvider' => $dataProvider]);
<?php echo $this->render('_search-per-sales', ['model' => $model]); ?>
->andFilterWhere(['sales_id' => $model->sales_id ])