Q: How to fix 'Data has already been sent to output, unable to output PDF file' in MPDF I tried to use MPDF library for generating PDF.
try {
        $mpdf = new \Mpdf\Mpdf();
        $mpdf->WriteHTML('Hello World');
        // Other code
        $mpdf->Output("1.pdf", 'D');
    } catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
        // Process the exception, log, print etc.
        echo $e->getMessage();
    }

And I get this error message.

Data has already been sent to output, unable to output PDF file

I used ob_end_clean() but not working.
I used all answers in this question but nothing works for me.
TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file

A: I have got the same error.

Data has already been sent to output, unable to output PDF file

This means before creating a PDF with mPDF some data is stored in the buffer which is sent to the browser. Therefore it is unable to create PDF.
To rectify this, add this below php built-in function at the first line of your page were you are preparing data for pdf.
ob_start();

And add this below php built-in function before mPDF code (before where you are calling mpdf)
ob_end_flush();

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

This will clear the buffer output before processing mPDF.
Make sure if you use any functions to keep them on the same page, don't include a functions page where you have kept your all functions.
Read more about PHP Buffering

A: I just had this error and the correct solution for it is the following.
if when you type <?php in your script as the very first word, make sure there are no space characters or any characters before <?php whatsoever. This also happens with anything else giving the error Data has already been sent to output. Space characters like space and TAB don't appear to you and it will definitely deceive you.
This won't appear easily to anyone.

A: For me, it helped to insert <?php instead of <?.
