reportes en pdf yii2

I want to export a view to pdf. how should I do to get it right ?, in yii1 did something, maybe it is not the most efficient but it worked, this was my view




<?php $pdf = Yii::createComponent('application.vendors.mpdf.mpdf');

$dataProvider = $_SESSION['Anexo1']->getData();

$contador=count($dataProvider);


//$i=0;




$html.='

<table align="center"><tr>

<td align="center"> <b>Contraloria General de la Republica</b></td></tr>

<tr></tr>

<tr>

<td align="center"> <b>Hechos de Corrupci&oacute;n</br></b></td></tr>


</tr></table>

Total Resultados: '.$contador.' 

<p>=======================================================================================</p>    

     <table class="detail-view2" repeat_header="1" cellpadding="1" cellspacing="1"

width="100%" border="0">

     <tr>

               <td width="66" rowspan="18" class="style3">Organismo</td>

               <td width="119" height="15" rowspan="2"><span class="style3">No. HC</span></td>

               <td width="167" rowspan="30" class="style3">Nombre Entidad </td>

               <td width="125" rowspan="20" class="style3">Provincia</td>

               <td width="152" rowspan="20" class="style3">Municipio</td>

               <td width="85" rowspan="10"><span class="style3">Fecha Reg.</span> </td>

               <td width="90" rowspan="10" class="style3">Fecha Ocu. </td>

               <td width="89" rowspan="10" class="style3">Fecha det. </td>

               <td height="49" colspan="15" class="style3">Cant. Implicados </td>

               <td width="107" rowspan="12" class="style3">Cant. Colateral </td>

               <td width="81" rowspan="12" class="style3">Afect. MLC </td>

               <td width="72" rowspan="12" class="style3">Afect. CUP </td>

               <td width="97" rowspan="15" class="style3">Fuente Det. </td>

               

     </tr>

     <tr>

               <td width="34" height="25"><div align="center" class="style3">T</div></td>

               <td width="37"><div align="center" class="style3">E</div></td>

               <td width="40"><div align="center" class="style3">O</div></td>

     </tr>

         <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				<div align="center">===============================================================================================================================================================================================================</div></td>

               


         </tr>

';

    $afectacionmlc=0;

    $afectacionmn=0;

    //$recuperadomlc=0;

    //$recuperadomn=0;

    $imptotal=0;

    $imppertenece=0;

    $impnopertenece=0;

            

      $i=0;

      

      $sql='SELECT * FROM implicado INNER JOIN anexo1 ON (implicado.anexo1_fk = anexo1.id) WHERE anexo1_fk='.$dataProvider[$i]["id"];

$sql1='SELECT * FROM colateral INNER JOIN anexo1 ON (colateral.anexo1_fk = anexo1.id) WHERE anexo1_fk='.$dataProvider[$i]["id"];

$sql2='SELECT count(implicado.anexo1_fk) FROM anexo1 inner JOIN implicado ON (anexo1.id = implicado.anexo1_fk) WHERE anexo1_fk='.$dataProvider[$i]["id"].' AND imp_pert_entidad="No"';

$sql3='SELECT count(implicado.anexo1_fk) FROM anexo1 inner JOIN implicado ON (anexo1.id = implicado.anexo1_fk) WHERE anexo1_fk='.$dataProvider[$i]["id"].' AND imp_pert_entidad="Si"';







$lista1= Implicado::model()->with('anexo1Fk')->findAllBySql($sql);

$lista2=  Colateral::model()->with('anexo1Fk')->findAllBySql($sql1);


$sql4="SELECT * FROM anexo1 WHERE anexo1.id =".$dataProvider[$i]["id"];

$anexo= Anexo1::model()->findBySql($sql4);

$usuario=$anexo->registrado_por;


$sql5= "select * from tbl_user where username='".$usuario."'";

$usuario1= User::model()->findBySql($sql5);

$cargo=$usuario1->cargo;


$contador1= Implicado::model()->with('anexo1Fk')->count('anexo1_fk='.$dataProvider[$i]["id"]);

if ($contador1==null)

{

    $contador1= 0; 

}


$contador2= Colateral::model()->with('anexo1Fk')->count('anexo1_fk='.$dataProvider[$i]["id"]);

if ($contador2==null){

$contador2=0;  

}

 

$nopertenece= Implicado::model()->with('anexo1Fk')->countBySql($sql2);

if ($nopertenece==null){

$nopertenece=0;

}

 

$pertenece= Implicado::model()->with('anexo1Fk')->countBySql($sql3);

if ($pertenece==null){

    $pertenece=0;

}


      

      $val=count($dataProvider);

      while($i<$val){

    $html.='

        

         <tr class="odd">

               <td height="43">&nbsp;'.$dataProvider[$i]["entidadhcFk"]["orgaFk"]["orga_siglas"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["no_hc_a1"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["entidadhcFk"]["enthc_nombre"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["entidadhcFk"]["provinciaFk"]["prov_siglas"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["entidadhcFk"]["municipioFk"]["mun_desc"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["fecha_reg_a1"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["fecha_ocu_a1"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["fecha_det_a1"].'</td>

               <td>&nbsp;'.$contador1.'</td>

               <td>&nbsp;'.$pertenece.'</td>

               <td>&nbsp;'.$nopertenece.'</td>

               <td>&nbsp;'.$contador2.'</td>

               <td>&nbsp;'.$dataProvider[$i]["afect_mlc"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["afect_mn"].'</td>

               <td>&nbsp;'.$dataProvider[$i]["fuenteDetFk"]["fuente_det_siglas"].'</td>            

        </tr>

            

                                    

                            

            

            ';

$html.='</tr>

     <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				<div align="center">-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</div></td>

               


         </tr>    

'; 




    

    

   $afectacionmlc+=$dataProvider[$i]["afect_mlc"];

   $afectacionmn+=$dataProvider[$i]["afect_mn"];

   $imptotal+=$contador1;

   $imppertenece+=$pertenece;

   $impnopertenece+=$nopertenece;

   





$i++;


}


$html.='<table>

<tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr> 

$html.=</table>';

$html.='</table>

 <td align="center" class="principal" width="12%"><strong>&nbsp;Afectación MLC total:'.$afectacionmlc.'</strong></td>

    <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr>  

 <td align="center" class="principal" width="12%"><strong>&nbsp;Afectación MN total:'.$afectacionmn.'</strong></td>

     <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr> 

 <td align="center" class="principal" width="12%"><strong>&nbsp;Implicados total:'.$imptotal.'</strong></td>

     <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr> 

 <td align="center" class="principal" width="12%"><strong>&nbsp;Pertenecen Entidad total:'.$imppertenece.'</strong></td>

     <tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr> 

 <td align="center" class="principal" width="12%"><strong>&nbsp;No pertenecen entidad:'.$impnopertenece.'</strong></td>';

$html.='</table>';


$html.='<table>

<tr class="principal">

         

                <td colspan="13"  class="principal" width="12%">

				

               


         </tr> 

$html.=</table>';





$mpdf=new mPDF('', 'LETTER-L', '12','TAHOMA','','','','','','','L');

$mpdf->WriteHTML($html);

$mpdf->Output('Reporte.pdf','D');

exit; ?>



Now, in yii2 I have no idea, my goal is to export the data from gridview, either wholly or the result of a search, I know there are extensions for this, but none tells how to configure the view. thx

You can use this extension : yii2-mpdf.

You should create a view file.

Do something like this :




...

$pdf = Yii::$app->pdf;

$pdf->content = $this->renderPartial('/path/to/view', ['lista1'=>$lista1, 'lista2'=>$lista2]);

return $pdf->render();



so, i must keep my html code in my new view and add those lines at the end?? i want to show the result of my gridview, so, instead to


$pdf->content = $this->renderPartial('myview', ['lista1'=>$lista1, 'lista2'=>$lista2])

i put this


$pdf->content = $this->renderPartial('myview')

, then i created a button like this


<?= Html::a(Yii::t('app', 'pdf'),['myview'],['class' => 'btn btn-success'])?>

but when i press my button i’ve got this:Not Found (#404) , what it’s wrong because the view is there, can you help me?

You need a action "pdf" like this in your controller :




//This action will show the PDF

public function actionPdf()

{

  ...

  //Make all your queries here

  $lista1= Implicado::model()->with('anexo1Fk')->findAllBySql($sql);

  $lista2=  Colateral::model()->with('anexo1Fk')->findAllBySql($sql1);

  ...

  $pdf = Yii::$app->pdf;

  //Returns the HTML content and put it in the PDF content

  $pdf->content = $this->renderPartial('pdf', ['lista1'=>$lista1, 'lista2'=>$lista2]);

  return $pdf->render();

}




And create a view file in "view/controller/pdf" (controller is the name of your controller of course).

This is the good way to separate the view and the code.

And in a view, you can create the URL to show the PDF :


<?= Html::a(Yii::t('app', 'pdf'),['/controller/pdf'],['class' => 'btn btn-success'])?>

dude thx a lot, just one question, i have my separated of my controller,an example, in my folder views i have all my crud views separated by name of course, so i added my new view in on of them, my public function actionPdf() should be in my controllers folder in the who controls that view, so the view must be created under

view/anexo1/myview and call it like this


<?= Html::a(Yii::t('app', 'pdf'),['/anexo1/pdf'],['class' => 'btn btn-success'])?>

?

Yes your action must be in the same Controller of your CRUD ;)

Don’t forget to add the action name in the behaviors() function to be able to access it ;)

dude this is my view




'<table width="200" border="1">

  <tr>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

  </tr>

  <tr>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

  </tr>

  <tr>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

    <td>&nbsp;</td>

  </tr>';




it is under views/planificacion/pdf, now in my planificacionController i have this




public function actionPdf()

{

  

  $pdf = Yii::$app->pdf;

  //Returns the HTML content and put it in the PDF content

  $pdf->content = $this->renderPartial('pdf');

  return $pdf->render();

}



in my views/planificacion/index i put this




<?= Html::a(Yii::t('app', 'pdf'),['/planificacion/pdf'],['class' => 'btn btn-success'])?>



but it returns me back a lot of rare characters and not a pdf file, what i’m doing wrong?

Maybe be the content of the PDF is empty (does your view contains HTML ? I don’t see “echo” )

I don’t know.

I put the "echo " and still gives me page not found,what’s on the page is an empty table , my interest is to see only if the table shows, then I will include what I want to show me