Using dompdf to render pdf in a while expression

966 views
Skip to first unread message

Oleksandr Lazarev

unread,
Feb 18, 2016, 8:13:30 AM2/18/16
to dompdf


Hello,

I still learning how to program, but I find my self in a problem. When I get a list of ids with information and I need to generate separate pdfs for each id Im getting the next error in apache logs:

[Thu Feb 18 04:08:33.054205 2016] [:error] [pid 1501] [client 181.47.196.68:1495] PHP Parse error:  syntax error, unexpected 'use' (T_USE) in /var/www/html/com/2pdf.php on line 15



My source code of the file is this:

<?php

include 'db/connect.php';
require 'dom/vendor/autoload.php';
require 'dompdf/autoload.inc.php';


//Obtenemos las terminales con las comisiones
$query = mysql_query("SELECT comisionesEnero.*, puntos.direccion FROM comisionesEnero LEFT JOIN puntos ON comisionesEnero.idTerminal=puntos.id");

while ($result = mysql_fetch_array($query, MYSQL_ASSOC)) {

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$idTerminal = $result["idTerminal"];
$direccion = $result["direccion"];
// Dinero Virual
$cantDineroVirtual = $result["cantDineroVirtual"];
$comDineroVirtual = round($result["comDineroVirtual"],1);
// Donaciones
$cantDonaciones = $result["cantDonaciones"];
$comDonaciones = round($result["comDonaciones"],1);
// Juegos
$cantJuegos = $result["cantJuegos"];
$comJuegos = round($result["comJuegos"],1);
//Facturas de telefonia
$cantPostpago1 = $result["cantPostpago1"];
$comPostpagos1 = round($result["comPostpagos1"],1);
//Facturas TV / Internet
$cantPostpago2 = $result["cantPostpago2"];
$comPostpagos2 = round($result["comPostpagos2"],1);
//Facturas de tarjetas de credito
$cantPostpago3 = $result["cantPostpago3"];
$comPostpagos3 = round($result["comPostpagos3"],1);
//Recargas de celulares
$cantRecargas = $result["cantRecargas"];
$comRecargas = round($result["comRecargas"],1);

//Calculamos comisiones para recargas dinamicas
if($cantRecargas<=40000){ $comRecargasInfo = '2%'; /*2%*/ } 
else if($cantRecargas>=40001 && $cantRecargas<=90000) { $comRecargasInfo = '2,5%'; /*2,5%*/ } 
else if($cantRecargas>=90001) { $comRecargasInfo = '3%'; /*3%*/ } 
else { $comRecargasInfo = '2%'; /*2%*/ }
//SUBE
$cantSube = $result["cantSube"];
$comSube = round($result["comSube"],1);

//Calculamos comisiones para sube dinamicas
if($cantSube<=40000){ $comSubeInfo = '0,5%'; /*0.5%*/ } 
else if($cantSube>=40001 && $cantSube<=90000) { $comSubeInfo = '0,75%'; /*0.75%*/ } 
else if($cantSube>=90001) { $comSubeInfo = '1%'; /*1%*/ } 
else { $comSubeInfo = '0,5%'; /*0.5%*/ }
//Tarjetas de telefonia
$cantTarjetasTelef = $result["cantTarjetasTelef"];
$comTarjetasTelf = round($result["comTarjetasTelf"],1);
//Servicios de TV
$cantTelevision = $result["cantTelevision"];
$comTelevision = round($result["comTelevision"],1);

$totalCom = $comDineroVirtual + $comDonaciones + $comJuegos + $comPostpagos1 + $comPostpagos2 + $comPostpagos3 + $comRecargas + $comSube + $comTarjetasTelf + $comTelevision;
$totalCom = round($totalCom);

$page = '<center><font style=font-size:30px;>Comisiones Enero</font></center>
<hr>
<br />
<left><font style=font-size:25px;font-weight:bold;>'.$idTerminal.' - '.$direccion.'</font></left>
<br /><br />

<table style=width:100%;font-size:15px;>
<tbody>
<tr>
<td>Dinero Virtual</td>`
<td>Total: $'.$cantDineroVirtual.'</td>
<td>Comision: 1%</td>
<td>Pagado: '.$comDineroVirtual.'</td>
</tr>
<tr>
<td>Donaciones</td>
<td>Total: $'.$cantDonaciones.'</td>
<td>Comision: 1%</td>
<td>Pagado: $'.$comDonaciones.'</td>
</tr>
<tr>
<td>Juegos / Pines</td>
<td>Total: $'.$cantJuegos.'</td>
<td>Comision: 2%</td>
<td>Pagado: $'.$comJuegos.'</td>
</tr>
<tr>
<td>Facturas de telefonia</td>
<td>Cantidad: '.$cantPostpago1.'</td>
<td>Comision: $0,20</td>
<td>Pagado: $'.$comPostpagos1.'</td>
</tr>
<tr>
<td>Facturas TV / Internet</td>
<td>Cantidad: '.$cantPostpago2.'</td>
<td>Comision: $0,20</td>
<td>Pagado: $'.$comPostpagos2.'</td>
</tr>
<tr>
<td>Facturas de tarjetas de credito</td>
<td>Total: $'.$cantPostpago3.'</td>
<td>Comision: 0,10%</td>
<td>Pagado: $'.$comPostpagos3.'</td>
</tr>
<tr>
<td>Recargas de celulares</td>
<td>Total: $'.$cantRecargas.'</td>
<td>Comision: '.$comRecargasInfo.'</td>
<td>Pagado: $'.$comRecargas.'</td>
</tr>
<tr>
<td>SUBE</td>
<td>Total: $'.$cantSube.'</td>
<td>Comision: '.$comSubeInfo.'</td>
<td>Pagado: $'.$comSube.'</td>
</tr>
<tr>
<td>Tarjetas de telefonia</td>
<td>Total: $'.$cantTarjetasTelef.'</td>
<td>Comision: 2,0%</td>
<td>Pagado: $'.$comTarjetasTelf.'</td>
</tr>
<tr>
<td>Servicios de TV</td>
<td>Total: $'.$cantTelevision.'</td>
<td>Comision: 2,0%</td>
<td>Pagado: $'.$comTelevision.'</td>
</tr>
</tbody>
</table>
<hr>

<left><font style=font-size:15px;font-weight:bold;>Total pagado en comisiones: $'.$totalCom.'</font></left>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<left><font style=font-size:15px;font-weight:bold;>'.$idTerminal.' - '.$direccion.'<br />
Recibí de LIBERPAGO SA $'.$totalCom.' en concepto de alquiler de espacio por terminal en el mes de Enero
<br /><br /><br /><br />
________________________________________________<br />
Firma
<br /><br /><br />
________________________________________________<br />
Aclaraci&oacute;n
<br /><br /><br />
Fecha &nbsp;___ / ___ / 2016
</font></left>
<br /><br /><br />
<br /><br /><br />
<left style=padding-left:100px;>Call Center Liberpago: 0810-220-1370</left>
<right style=padding-left:50px;>Call Center Qiwi: 0810-222-7494</right>

';

$dompdf->loadHtml($page);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
//$dompdf->stream($idTerminal);

$output = $dompdf->output();
    $file_to_save = 'comisiones/'.$idTerminal.'.pdf';
    file_put_contents($file_to_save, $output);

} //End while
?>



What I'm doing wrong in order to use dompdf in a while when I get information from my db for each record?

BrianS

unread,
Feb 18, 2016, 8:19:08 AM2/18/16
to dompdf
Move the use Dompdf\Dompdf; statement outside the while loop. It should be before the logic for your code (but after the require statement).

Reference the PHP documentation for more info: http://php.net/manual/en/language.namespaces.importing.php.
Reply all
Reply to author
Forward
0 new messages