Lo primero que necesitamos para exportar a PDF es descargar la extensión EYiiPdf y ponerlo en la carpeta extensions, posteriormente debemos descargar http://www.mpdf1.com/mpdf/download y ponerlo en la carpeta vendors con el nombre de mpdf dentro de protected.
– En el main.php (config) debemos de agregar en el array components lo siguiente:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 'ePdf' => array ( 'class' => 'ext.yii-pdf.EYiiPdf' , 'params' => array ( 'mpdf' => array ( 'librarySourcePath' => 'application.vendors.mpdf.*' , 'constants' => array ( '_MPDF_TEMP_PATH' => Yii::getPathOfAlias( 'application.runtime' ), ), 'class' => 'mpdf' , // the literal class filename to be loaded from the vendors folder /*'defaultParams' => array( // More info: http://mpdf1.com/manual/index.php?tid=184 'mode' => '', // This parameter specifies the mode of the new document. 'format' => 'A4', // format A4, A5, ... 'default_font_size' => 0, // Sets the default document font size in points (pt) 'default_font' => '', // Sets the default font-family for the new document. 'mgl' => 15, // margin_left. Sets the page margins for the new document. 'mgr' => 15, // margin_right 'mgt' => 16, // margin_top 'mgb' => 16, // margin_bottom 'mgh' => 9, // margin_header 'mgf' => 9, // margin_footer 'orientation' => 'P', // landscape or portrait orientation )*/ ), ), ), |
– En el controlador productos (voy a usar de ejemplo) debemos agregar lo siguiente:
Para entender el ejemplo dejo la tabla productos:
CREATE TABLE `productos` ( `id_producto` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_categoria` int(10) NOT NULL, `id_marca` int(10) NOT NULL, `descripcion` varchar(150) NOT NULL, `unidad_medida` varchar(80) NOT NULL, `id_igv` int(10) NOT NULL, `precio_compra` float(30,0) NOT NULL, `descuento` float(10,2) DEFAULT NULL, PRIMARY KEY (`id_producto`), KEY `fk1` (`id_categoria`), KEY `fk2` (`id_marca`), KEY `fk3` (`id_igv`), CONSTRAINT `fk1` FOREIGN KEY (`id_categoria`) REFERENCES `categorias` (`id_categoria`), CONSTRAINT `fk2` FOREIGN KEY (`id_marca`) REFERENCES `marcas` (`id_marca`), CONSTRAINT `fk3` FOREIGN KEY (`id_igv`) REFERENCES `igv` (`id_igv`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public function actionGenerarPdf() { $model =Productos::model()->findAll(); //Consulta para buscar todos los registros $mPDF1 = Yii::app()->ePdf->mpdf( 'utf-8' , 'A4' , '' , '' ,15,15,35,25,9,9, 'P' ); //Esto lo pueden configurar como quieren, para eso deben de entrar en la web de MPDF para ver todo lo que permite. $mPDF1 ->useOnlyCoreFonts = true; $mPDF1 ->SetTitle( "JuzgadoSys - Reporte" ); $mPDF1 ->SetAuthor( "JuzgadoSys" ); $mPDF1 ->SetWatermarkText( "JuzgadoSys" ); $mPDF1 ->showWatermarkText = true; $mPDF1 ->watermark_font = 'DejaVuSansCondensed' ; $mPDF1 ->watermarkTextAlpha = 0.1; $mPDF1 ->SetDisplayMode( 'fullpage' ); $mPDF1 ->WriteHTML( $this ->renderPartial( 'pdfReport' , array ( 'model' => $model ), true)); //hacemos un render partial a una vista preparada, en este caso es la vista pdfReport $mPDF1 ->Output( 'Reporte_Productos' . date ( 'YmdHis' ), 'I' ); //Nombre del pdf y parámetro para ver pdf o descargarlo directamente. exit ; } |
– En la vista deseada (yo lo uso en el admin.php) debemos agregar un link o un botón con imagen para exportar a PDF:
1 | <?php echo CHtml::link(CHtml::image(Yii::app()->baseUrl. "/images/pdf.jpg" , "PDF" , array ( "title" => "Exportar a PDF" )), array ( "generarpdf" )); ?> |
– Agregar una vista dentro de la carpeta views (en este caso /views/productos/ pdfReport.php) en el nombre modelo (yo lo uso con el nombre de pdfReport.php) debemos agregar un link o un botón con imagen para exportar a PDF:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | <?php $contador = count ( $model ); if ( $model !== null):?> <html> <head> <style> body {font-family: sans-serif; font-size: 10pt; } p { margin: 0pt; } td { vertical-align: top; } .items td { border-left: 0.1mm solid #000000; border-right: 0.1mm solid #000000; } table thead td { background-color: #EEEEEE; text-align: center; border: 0.1mm solid #000000; } .items td.blanktotal { background-color: #FFFFFF; border: 0mm none #000000; border-top: 0.1mm solid #000000; } .items td.totals { text-align: right; border: 0.1mm solid #000000; } </style> </head> <body> <!--mpdf <htmlpageheader name= "myheader" > <table width= "100%" ><tr> <td width= "50%" style= "color:#0000BB;" ><span style= "font-weight: bold; font-size: 14pt;" >Juzgado de Paz de Hohenau</span><br />República del Paraguay<br /><span style= "font-size: 15pt;" >☎</span> 0775-232355</td> <td width= "50%" style= "text-align: right;" ><b>Listado de Productos</b></td> </tr></table> </htmlpageheader> <htmlpagefooter name= "myfooter" > <div style= "border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; " > Página {PAGENO} de {nb} </div> </htmlpagefooter> <sethtmlpageheader name= "myheader" value= "on" show-this-page= "1" /> <sethtmlpagefooter name= "myfooter" value= "on" /> mpdf--> <div style= "text-align: right" ><b>Fecha: </b><?php echo date ( "d/m/Y" ); ?> </div> <b>Total Resultados:</b> <?php echo $contador ; ?> <table class = "items" width= "100%" style= "font-size: 9pt; border-collapse: collapse;" cellpadding= "5" > <thead> <tr> <td width= "16.666666666667%" >ID</td> <td width= "16.666666666667%" >Categoría</td> <td width= "16.666666666667%" >Marca</td> <td width= "16.666666666667%" >Descripción</td> <td width= "16.666666666667%" >Unidad de medida</td> <td width= "16.666666666667%" >Precio Compra</td> <td width= "6.666666666667%" >% Dcto</td> <td width= "6.666666666667%" >% IVA</td> </tr> </thead> <tbody> <!-- ITEMS --> <?php foreach ( $model as $row ): ?> <tr> <td align= "center" > <?php echo $row ->id_producto; ?> </td> <td align= "center" > <?php echo $row ->categoria->desc_categoria; ?> </td> <td align= "center" > <?php echo $row ->marca->desc_marca; ?> </td> <td align= "center" > <?php echo $row ->descripcion; ?> </td> <td align= "center" > <?php echo $row ->unidad_medida; ?> </td> <td align= "center" > <?php echo MyModel::formatoPrecio( $row ->precio_compra); ?> </td> <td align= "center" > <?php echo ( $row ->descuento); ?> </td> <td align= "center" > <?php echo ( $row ->idIgv->desc); ?> </td> </tr> <?php endforeach ; ?> <!-- FIN ITEMS --> <tr> <td class = "blanktotal" colspan= "8" rowspan= "8" ></td> </tr> </tbody> </table> </body> </html> <?php endif ; ?> |
Con eso ya podrán exportar a PDF sin problema alguno.