Good colleagues, I am starting in yii2, I am using mpdf kartik to export a pdf with data, but it appears in the same window, I would like to know how to generate the pdf in a new window, I have tried using target _blank but without results, if you could me help I would appreciate it, greetings
Thanks for your quick response, I show you a method of the action, it refers to two views, the first one shows the initial form, and the second view does a search in a database and shows the result in table form.
actionMethod
public function actionResumenopCasas()
{
$model = new ReporteSucursales();
if($model->load(Yii::$app->request->post())&& $model->validate())
{
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('view-resumenop-casas-confirm',['model'=>$model]);
// 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/src/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' => 'Reportes Resumen Operaciones General'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Reportes Resumen Operaciones'],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
else
{
return $this->render('view-resumenop-casas-form',['model'=>$model]);
}
}
// view-resumenop-casas-confirm
use yii\db\Query;
//recibiendo datos del modelo
$desde = $model->desde;
$hasta = $model->hasta;
$sucursal = $model->sucursal;
//creando consultas
//generando consulta para obtener resumen de operaciones
$consulta_operaciones = new Query();
$consulta_operaciones->select('c_operaciones_caja.nombre,o_operaciones_caja.mdae,sum(o_operaciones_caja.impe) as importeE,o_operaciones_caja.mdas,sum(o_operaciones_caja.imps) as importeS,sum(o_operaciones_caja.rpp) as total');
$consulta_operaciones->from('o_operaciones_caja');
$consulta_operaciones->innerJoin('c_operaciones_caja','o_operaciones_caja.codigo = c_operaciones_caja.codigo');
$consulta_operaciones->where(['between','o_operaciones_caja.fecha',$desde,$hasta]);
$consulta_operaciones->andWhere(['o_operaciones_caja.cadeca'=>$sucursal]);
$consulta_operaciones->groupBy(['c_operaciones_caja.nombre','o_operaciones_caja.mdae','o_operaciones_caja.mdas']);
$consulta_operaciones->orderBy(['c_operaciones_caja.nombre'=>'SORT_DESC']);
$comando_operaciones = $consulta_operaciones->createCommand();
$filas_operaciones = $comando_operaciones->queryAll();
echo('<h3>Resumen Operaciones Casas de Cambio</h3>');
echo('<table>');
echo('<tr>');
echo('<td>Desde</td>');
echo('<td>'.$desde.'</td>');
echo('</tr>');
echo('<tr>');
echo('<td>Hasta</td>');
echo('<td>'.$hasta.'</td>');
echo('</tr>');
echo('<tr>');
echo('<td>Sucursal</td>');
echo('<td>'.$sucursal.'</td>');
echo('</tr>');
echo('</table>');
//generando la tabla
echo('<table class="table table-striped">');
echo ('<thead>');
echo('<tr>');
echo('<th>Operacion</th>');
echo('<th>Entradas</th>');
echo('<th>Importe</th>');
echo('<th>Salidas</th>');
echo('<th>Importe</th>');
echo('<th>Total</th>');
echo('</tr>');
echo('</thead>');
foreach ($filas_operaciones as $items):
$operacion = $items['nombre'];
$entradas = $items['mdae'];
$importeE = $items['importeE'];
$salidas = $items['mdas'];
$importeS = $items['importeS'];
$total = $items['total'];
echo('<tbody>');
echo('<tr>');
echo('<td>'.$operacion.'</td>');
echo('<td>'.$entradas.'</td>');
echo('<td>'.\Yii::$app->formatter->asDecimal($importeE).'</td>');
echo('<td>'.$salidas.'</td>');
echo('<td>'.\Yii::$app->formatter->asDecimal($importeS).'</td>');
echo('<td>'.\Yii::$app->formatter->asDecimal($total).'</td>');
echo('</tr>');
endforeach;
echo('</table>');
In this view I obtain the data from the model and perform a query to the database taking as parameters the data obtained from the model and then I show the results in a pdf in the form of a table, but I would like this pdf to appear on a new page
Well with this kind of design it is not possible. I would suggest submitting the form should save the data somewhere and then give another view which would display details of the model plus a link to pdf viewing action.
Something like
public function actionCreate()
{
$model = new MyModel()
//receive form
//save data
//redirect to view page
return $this->redirect(['view', 'id' => $model->id]);
}
public function actionView($id)
{
$model = MyModel::findOne(['id' => $id]);
//render to view
}
//view.php
- Render model details
- Create link with taget _blank to open export action
public function actionExport($id)
{
$model = MyModel::findOne(['id' => $id]);
$pdf = ...//mpdf stuffs
//render MPDF with inline option
}