What are potential reasons for receiving the error message "Datei beginnt nicht mit "%PDF-"" when trying to output a PDF file in PHP?

The error message "Datei beginnt nicht mit "%PDF-"" indicates that the PDF file being generated does not start with the "%PDF-" header, which is necessary for it to be recognized as a PDF file. This error can occur if there is output being sent before the PDF content, such as whitespace, HTML tags, or error messages. To resolve this issue, ensure that no output is sent before generating the PDF file.

<?php
ob_start(); // Start output buffering
// Generate PDF content here

// Output PDF file
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="example.pdf"');
ob_end_flush(); // Flush output buffer
exit();
?>