Generate a pdf of a table in html and php?

Asked

Viewed 1,091 times

2

Basically I have 3 tables , I’m making a kind of Inner Join in php kk and generating a new table with the data I want ,has how to turn this table into pdf ?

`

<table>

                            <th>Nome do Aluno</th>
                            <th>Nome do Curso</th>
                            <th>Nome do Professor</th>

<?php

                $con=mysqli_connect("localhost","root","36398020","flex");
                $a=mysqli_query($con,"select count(*) from ALUNO");
                $res=mysqli_fetch_array($a);
                echo $res[0];

                    $f=$res[0];
                    $f+=10;
                    $nomeAluno=array();
                    $alunoCursoId=array();
                    $nomeCurso=array();
                    $nomeProfessor=array();

                    for($a=0;$a<=$f;$a++){
                        $sql=mysqli_query($con,"select NOME_ALUNO from ALUNO where ID_ALUNO=$a;");
                        $n=mysqli_fetch_array($sql);
                        $nome[$a]=$n[0];

                        $query=mysqli_query($con,"select ALUNO_CURSO_ID from ALUNO where ID_ALUNO=$a;");
                        $we=mysqli_fetch_array($query);
                        $alunoCursoId[$a]=$we[0];

                        $sql1=mysqli_query($con,"select NOME_CURSO from CURSO where ID_CURSO='$alunoCursoId[$a]';");
                        $n1=mysqli_fetch_array($sql1);
                        $nomeCurso[$a]=$n1[0];



                        $sql2=mysqli_query($con,"select NOME_PROFESSOR from PROFESSOR where ID_PROFESSOR='$alunoCursoId[$a]';");
                        $n2=mysqli_fetch_array($sql2);
                        $nomeProfessor[$a]=$n2[0];




                        echo $table="       
                            <tr>
                                <td>$nome[$a]</td>
                                <td>$nomeCurso[$a]</td>
                                <td>$nomeProfessor[$a]</td>
                            <tr>

                        ";
                    }
                    ?>
</table>

        <A href=pf.php>PDF</A>

</body>

<?php 
        echo $table;
?>

</html>

`

The link points to this code

`

                          <?php   
            $con=mysqli_connect     

                         ( "localhost","root","36398020","flex");

            use Dompdf\Dompdf;
            require_once'dompdf/autoload.inc.php';
            $dompdf=new DOMPDF();

            $html=file_get_contents(pdf.php);

            $dompdf->load_html($html);
            $dompdf->render();
            $dompdf->stream(
                " ".$html."",
                array("attachment"=>false )
            );



            ?>`
  • Inserts the code you already have. There are some libraries that do this.

  • I’m trying the dompdf but it ends in error

  • Put the code in the question and enter the errors that are displayed. Only then can someone help you.

  • Which error is produced?

  • The PDF simply does not open.

1 answer

2

The problem is that you are using the file_get_contents.

This function, takes what is written inside the content, it means that in this case it takes a lot of codes php that are written. It does not query the database, for example...

What you can do is use one buffer output to temporarily store that data that is generated and then use them in the Dompdf. Thus:

    use Dompdf\Dompdf;
    require_once'dompdf/autoload.inc.php';
    $dompdf=new DOMPDF();

    $html = ""; // html que será usado

    /*
        A partir daqui inicia o buffer
    */
    ob_start();  // inicio
    include "pdf.php"; // roda o arquivo

    // tudo o que foi gerado até agora, salva na variável $html
    $html .= ob_get_contents(); 

    ob_end_clean(); // joga fora o buffer

    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream();
  • The following errors occur: ) Fatal error: Uncaught Dompdf Exception: Frame not found in cellmap in C: wamp64 www Flex dompdf src Cellmap.php on line 350 ( ! ) Dompdf Exception: Frame not found in cellmap in C: wamp64 www Flex dompdf src Cellmap.php on line 350

  • @Guillermooak take a look > https://answall.com/a/51683/15361

  • This ai is a mistake of your HTML. Ai already another problem you need to dig into...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.