How to create pdf in php and mysql

2

I do not know how to work with the creation of pdf

Considering these fields
$conector =" SELECT nombre, descripcion, cantidad, precio, cantidad*precio FROM materiales";

pdfplantilla.php

<?php

    require 'fpdf/fpdf.php';

    class PDF extends FPDF
    {
        function Header()
        {
            // $this->Image('images/logo.png', 5, 5, 30 );
            $this->SetFont('Arial','B',15);
            $this->Cell(30);
            $this->Cell(120,10, 'Reporte De Estados',0,0,'C');
            $this->Ln(20);
        }

        function Footer()
        {
            $this->SetY(-15);
            $this->SetFont('Arial','I', 8);
            $this->Cell(0,10, 'Pagina '.$this->PageNo().'/{nb}',0,0,'C' );
        }       
    }
?>

pdfReporte.php

<?php
    include 'pdfPlantilla.php';
    require '../configuracion/conexion.php';

    $query = "SELECT nombre, descripcion, cantidad FROM materiales";      
    // $query = "SELECT e.estado, m.id_municipio, m.municipio FROM t_municipio AS m INNER JOIN t_estado AS e ON m.id_estado=e.id_estado";
    $resultado = $conexion->query($query);

    $pdf = new PDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();

    $pdf->SetFillColor(232,232,232);
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell(70,6,'nombre',1,0,'C',1);
    $pdf->Cell(20,6,'descripcion',1,0,'C',1);
    // $pdf->Cell(70,6,'cantidad',1,1,'C',1);
    // $pdf->Cell(70,6,'precio',1,1,'C',1);

    $pdf->SetFont('Arial','',10);

    while($row = $resultado->fetch_assoc())
    {
        $pdf->Cell(70,6,$row['nombre'],1,0,'C');
        $pdf->Cell(20,6,$row['descripcion'],1,0,'C');
        // $pdf->Cell(70,6,$row['cantidad'],1,1,'C');
        // $pdf->Cell(70,6,$row['precio'],1,1,'C');

    }
    $pdf->Output();
?>

The error I have is this:

  

Fatal error: Uncaught exception 'Exception' with message 'FPDF error:   Some data has already been output, can not send PDF file 'in   C: \ xampp \ htdocs \ www \ communal \ pdf \ fpdf \ fpdf.php: 271 Stack trace: # 0   C: \ xampp \ htdocs \ www \ communal \ pdf \ fpdf \ fpdf.php (1063): FPDF-> Error ('Some   data has a ... ') # 1 C: \ xampp \ htdocs \ www \ comunal \ pdf \ fpdf \ fpdf.php (999):   FPDF-> _checkoutput () # 2   C: \ xampp \ htdocs \ www \ communal \ pdf \ pdfReport.php (30): FPDF-> Output () # 3   {main} thrown in C: \ xampp \ htdocs \ www \ comunal \ pdf \ fpdf \ fpdf.php on line   271 -

    
asked by Gamez 27.06.2017 в 23:18
source

1 answer

0

This error can occur because something was written and, for FPDF to work correctly, there should be no output (other than what the FPDF itself generates). It is an error similar to what happens if you try to redirect with header once something has been written.

The code you share seems to be fine with the naked eye, but you must make sure that:

  • Avoid writing on the screen (with echo , var_dump , print , etc ...). You do not see any of this in your code, so it's not relevant in your case, but it's a common cause.

  • Make sure there are no errors in SQL queries . If for any reason, your SQL query or the connection to the database fails, then PHP will show an error and that will cause FPDF to fail with that message.

  • Do not have spaces at the beginning . For example, avoid things like this:

     <?php
    

    If you notice, there is a blank space just before the key <?php , that will cause FPDF to fail because there has already been an exit (that blank space) that is not FPDF.

  • Do not have spaces at the end . This may be harder to see:

    ?> 
    

    You can not see anything, but there is a blank space just after the ?> that will cause FPDF to throw that error. This can be solved by eliminating the ?> in pages that are going to be only PHP, so if there is some blank space at the end, it will be directly ignored (and it is not necessary to close the PHP tag at the end)

In your particular case, the code looks good at first sight and you do not see blank spaces (maybe you did not copy them). That's why I'm imagining that the problem is in the database / SQL query connection.

    
answered by 28.06.2017 в 03:38