Problema con FPDF, usando AJAX y PHP

  • Autor Autor giulichajari
  • Fecha de inicio Fecha de inicio
giulichajari

giulichajari

Gamma
Verificado por Whatsapp
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Mediante un llamado AJAX invoco a reportecaja.php que deberia generar un archivo pdf y abrirlo en una nueva pestaña.

Obtengo este error:

<b>Fatal error</b>: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file' in C:\xampp\htdocs\gestionweb\fpdf.php:271
Stack trace:
#0 C:\xampp\htdocs\gestionweb\fpdf.php(1063): FPDF-&gt;Error('Some data has a...')
#1 C:\xampp\htdocs\gestionweb\fpdf.php(999): FPDF-&gt;_checkoutput()
#2 C:\xampp\htdocs\gestionweb\views\modules\caja\reportecaja.php(200): FPDF-&gt;Output()
#3 {main}
thrown in <b>C:\xampp\htdocs\gestionweb\fpdf.php</b> on line <b>271</b><br />


PHP:
<?php

include_once ($_SERVER['DOCUMENT_ROOT'].'/fpdf.php');
include_once ($_SERVER['DOCUMENT_ROOT'].'/includes/claseConexion.php');
include_once ($_SERVER['DOCUMENT_ROOT'].'/models/claseCaja.php');
   $cajero=$_POST['cajero'];
     $sesion=$_POST['idsesion'];
     $cambio=$_POST['cambio'];

function obtenerh($ids){
    
     try
		{
            $conexion = Conexion::singleton_conexion();
	
        
            $query="SELECT horainicio FROM sesiones WHERE idsesion=".$ids.";";
   
            $conexion->beginTransaction();
            
            $arrayr=$conexion->query($query);
            $conexion->commit();
            return $arrayr;
        }
		catch(Exception $e)
		{
			die($e->getMessage());
		}
	
  
  }
  function obtenergastos($ids){
    
     try
		{
            $conexion = Conexion::singleton_conexion();
	
       
            $query="SELECT * FROM cajagrande WHERE idsesion=".$ids.";";
   
            $conexion->beginTransaction();
            
            $arrayg=$conexion->query($query);
            $conexion->commit();
            return $arrayg;
        }
		catch(Exception $e)
		{
			die($e->getMessage());
		}
	
  
  }
$mov=new Caja();
  date_default_timezone_set("America/Argentina/Buenos_Aires");
     $fecha=date("d/m/y");
  
$arraymov=$mov->obtenerMovs($cajero,$sesion);
$arraygastos=obtenergastos($sesion);
$pdf = new FPDF();
$pdf->SetLeftMargin(10);
$pdf->SetRightMargin(10);
//$pdf->AddPage('P','mio');
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Cell(175,10,'CERVEZERIA OTTRA',0,1,'C');

$pdf->Cell(175,3,'VENTA DE CERVEZA ARTESANAL ',0,1,'C');
$array=obtenerh($sesion);

$hora=$array[0]['horainicio'];
$pdf->Cell(175,5,'RESUMEN DE VENTAS Y ENTRADAS DE CAJA..  '.$fecha.' DESDE '.$hora.' HASTA '.date("H:i"). '',0,1,'C');

$pdf->SetFont('courier','B',12);
$pdf->Cell(30,5,"FECHA..:".$fecha,0,1,'L');
$pdf->Cell(30,5,"CAJERO..:".$_POST['nombre'],0,1,'L');
$pdf->SetFont('courier','B',12);

$pdf->Cell(15,6,'Cant',1,0,"C");

$pdf->Cell(90,6,utf8_decode('Descripción'),1,0,"C");
$pdf->Cell(10,6,'P/U',1,0,0);
$pdf->Cell(30,6,'Sub',1,0,"C");
$pdf->Cell(20,6,'Desc',1,0,"C");
$pdf->Cell(20,6,'Total',1,0,"C");

$pdf->ln(2);

foreach($arraymov as $f=>$r){

       if ($r['granel']==1){
          if (isset($r['descu'])){
            $descuento=$r['descu'];
        }else{
            $descuento=0;
        }
          $cant=$r['3'];
        $descripcion=$r['nombre'] . "    ". $r['medida'];
        $pu=$r['4'];
       }else{
        if (isset($r['descu'])){
            $descuento=$r['descu'];
        }else{
            $descuento=0;
        }
          $cant=$r['5'];
        $descripcion=$r['nombre'] . "    ". $r['medida'];
        $pu=$r['4'];
       } 
    
 
        $importe=$cant * $pu;
      
        $subtotal=$importe - $descuento;
        $total=$total+$subtotal;
    

            $pdf-> cell(15,12,$cant,0,0,'R');
            $y = $pdf->GetY();
			//$pdf-> cell(58,4,$descripcion,0,0,'R');
			$pdf-> Multicell(70,12,utf8_decode($descripcion),0,'L',0);
			//$pdf->SetY(40); /* Set 20 Eje Y */
 
	$pdf->SetXY(60,$y);
		  
            
            $pdf->cell(75,12,'$'.$pu,0,'C',0);
			$pdf->cell(20,12,'$'.$importe,0,'C',0);
            $pdf->cell(20,12,'$'.$descuento,0,'C',0);
             $pdf->cell(30,12,'$'.$subtotal,0,1,'C',0);
             		      $yFin = $pdf->GetY();
     

}
$posicion=0;

$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Cell(175,10,'CERVEZERIA OTTRA',0,1,'C');
$pdf->SetFont('Times','',7);
$pdf->Cell(175,3,'VENTA DE CERVEZA ARTESANAL ',0,1,'C');
$array=obtenerh($sesion);

$hora=$array[0]['horainicio'];
$pdf->Cell(175,5,'RESUMEN DE GASTOS DE CAJA..  '.$fecha.' DESDE '.$hora.' HASTA '.date("H:i"). '',0,1,'C');

$pdf->SetFont('courier','B',12);
$pdf->Cell(70,5,$fecha,0,1,'R');

$pdf->SetFont('courier','B',12);

$pdf->Cell(15,6,'Cant',1,0);

$pdf->Cell(120,6,utf8_decode('Descripción'),1,0,"C");
$pdf->Cell(20,6,'COSTO',1,0);

$pdf->Cell(20,6,'Total',1,0);

$pdf->ln(2);

$posicion=0;


$totalg=0;
var_dump($arraygastos);
foreach($arraygastos as $g=>$gasto){
    $pdf-> cell(15,12,'1',0,0,'R');
            $y = $pdf->GetY();
			//$pdf-> cell(58,4,$descripcion,0,0,'R');
			$pdf-> Multicell(200,12,utf8_decode($gasto['detalle']),0,'L',0);
			//$pdf->SetY(40); /* Set 20 Eje Y */
      $pdf->SetY($yFin);
	$pdf->SetXY(60,$y);
		  
            
            $pdf->cell(60,12,'$'.$gasto['monto'],0,'C',0);
		
             $pdf->cell(50,12,'$'.$gasto['monto'],0,1,'C',0);
             		      $yFin = $pdf->GetY();
                          $totalg=$totalg + $gasto['monto'];
    
}
$pdf->ln(4);
$pdf->Cell(76,6,'---------------------------',0,1);
$pdf->Cell(76,6,'---------------------------',0,1);
$pdf->ln(2);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(50,6,'SubTotal Movimientos............ $'.$total,0,1);
$pdf->Cell(50,6,'CAMBIO....................... $'.$cambio,0,1);
$pdf->Cell(50,6,'GASTOS....................... $'.$totalg,0,1);
$pdf->Cell(70,6,'Total........................ $'.($total - $cambio - $totalg),0,'L',1);
//$pdf->Cell(76,1,'--------------------------------------------------',0,1);

$pdf->ln(10);

$pdf->SetFont('Times','I',7);



$pdf->Output('ficha.pdf','D');
$pdf->Output();
?>

El codigo de arriba es reportecaja.php.

Algunos dicen usar window.open desde JQuery en vez de output pero la verdad no se como podria hacer:

Insertar CODE, HTML o PHP:
var sutotal;
var total;
var valor;
var entrega;
 var gral=0;
  var idsesion;
  var idcajero;
  var rem;
$(document).ready(function() {
  

     var idsesion=$("#sesion").val();
  var idcajero=$("#cajero").val();
    $("#cierre").click(function(){
       if($("#cambio").val()==""){
        alert("ingrese el cambio o 0(cero)");
       }else{
    
        rem=$("#cambio").val();
     
    
        $.ajax({
        
            type: "POST",
            url: "/views/modules/caja/procesocaja.php",
                data:{"accion":"cierre","idcajero":idcajero,"idsesion":idsesion,"monto":gral,"r":rem},
 

            error: function(){
                alert("error petición ajax");
            },
            success:function(data){
                 $.ajax({
        
            type: "POST",
            url: "/views/modules/caja/procesocaja.php",
                data:{"accion":"cj","monto":gral},
 

            error: function(){
                alert("error petición ajax");
            },
            success:function(data){
             
                }
                });
        
  var nombre=$("#cajeron").val();
                    $.ajax({
       
            type: "POST",
            url: "/views/modules/caja/reportecaja.php",
            data:{"cajero":idcajero,"idsesion":idsesion,"cambio":rem,"nombre":nombre},
        
 

            error: function(){
                alert("error petición ajax");
            },
            success:function(data){
     
           
                }
                }); 
                      


    });
 
Hola, prueba enviando directo sin Ajax y revisa esta información: Link

Saludos
 
Atrás
Arriba