Vous êtes sur la page 1sur 14

Descargar mPDF(de preferencia) y descomprimir en:

C:\AppServ\www\MIAPP\protected\extensions
Reportes mediante el uso de PDF
Reporte simple de un dato
Modificar el Controlador
C:\AppServ\www\MIAPP\protected\controllers\UserController.php

Agregar la funcin actionPdf al final del controlador.
En la funcin accesRules le otorgamos permiso a los usuarios logeados para poder
ejecutar la funcin
Modificar las Vistas
C:\AppServ\www\MIAPP\protected\views\user\admin.php
Sustituir el arreglo de botones por el siguiente
Modificar las Vistas
C:\AppServ\www\MIAPP\protected\views\user\view.php
Agregar al men la siguiente lnea de cdigo:
Crear archivo pdf.php
C:\AppServ\www\MIAPP\protected\views\user\pdf.php
Crear archivo pdf.php
C:\AppServ\www\MIAPP\protected\views\user\pdf.php
<?php
//se referencia a la extensin de mPDF
$pdf = Yii::createComponent('application.extensions.MPDF57.mpdf');
$html='
<table id="yw0" class="detail-view2">
<tr class="principal">
<td colspan="2" align="center"><b>DATOS DEL USUARIO</b></td>
<tr>
<tr class="odd">
<td> <b>Nombre de usuario</b> </td>
<td> '.$model->user.'</td>
</tr>
<tr class="even">
<td> <b>Contrasea</b> </td>
<td> '.$model->password.'</td>
</tr>
</tr>
</table>';
$html= utf8_encode($html); //se codifica para no tener problema con los acentos y las .
$mpdf=new mPDF("");
$mpdf->SetHTMLHeader($header);
$mpdf->WriteHTML($html);
$mpdf->Output(""); //muestra en el navegador el archivo PDF creado
//$mpdf->Output('Ficha-USUARIO.pdf','D'); //crea un archivo PDF y lo descarga automaticamente
exit;
?>
Reporte desde un gridView
Modificar el modelo
C:\AppServ\www\MIAPP\protected\models\User.php
Agregar a la funcin search() el siguiente cdigo
Modificar el Controlador
C:\AppServ\www\MIAPP\protected\controllers\UserController.php

Agregar la funcin actionExportPdf al final del controlador.
En la funcin accesRules le otorgamos permiso a los usuarios logeados para poder
ejecutar la funcin
Modificar las Vistas
C:\AppServ\www\MIAPP\protected\views\user\admin.php
Agregar el botn exportar al men
Crear archivo exportpdf.php
C:\AppServ\www\MIAPP\protected\views\user\exportpdf.php
Crear archivo exportpdf.php
C:\AppServ\www\MIAPP\protected\views\user\exportpdf.php
<?
$pdf = Yii::createComponent('application.extensions.MPDF57.mpdf');
$dataProvider = $_SESSION['datos_filtrados']->getData();
$contador=count($dataProvider);
//creamos las cabeceras
$html.='
<table align="center"><tr>
<td align="center"><b>LISTADO DE Usuarios</b></td>
</tr></table>
Total Resultados: '.$contador.'
<table class="detail-view2" repeat_header="1" cellpadding="1" cellspacing="1"
width="100%" border="0">
<tr class="principal">
<td class="principal" width="7%">&nbsp;Nombre de usuario</td>
<td class="principal" width="7%">&nbsp;Contrasea</td>
</tr>';
$i=0;
$val=count($dataProvider);
//dentro del ciclo vamos insertando los datos obtenidos
while($i<$val){
$html.='
<tr class="odd">
<td class="odd" width="7%">&nbsp;'.$dataProvider[$i]
["user"].'</td>
<td class="odd" width="7%">&nbsp;'.$dataProvider[$i]
["password"].'</td>
';
$html.='</tr>'; $i++;
}
$html.='</table>';
$html=utf8_encode($html);
$mpdf=new mPDF("");
$mpdf->WriteHTML($html);
$mpdf->Output("");
exit; ?>

Vous aimerez peut-être aussi