What role does encoding play in the successful download and opening of RTF files in Word using PHP?

When downloading and opening RTF files in Word using PHP, encoding plays a crucial role in ensuring that the file is properly formatted and displayed correctly. To ensure successful download and opening, it is important to encode the RTF file in UTF-8 format before sending it to the user. This ensures that special characters and formatting are preserved during the download process.

// Encode RTF file in UTF-8 format before sending to user
$rtfContent = "RTF file content here";

header('Content-Type: application/rtf');
header('Content-Disposition: attachment; filename="example.rtf"');
echo mb_convert_encoding($rtfContent, 'UTF-8', 'auto');
exit;