How to solve the (Uncaught Exception: FPDF error) when trying to generate PDF with PHP

0

Error trying to create a PDF file with PHP and the FPDF library

  

Fatal error: Uncaught Exception: FPDF error: Some data has already   been output, can not send PDF file (output started at   C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ ord_com.php: 68) in   C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ fpdf \ fpdf.php: 271 Stack trace:   0 C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ fpdf \ fpdf.php (1052): FPDF-> Error ('Some data has a ...') # 1   C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ fpdf \ fpdf.php (999):   FPDF-> _checkoutput () # 2   C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ ord_com.php (538):   FPDF-> Output () # 3 {main} thrown in   C: \ xampp \ htdocs \ paginaoctago \ temp \ lev_6 \ fpdf \ fpdf.php on line 271

The code is as follows:

<body>
    <script src="js/main.js">
    </script> 
<script src="consultas.js">

</script>
    <script src="funciones_de_ord_comp.js">

    </script>


<?php
require('fpdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Hola, Mundo!');
$pdf->Output();

?>
</body>
    
asked by Cesar Velasquez 28.05.2018 в 21:28
source

1 answer

1

The solution was to remove all HTML visuals by creating a separate PHP file just to show the PDF

<?php
    require('fpdf/fpdf.php');

    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'¡Hola, Mundo!');
    $pdf->Output();

    ?>

To save without displaying it you can use by placing the parameters of the file name in the > output

$pdf->output("nombre_archivo.pdf","F")

If you want you can use variables such as

$filename = "nombre_del_archivo.pdf";
output($filename,"F");
    
answered by 13.06.2018 / 15:39
source