When adding HTML gives header error

Asked

Viewed 48 times

0

page:

<?php

     header("Content-Type: text/html; charset=utf-8"); 

?>

<html>

    <head>

        <title>Gceu IMW Muriaé - São Cristóvão </title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    </head>

    <body>

        <?php 

            require_once "../_scripts/_php/_configuracoes/erros.php";
            require_once "fpd181/PDF.php";

            $html = "<div><h1>Hoje é o dia " . date( "Y-m-d" ) . "</h1></div>";

            $pdf = new PDF();
            $pdf->AddPage();
            $pdf->SetFont('Arial','B',16);
            $pdf->WriteHTML($html);
            $pdf->Output();

        ?>

    </body>

</html>

You’re making the following mistake:

Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file in D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\pdf\fpd181\fpdf.php:271 Stack trace: #0 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\pdf\fpd181\fpdf.php(1063): FPDF->Error('Some data has a...') #1 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\pdf\fpd181\fpdf.php(999): FPDF->_checkoutput() #2 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\pdf\pdf.php(29): FPDF->Output() #3 {main} thrown in D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\pdf\fpd181\fpdf.php on line 271

But if I remove the HTML and leave only the PHP

    <?php

         header("Content-Type: text/html; charset=utf-8"); 

    ?>

<?php 

                require_once "../_scripts/_php/_configuracoes/erros.php";
                require_once "fpd181/PDF.php";

                $html = "<div><h1>Hoje é o dia " . date( "Y-m-d" ) . "</h1></div>";

                $pdf = new PDF();
                $pdf->AddPage();
                $pdf->SetFont('Arial','B',16);
                $pdf->WriteHTML($html);
                $pdf->Output();

            ?>

The error disappears and the pdf displays. But with accent errors.

Obs.: All files involved are UTF-8 unsigned GOOD

  • Try it here Change the line $pdf->WriteHTML($html);for $pdf->WriteHTML(utf8_decode($html));

  • I think I got it. To generate the pdf, you need to use the pdf header. Right? Hence the problem when I enter another header. It worked. But it is necessary to remove the HTML from there. Valeu1 But it will not display in the browser. Just download. Wanted to display in the browser!

1 answer

0

There can be no printout of the header for PDF creation by the library, even because there would be no reason to insert this code in the middle of the HTML structure, creates a call for generation of this file in another location or buffer your HTML so that it is printed after PDF.

Browser other questions tagged

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