Hi , I’m using this extension http://www.yiiframework.com/wiki/695/yii2-and-pdf-files-with-mpdf/ , and I need to know how I can generate a pdf in a modal … appreciate some examples if it’s posible
Hi , I’m using this extension http://www.yiiframework.com/wiki/695/yii2-and-pdf-files-with-mpdf/ , and I need to know how I can generate a pdf in a modal … appreciate some examples if it’s posible
Hello,
In first place, you should create an action in your controller with the html data you want to print. In my case I embed an HTML layout with data, I have this code. Then launch your modal loading the controller action that generates the pdf.
public function actionPdf($id)
{
$html = file_get_contents(Yii::getAlias('@backendPrint').'?id='.$id);
$pdf = new \mPDF();
$pdf->WriteHTML($html);
$pdf->Output();
exit;
}
I hope this helps you.
Regards
this is my controller action
public function actionPdf($id)
{
$model = $this->findModel($id);
$empre = GeTcias::findOne(['esta_codi' => 'A']);
$logo = GeTlogo::findOne(['esta_codi' => 'A', 'logo_tipo' => 'DC']);
$detfac = FcDetf::find()->where(['fact_codi'=>$id])->all();
return $this->render('pdf', [
'model' => $model,
'empre' => $empre,
'logo' => $logo,
'detfac' => $detfac,
]);
}
and this is my pdf.php
<?php
if ($model->reso_pref==="") {
$prefijo=null;
$pref= null;
}
else{
$pref= 'Prefijo'.' '.$model->fcResos->reso_pref;
$prefijo=$model->reso_pref.'-';
}
if ($model->persAuxi->pers_dv!=null) {
$dv='-'.$model->persAuxi->pers_dv;
}
if ($model->esta_codi=='N') {
$anulado="ESTA FACTURA ESTA ANULADA";
}
$htmlContent='<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
font-family: Verdana, Geneva, sans-serif;
margin:0px;
}
.font1{
font-size: 24px;
}
.font2{
font-size: 12px;
}
.font3{
font-size:10px;
}
.text-center{
text-align:center;
}
.text-bold{
font-weight:bold;
}
.box{border:1px solid #000000;}
.tabla {
margin:0px;padding:0px;
border:1px solid #CCC;
border-width:0px 0px 0px 0px;
border-spacing: 0;
border-collapse: collapse;
}.tabla table{
margin:0px;padding:0px;
}
.tabla td{
border:1px solid #cccccc;
border-width:1px 1px 1px 1px;
text-align:left;
padding:2px;
color:#000000;
}
.hr1{
width:80%;
}
.bg1{
background:#CCC;
}
-->
</style>
</head>
<body>
<table width="828" border="0" cellspacing="0">
<tr>
<td width="156" align="center"><img src="'.Yii::$app->request->BaseUrl.'/images/logos/'.$logo->logo_imag.'" width="'.$logo->logo_anch.'" height="'.$logo->logo_alto.'" alt="LeFuturisme Comercializadora" /></td>
<td colspan="2" align="center"> <span class="font1">'.$empre->cias_rsoc.' <br />'.$empre->cias_slog.'</span><br />
<span class="font2">Nit.'.$empre->cias_nit.'-'.$empre->cias_dive.' '.$empre->cias_regi.'</span><br />
<span class="font3">'.$empre->cias_notf.'</span></td>
</tr>
<tr>
<td class="font2">Cliente:</td>
<td width="452" class="text-bold">'.$model->persAuxi->pers_nom1.' '.$model->persAuxi->pers_nom2.' '.$model->persAuxi->pers_ape1.' '.$model->persAuxi->pers_ape2.'</td>
<td width="198" class="font2 text-center ">FACTURA DE VENTA</td>
</tr>
<tr>
<td class="font2">Nit:</td>
<td class="text-bold">'.$model->persAuxi->pers_ndoc.$dv.'</td>
<td align="center" class="text-bold bg1">'.$prefijo.''.sprintf('%04d',$model->fact_cons).'</td>
</tr>
<tr>
<td class="font2">Dirección / Tels:</td>
<td class="text-bold">'.$model->persAuxi->pers_dirr.' / '.$model->persAuxi->pers_telf.'-'.$model->persAuxi->pers_telc.'</td>
<td align="center">Fc: '.$model->fact_fech.'</td>
</tr>
<tr>
<td class="font2">Ciudad: </td>
<td class="text-bold">'.$model->persAuxi->pers_ciur.'</td>
<td class="text-center">Fv: '.$model->fact_venc.'</td>
</tr>
<tr>
<td colspan="3" class="font2">
<table width="800px" class="tabla">
<tr class="bg1">
<td class="text-bold" align="center">DESCRIPCION</td>
<td class="text-bold" align="center">CANT</td>
<td class="text-bold" align="center">UNID</td>
<td class="text-bold" align="center">VLR UNI</td>
<td class="text-bold" align="center">%DCTO</td>
<td class="text-bold" align="center">%IVA</td>
<td class="text-bold" align="center">TOTAL</td>
</tr>';
$subTotal=0;
$totDcto=0;
$totIva=0;
$totNeto=0;
foreach($detfac as $detf){
$htmlContent.='<tr>
<td>'.$detf->getProds->prod_desc.'</td>
<td align="right">'.number_format($detf->detf_cant).'</td>
<td align="right">'.$detf->umed_abre.'</td>
<td align="right">'.number_format($detf->detf_valu).'</td>
<td align="right">'.$detf->detf_dcto.'</td>
<td align="right">'.$detf->detf_iva.'</td>
<td align="right">'.number_format($detf->detf_cant * $detf->detf_valu).'</td>
</tr>';
$dcto=($detf->detf_dcto/100)*($detf->detf_cant * $detf->detf_valu);
$stotal= $detf->detf_cant * $detf->detf_valu;
$subTotal+=$stotal;
$totDcto+= $dcto;
$totIva+=($detf->detf_iva/100)*($stotal-$dcto);
}
$totNeto=$totIva+($subTotal-$totDcto);
$htmlContent.='
</table>
</td>
</tr>
<tr><td colspan="3">
<table width="800px" border="0">
<tr>
<td width="65%" rowspan="4" valign="top"><span class="text-bold">Notas:</span> '.$model->fact_obse.'<p style="color: red; font-weight:bold; font-size:16px;" />'.$anulado.'</p></td>
<td width="15%" class="bg1">SUBTOTAL</td>
<td width="20%" align="right" class="text-bold">'.number_format($subTotal).'</td>
</tr>
<tr>
<td class="bg1">DESCUENTO</td>
<td align="right" class="text-bold">'.number_format($totDcto).'</td>
</tr>
<tr>
<td class="bg1">IVA</td>
<td align="right" class="text-bold">'.number_format($totIva).'</td>
</tr>
<tr>
<td class="bg1">NETO A PAGAR</td>
<td align="right" class="text-bold">'.number_format($totNeto).'</td>
</tr>
</table>
</td>
</tr>
<tr><td colspan="3"> <hr /> </td></tr>
<tr><td colspan="3">
<table width="800px" border="0">
<tr>
<td align="center" class="font2">Resolución Nro. '.$model->fcResos->reso_reso.' de Fecha '.$model->fcResos->reso_fech.' '.$pref.', Numeración del '.$model->fcResos->reso_ini.' al '.$model->fcResos->reso_fin.' '.$model->fcResos->reso_tipo.'. Facturación por Computador. Somos responsables de I.V.A. Somos agentes retenedores de IVA al régimen Simplificado. Esta Factura de Venta se asimila en todos sus efectos legales a la Letra de Cambio, Segùn Art. 774 del Código de Comercio . Actividad económica principal 4659. Actividades económicas secundarias 4663, 7110.</td>
</tr>
</table>
<br />
<table width="800px" border="0">
<tr>
<td width="40%" align="center" class="font2"><hr class="hr1" /></td>
<td width="20%" align="center" class="font2"> </td>
<td width="40%" align="center" class="font2"><hr class="hr1" /></td>
</tr>
<tr>
<td align="center" class="font2">Firma '.$empre->cias_rsoc.'</td>
<td align="center" class="font2"> </td>
<td align="center" class="font2">Firma y sello Cliente</td>
</tr>
</table>
</td>
</tr>
</table>
<div class="text-center font3">'.$empre->cias_dire.' / Tels: '.$empre->cias_tels.' '.$empre->cias_celu.' <br />'.$empre->cias_mail.' / '.$empre->cias_web.'</div>
</body>';
$mpdf = new mPDF;
$mpdf->WriteHTML($htmlContent);
$mpdf->Output();
exit;
?>
and in my view i have this button and the modal
<?= Html::button('+ imprmir', ['value'=>Url::to(['/Facturacion/fc-fact/pdf','id'=>$model->fact_codi]), 'class' => 'btn btn-info', 'id'=>'modalCreatePdf']) ?>
<?php
Modal::begin([
// 'header'=> '<h4> Respuestas </h4>',
'id' => 'modalpd',
'size' => 'modal-lg',
]);
echo "<div id='modalPdf'></div>";
Modal::end();
?>
and this is mi main.js
$(function () {
$('#modalProductos').click(function(){
$('#modalp').modal('show')
.find('#modalPr')
.load($(this).attr('value'));
});
$('#modalCreatePdf').click(function(){
$('#modalpd').modal('show')
.find('#modalPdf')
.load($(this).attr('value'));
});
});
When I click the button to get the following result. I’m doing wrong?
6628
when I load the view in a new tab perfectly I see pdf file plis help me
Hi, the problem is that you’re generating the PDF in your view and you should do it in your controller.
My sugestion is that you should do something like this. Generate a new controller action to generate the layout with the data.
public function actionGeneratePdf($id)
{
$model = $this->findModel($id);
$empre = GeTcias::findOne(['esta_codi' => 'A']);
$logo = GeTlogo::findOne(['esta_codi' => 'A', 'logo_tipo' => 'DC']);
$detfac = FcDetf::find()->where(['fact_codi'=>$id])->all();
return $this->render('pdf', [
'model' => $model,
'empre' => $empre,
'logo' => $logo,
'detfac' => $detfac,
]);
}
Remove the mPDF from your pdf.php file, this part I’m pasting underneath and put it in other controller action
$mpdf = new mPDF;
$mpdf->WriteHTML($htmlContent);
$mpdf->Output();
exit;
And then change the actionPdf to this. Don’t forget to add use mPDF in your controller.
public function actionPdf($id)
{
$html = file_get_contents('/Facturacion/fc-fact/generate-pdf?id='.$id);
$pdf = new \mPDF();
$pdf->WriteHTML($html);
$pdf->Output();
exit;
}
Regards.
now i get this error file_get_contents(/Facturacion/fc-fact/generate-pdf?id=4): failed to open stream: No error
it does not work help pls