Failed to load PDF document

Asked

Viewed 3,295 times

4

I am working with a crafts system, where the user will enter the number of the craft, the person to whom the document will be directed and the message, and would like that, if the data were successfully added to the database, a PDF with the information entered was generated, through the library DOMPDF. However, when executing, the following error is returned: "Failed to load PDF document". The code is this:

<?php
    include_once("../conexao.php");

    $numero = $_POST["numero"];
    $interessado = $_POST["interessado"];
    $assunto = $_POST["assunto"];       

    $query = mysqli_query($conexao, "INSERT INTO oficio VALUES ('$numero','$interessado','$assunto');");

    use Dompdf\Dompdf;
    require_once ("../dompdf/autoload.inc.php");

    if(mysqli_affected_rows($conexao) != 0){
        $html = "<p>".$numero."</p>";
        $html .= "<p>".$interessado."</p>";
        $html .= "<p>".$assunto."</p>";

        $dompdf = new DOMPDF();
        //echo $html;

        $dompdf->load_html("<h1 style='text-align: center;'>Teste - Gerar PDF</h1>".$html);
        $dompdf->render();
        $dompdf->stream("teste_pdf.pdf",array("Attachment" => false));
    }
    else{
        echo "<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=http://localhost/principal.php?link=01'>
                <script type=\"text/javascript\">
                    alert(\"Não foi possível realizar a abertura do ofício!\");
                </script>";
    }
?>

What could be done to create the document?

1 answer

1

I have two suggestions:

  • First try to see which text charset comes from your database, it may be that in the database you are using ISO-8859-1 and the lib DOMPDF user UTF-8
  • The documentation of this lib says that poorly formatted HTML can cause trouble (check if it has no tag without closing, or using invalid attributes) (https://github.com/dompdf/dompdf#Limitations-known-issues).

Browser other questions tagged

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