Greetings, when generating my report with DOMPDF, look what it throws at me:
It generates a blank page with my data in the way it is in the image and with this error at the bottom of the page:
Unable to stream pdf: headers already sent
Does anyone know what could be going on? I will leave the code that I use:
<?php
require('../lib/conexion.php');
?>
<html>
<head>
<meta charset="UTF-8">
</head>
<style media="screen">
body{
margin: 20px;
}
img{
width: 150px;
position: absolute;
left: 75%;
top: 0;
}
.head{
position: absolute;
left: 65%;
top: 14%;
background: #B40431;
color: #fff;
}
h2,h3,h5{
font-weight: bold;
}
</style>
<?php
$e = "SELECT * FROM empresa";
$s = $mysqli->query($e);
$r = $s -> fetch_assoc();
?>
<body><br>
<img src="../imgs/<?php echo $r['logo'];?>">
<h5 style="margin-left:45px;"><?php echo $r['tipo']?></h5>
<h5><?php echo $r['name']?></h5>
<h6 style="margin-left:45px">Calle Merida con Av. Libertad Edificio Don
Rosario Piso 1 Locales 1 y 2</h6>
<h6 style="margin-left:125px;">Barinas Estado Barinas - Venezuela</h6>
<table class="head" width="250px" border="none">
<thead>
<tr>
<th>FECHA:</th>
<th><?php echo date('d-m-Y')?></th>
</tr>
</thead>
</table>
<center>
<h5>CIERRE DE CAJA DEL DIA: <?php echo date('d-m-Y')?> HASTA LAS <?php
echo date('h:i:s')?></h5>
</center>
<table width="100%" border="none">
<thead style="background:#B40431">
<tr>
<th>NRO</th>
<th><center>CONCEPTO</center></th>
<th>MONTO Bs.</th>
</tr>
</thead>
<?php
$hoy = date('d-m-Y');
$query = "SELECT id,concepto,monto FROM ingresos WHERE fecha = '$hoy'";
$sql = $mysqli->query($query);
if($sql->num_rows >0){
$ingresos = 0;
while($row = $sql -> fetch_assoc()){
$ingresos += intval($row['monto'])
?>
<tbody>
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['concepto']?></td>
<td><?php echo $row['monto']?></td>
</tr>
</tbody>
<?php }
?>
<?php } else {
$ingresos = 0;
}
?>
</table><br><br>
<?php
$egresos = "SELECT monto FROM egresos WHERE fecha = '$hoy'";
$sql2 = $mysqli->query($egresos);
if($sql2->num_rows>0){
$t_egresos = 0;
while($row2 = $sql2->fetch_assoc()){
$t_egresos += intval($row2['monto']);
}
}else{
$t_egresos = 0;
}
?>
<table width="100%" border="none">
<thead style="background:#01A9DB">
<tr>
<th>TOTAL INGRESOS: </th>
<th><center><?php echo $ingresos; ?> Bs.</center></th>
</tr>
</thead>
<tbody>
<tr>
<?php $total = $ingresos - $t_egresos; ?>
<td style="background:#0404B4; color:#fff">TOTAL EGRESOS: </td>
<td style="background:#0404B4; color:#fff"><center><?php echo
$t_egresos; ?> Bs.</center></td>
</tr>
<th style="background:#01A9DB;">SALDO ACTUAL EN CAJA DEL DIA <?php echo
$hoy; ?> HASTA LAS <?php echo date('h:i:s') ?></th>
<th style="background:#01A9DB;"><center><?php echo $total; ?> Bs.
</center></th>
</tbody>
</table><br>
<span style="margin-left:60%">INGRESO</span> <span style="margin-
left:5%"><?php echo $ingresos; ?> Bs.</span>
<span style="margin-left:60%">EGRESO</span> <span style="margin-
left:5%"><?php echo $t_egresos; ?> Bs.</span>
<span style="margin-left:60%">PUNTO</span>
<span style="margin-left:60%">TRANSFERENCIA</span>
<span style="margin-left:60%">EFECTIVO</span>
<span style="margin-left:60%">DEVOLUCIÓN</span>
</body>
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new DOMPDF();
$dompdf->set_paper("A4", "letter");
$dompdf->loadHtml(ob_get_clean());
$dompdf->render();
$pdf = $dompdf->output();
$nombre_archivo = 'comprobante.pdf';
$dompdf->stream($nombre_archivo,array("Attachment"=>0));
?>
</html>
This usually happens because somewhere in your code a whitespace or newline is being generated.
It is likely that some result from DB will come with the white space or new line.
Use this code before the
stream
for you to discard:If it does not detect, upload the code a few more lines until it is located.
as julius says. With this function, you will find the spaces or header that give you the problem.
In my case, the blank space was generated in the file where my connection to my database is.