Rtf 文件导出第一行留空

Rtf file export leaves a first line blank

我只是想制作 .rtf 文件,其中包含简单的以下内容 ("eee")。当我打开我的文件时,它在我的输出前添加了一个空行。

header("Content-Disposition: attachment;filename=test.rtf");
    echo 'eee'; die;

我也试过

header("Content-type: application/rtf; charset=utf-8");
header("Content-Disposition: attachment;filename=test.rtf");
echo 'eee'; die;

但仍然在输出前添加空行。

当我尝试另存为 .txt 文件时,它没有添加任何空行。

header("Content-Disposition: attachment;filename=test.txt");
echo 'eee'; die;

我使用的是 codeigniter 框架。在控制器上,我加载了一个模型,该模型在 ?> 结束标记 之后有空行。删除后它工作正常。简单的文本回显在输出前添加空白 space 的原因是 RTF 文件查看器期望在第一行本身提到适当的 内容编码 。所以输出应该像 {\rtf1\ansi} 这样开始。确保之前没有插入其他文本或 space。

重要的是在行首 <?php 之前或包含的其他一些文件中(例如,在 ?> 之后)没有空行。

我正在使用

    ob_start('ob_gzhandler');
    ...
    if ($isDownload) {
      header("Content-type: text/rtf;  charset=UTF-8");
      header("Content-Disposition: attachment; filename=myfile.rtf");
      header("Expires: 0");
    }
    echo $rtf;

我通过

修复了它
    ob_start();
    ...
    if ($isDownload) {
      ob_clean()
      header("Content-type: text/rtf;  charset=UTF-8");
      header("Content-Disposition: attachment; filename=myfile.rtf");
      header("Expires: 0");
    }
    echo $rtf;