What are the best practices for sending headers in PHP to prompt a user to save a file, like a generated PDF?

When generating a file in PHP, such as a PDF, you can prompt the user to save it by setting the appropriate headers. This ensures that the file is downloaded by the user instead of being displayed in the browser. To achieve this, you need to send headers specifying the content type as application/pdf and setting the Content-Disposition to attachment with a filename.

// Set the content type and attachment header
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="generated_file.pdf"');

// Output the generated PDF file content
readfile('path_to_generated_pdf_file.pdf');