Problem when generating PDF with DOMPDF?

1

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 end of the page:

Unable to stream pdf: headers already sent

Does anyone know what may be happening? I'll leave the code 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>
    
asked by Alejo Mendoza 04.09.2017 в 03:07
source

2 answers

1

This usually happens because somewhere in your code is generating a blank space or a new line.

It is likely that some BD result comes with the blank or new line.

Use this code before stream for you to discard:

$f;
$l;
if(headers_sent($f,$l)){
    echo $f,'<br/>',$l,'<br/>';
    die('se detecto linea');
}

If it does not detect, upload the code a few more lines until it is located.

    
answered by 04.09.2017 / 05:10
source
0

as Juliocpiro says. with this function, you will find the spaces or header that give you the problem.

$f;
$l;
if(headers_sent($f,$l)){
    echo $f,'<br/>',$l,'<br/>';
    die('se detecto linea');
}

in my case the blank space generated me in the file where my connection to my bd is.

    
answered by 16.09.2017 в 00:54